.\" ident @(#)basic_string.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH basic_string 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2basic_string\fP \ - A templatized class for handling sequences of character-like entities. string and wstring are specialized versions of basic_string for \f2char\fP's and \f2wchar_t\fP's, respectively. .br typedef basic_string string; .br typedef basic_string wstring; .SH SYNOPSIS .br #include .br .br template , .br class Allocator = allocator > .RE .RS 0 .br class basic_string; .SH DESCRIPTION basic_string is a homogeneous collection of character-like entities that includes string functions such as compare, append, assign, insert, remove, and replace, along with various searches. basic_string also functions as an STL sequence container that provides random access iterators. This allows some of the generic algorithms to apply to strings. Any underlying character-like type may be used as long as an appropriate \f2char_traits\fP class is included or the default \f2traits\fP class is applicable. .SH INTERFACE .br template , .br class Allocator = allocator > .RE .RS 0 class basic_string { .br .br public: .br .br // Types .br .br typedef traits traits_type; .br typedef typename traits::char_type value_type; .br typedef Allocator allocator_type; .br typedef typename Allocator::size_type size_type; .br typedef typename Allocator::difference_type difference_type; .br typedef typename Allocator::reference reference; .br typedef typename Allocator::const_reference const_reference; .br typedef typename Allocator::pointer pointer; .br typedef typename Allocator::const_pointer const_pointer; .br typedef typename Allocator::pointer iterator; .br typedef typename Allocator::const_pointer const_iterator; .br typedef std::reverse_iterator .RE .RS 36 const_reverse_iterator; .RE .RS 0 typedef std::reverse_iterator reverse_iterator; .br .br static const size_type npos = -1; .br .br // Constructors/Destructors .br .br explicit basic_string(const Allocator& = Allocator()); .br basic_string (const basic_string&); .RE .RS 0 basic_string(const basic_string&, size_type, .RE .RS 12 size_type = npos, .br const Allocator& a = Allocator()); .RE .RS 0 basic_string(const charT*, size_type, .RE .RS 12 const Allocator& = Allocator()); .RE .RS 0 basic_string(const charT*, const Allocator& = Allocator()); .br basic_string(size_type, charT, .RE .RS 12 const Allocator& = Allocator()); .RE .RS 0 template .br basic_string(InputIterator, InputIterator, .RE .RS 12 const Allocator& = Allocator()); .RE .RS 0 ~basic_string(); .br .br // Assignment operators .br basic_string& operator=(const basic_string&); .br basic_string& operator=(const charT*); .br basic_string& operator=(charT); .br .br // Iterators .br .br iterator begin(); .br const_iterator begin() const; .br iterator end(); .br const_iterator end() const; .br .br reverse_iterator rbegin(); .br const_reverse_iterator rbegin() const; .br reverse_iterator rend(); .br const_reverse_iterator rend() const; .br .br // Capacity .br .RE .RS 2 size_type size() const; .br size_type length() const; .br size_type max_size() const; .br void resize(size_type, charT); .br void resize(size_type); .br size_type capacity() const; .br void reserve(size_type = 0); .br bool empty() const; .RE .RS 0 .br // Element access .br .RE .RS 2 const_reference operator[](size_type) const; .br reference operator[](size_type); .br const_reference at(size_type) const; .br reference at(size_type); .RE .RS 0 .br // Modifiers .br .RE .RS 2 basic_string& operator+=(const basic_string&); .br basic_string& operator+=(const charT*); .br basic_string& operator+=(charT); .RE .RS 3 .RE .RS 2 basic_string& append(const basic_string&); .br basic_string& append(const basic_string&, .RE .RS 23 size_type, size_type); .RE .RS 2 basic_string& append(const charT*, size_type); .br basic_string& append(const charT*); .br basic_string& append(size_type, charT); .br template .RE .RS 3 basic_string& append(InputIterator, InputIterator); .br .RE .RS 2 basic_string& assign(const basic_string&); .br basic_string& assign(const basic_string&, .RE .RS 23 size_type, size_type); .RE .RS 2 basic_string& assign(const charT*, size_type); .br basic_string& assign(const charT*); .br basic_string& assign(size_type, charT); .br template .RE .RS 3 basic_string& assign(InputIterator, InputIterator); .br .RE .RS 2 basic_string& insert(size_type, const basic_string&); .br basic_string& insert(size_type, const basic_string&, .RE .RS 23 size_type, size_type); .RE .RS 2 basic_string& insert(size_type, const charT*, size_type); .br basic_string& insert(size_type, const charT*); .br basic_string& insert(size_type, size_type, charT); .br iterator insert(iterator, charT = charT()); .br void insert(iterator, size_type, charT); .br template .RE .RS 3 void insert(iterator, InputIterator, InputIterator); .RE .RS 0 .RE .RS 2 basic_string& erase(size_type = 0, size_type= npos); .br iterator erase(iterator); .br iterator erase(iterator, iterator); .RE .RS 0 .RE .RS 2 basic_string& replace(size_type, size_type, .RE .RS 24 const basic_string&); .RE .RS 2 basic_string& replace(size_type, size_type, .RE .RS 24 const basic_string&, .br size_type, size_type); .RE .RS 2 basic_string& replace(size_type, size_type, .RE .RS 24 const charT*, size_type); .RE .RS 2 basic_string& replace(size_type, size_type, .RE .RS 24 const charT*); .RE .RS 2 basic_string& replace(size_type, size_type, .RE .RS 24 size_type, charT); .RE .RS 2 basic_string& replace(iterator, iterator, .RE .RS 24 const basic_string&); .RE .RS 2 basic_string& replace(iterator, iterator, .RE .RS 24 const charT*, size_type); .RE .RS 2 basic_string& replace(iterator, iterator, .RE .RS 24 const charT*); .RE .RS 2 basic_string& replace(iterator, iterator, .RE .RS 24 size_type, charT); .RE .RS 2 template .RE .RS 3 basic_string& replace(iterator, iterator, .RE .RS 24 InputIterator, InputIterator); .RE .RS 0 .RE .RS 2 size_type copy(charT*, size_type, size_type = 0) const; .br void swap(basic_string&); .RE .RS 0 .br // String operations .br .RE .RS 2 const charT* c_str() const; .br const charT* data() const; .br const allocator_type& get_allocator() const; .RE .RS 3 .RE .RS 2 size_type find(const basic_string&, .RE .RS 17 size_type = 0) const; .RE .RS 2 size_type find(const charT*, .RE .RS 17 size_type, size_type) const; .RE .RS 2 size_type find(const charT*, size_type = 0) const; .br size_type find(charT, size_type = 0) const; .br size_type rfind(const basic_string&, .RE .RS 18 size_type = npos) const; .RE .RS 2 size_type rfind(const charT*, .RE .RS 18 size_type, size_type) const; .RE .RS 2 size_type rfind(const charT*, .RE .RS 18 size_type = npos) const; .RE .RS 2 size_type rfind(charT, size_type = npos) const; .RE .RS 0 .RE .RS 2 size_type find_first_of(const basic_string&, .RE .RS 26 size_type = 0) const; .RE .RS 2 size_type find_first_of(const charT*, .RE .RS 26 size_type, size_type) const; .RE .RS 2 size_type find_first_of(const charT*, .RE .RS 26 size_type = 0) const; .RE .RS 2 size_type find_first_of(charT, size_type = 0) const; .RE .RS 0 .RE .RS 2 size_type find_last_of(const basic_string&, .RE .RS 25 size_type = npos) const; .RE .RS 2 size_type find_last_of(const charT*, .RE .RS 25 size_type, size_type) const; .RE .RS 2 size_type find_last_of(const charT*, size_type = npos) .RE .RS 23 const; .RE .RS 2 size_type find_last_of(charT, size_type = npos) const; .RE .RS 0 .RE .RS 2 size_type find_first_not_of(const basic_string&, .RE .RS 30 size_type = 0) const; .RE .RS 2 size_type find_first_not_of(const charT*, .RE .RS 30 size_type, size_type) const; .RE .RS 2 size_type find_first_not_of(const charT*, size_type = 0) .RE .RS 26 const; .RE .RS 2 size_type find_first_not_of(charT, size_type = 0) const; .RE .RS 0 .RE .RS 2 size_type find_last_not_of(const basic_string&, .RE .RS 29 size_type = npos) const; .RE .RS 2 size_type find_last_not_of(const charT*, .RE .RS 29 size_type, size_type) const; .RE .RS 2 size_type find_last_not_of(const charT*, .RE .RS 29 size_type = npos) const; .RE .RS 2 size_type find_last_not_of(charT, size_type = npos) .RE .RS 28 const; .RE .RS 0 .RE .RS 2 basic_string substr(size_type = 0, size_type = npos) .RE .RS 21 const; .RE .RS 2 int compare(const basic_string&) const; .br int compare(size_type, size_type, const basic_string&) .RE .RS 13 const; .RE .RS 2 int compare(size_type, size_type, const basic_string&, .RE .RS 14 size_type, size_type) const; .RE .RS 2 int compare(size_type, size_type, charT*) const; .br int compare(charT*) const; .br int compare(size_type, size_type, const charT*, .RE .RS 14 size_type) const; .RE .RS 0 }; .br .br // Non-member Operators .br .br template .br basic_string operator+ (const basic_string&, .RE .RS 24 const basic_string&); .RE .RS 0 template .br basic_string operator+ (const charT*, const basic_string&); .br template .br basic_string operator+ (charT, const basic_string&); .br template .br basic_string operator+ (const basic_string&, const charT*); .br template .br basic_string operator+ (const basic_string&, charT); .br .br template .br bool operator== (const basic_string&, const basic_string&); .br template .br bool operator== (const charT*, const basic_string&); .br template .br bool operator== (const basic_string&, const charT*); .br .br template .br bool operator< (const basic_string&, const basic_string&); .br template .br bool operator< (const charT*, const basic_string&); .br template .br bool operator< (const basic_string&, const charT*); .br .br template .br bool operator!= (const basic_string&, const basic_string&); .br template .br bool operator!= (const charT*, const basic_string&); .br template .br bool operator!= (const basic_string&, const charT*); .br .br template .br bool operator> (const basic_string&, const basic_string&); .br template .br bool operator> (const charT*, const basic_string&); .br template .br bool operator> (const basic_string&, const charT*); .br .br template .br bool operator<= (const basic_string&, const basic_string&); .br template .br bool operator<= (const charT*, const basic_string&); .br template .br bool operator<= (const basic_string&, const charT*); .br .br .br template .br bool operator>= (const basic_string&, const basic_string&); .br template .br bool operator>= (const charT*, const basic_string&); .br template .br bool operator>= (const basic_string&, const charT*); .br .br template .br void swap(basic_string& a, .RE .RS 9 basic_string& b); .RE .RS 0 .br template .br basic_istream& operator>> .RE .RS 28 (istream&, basic_string&); .RE .RS 0 template .br basic_ostream& operator<< .RE .RS 28 (ostream&, const basic_string&); .RE .RS 0 template .RE .RS 0 basic_istream& getline .RE .RS 28 (Stream&, basic_string&, charT); .SH CONSTRUCTORS In all cases, the \f2Allocator\fP parameter is used for storage management. .RE .RS 0 explicit .br basic_string (const Allocator& a = Allocator()); .RE .RS 3 The default constructor. Creates a basic_string with the following effects: .HP 9 \f2data() \fPa non-null pointer that is copyable and can have \f20\fP added to it .HP 0 .HP 9 \f2size() 0\fP .HP 0 .HP 13 \f2capacity() \fPan unspecified value .HP 0 .RE .br basic_string (const basic_string& str); .RE .RS 3 Creates a string that is a copy of \f2str\fP. .RE .RE .RS 0 basic_string (const basic_string& str, size_type pos, .RE .RS 13 size_type n= npos, const allocator& .br a=allocator()); .RE .RS 3 Creates a string of \f2pos<=size() \fPand determines length \f2rlen\fP of the initial string value as the smaller of \f2n\fP and \f2str.size() - pos\fP. This has the following effects: .HP 9 \f2data() \fPpoints to the first element of an allocated copy of \f2rlen\fP elements of the string controlled by \f2str\fP beginning at position \f2pos\fP .HP 0 .HP 9 \f2size() rlen\fP .HP 0 .HP 13 \f2capacity() \fPa value at least as large as \f2size()\fP .HP 0 .HP 18 \f2get_allocator() str.get_allocator()\fP .HP 0 An \f2out_of_range\fP exception is thrown if \f2pos>str.size()\fP. .RE .RE .RS 0 basic_string (const charT* s, size_type n, .RE .RS 13 const Allocator& a = Allocator()); .RE .RS 3 Creates a string that contains the first \f2n\fP characters of \f2s\fP. \f2s\fP must not be a \f2NULL\fP pointer. The effects of this constructor are: .HP 9 \f2data() \fPpoints to the first element of an allocated copy of the array whose first element is pointed to by \f2s\fP .HP 0 .HP 9 \f2size() n\fP .HP 0 .HP 13 \f2capacity() \fPa value at least as large as \f2size()\fP .HP 0 A \f2length_error\fP exception is thrown if \f2n == npos.\fP .RE .RE .RS 0 basic_string (const charT * s, .RE .RS 13 const Allocator& a = Allocator()); .RE .RS 3 Constructs a string containing all characters in \f2s\fP up to, but not including, a \f2traits::eos()\fP character. \f2s\fP must not be a null pointer. The effects of this constructor are: .HP 9 \f2data() \fPpoints to the first element of an allocated copy of the array whose first element is pointed to by \f2s\fP .HP 0 .HP 9 \f2size() traits::length(s)\fP .HP 0 .HP 13 \f2capacity() \fPa value at least as large as \f2size()\fP .HP 0 .RE .RS 0 basic_string (size_type n, charT c, .RE .RS 13 const Allocator& a = Allocator()); .RE .RS 3 Constructs a string containing \f2n\fP repetitions of \f2c\fP. A \f2length_error\fP exception is thrown if \f2n == npos\fP. The effects of this constructor are: .HP 9 \f2data() \fPpoints to the first element of an allocated array of \f2n\fP elements, each storing the initial value \f2c\fP .HP 0 .HP 9 \f2size() n\fP .HP 0 .HP 13 \f2capacity() \fPa value at least as large as \f2size()\fP .HP 0 .RE .RS 0 template .br basic_string (InputIterator first, InputIterator last, .RE .RS 14 const Allocator& a = Allocator()); .RE .RS 3 Creates a basic_string of length \f2last - first\fP filled with all values obtained by dereferencing the \f2InputIterators\fP on the range \f2[first, last)\fP. The effects of this constructor are: .HP 9 \f2data() \fPpoints to the first element of an allocated copy of the elements in the range \f2[first,last)\fP .HP 0 .HP 9 \f2size() \fPdistance between \f2first\fP and \f2last\fP .HP 0 .HP 13 \f2capacity() \fPa value at least as large as \f2size()\fP .HP 0 .RE .SH DESTRUCTORS .RE .RS 0 ~basic_string (); .RE .RS 3 Releases any allocated memory for this basic_string. .RE .SH OPERATORS .br basic_string& .br operator= (const basic_string& str); .RE .RS 3 Sets the contents of this string to be the same as \f2str\fP. The effects of \f2operator=\fP are: .HP 9 \f2data() \fPpoints to the first element of an allocated copy of the array whose first element is pointed to by \f2str.size()\fP .HP 0 .HP 9 \f2size() str.size()\fP .HP 0 .HP 13 \f2capacity() \fPa value at least as large as \f2size()\fP .HP 0 .RE .br basic_string& .br operator= (const charT * s); .RE .RS 3 Sets the contents of this string to be the same as \f2s\fP up to, but not including, the \f2traits::eos()\fP character. .RE .br basic_string& .br operator= (charT c); .RE .RS 3 Sets the contents of this string to be equal to the single \f2charT\fP \f2c\fP. .RE .br const_reference .br operator[] (size_type pos) const; .br reference .br operator[] (size_type pos); .RE .RS 3 If \f2pos < size()\fP, returns the element at position \f2pos\fP in this string. If \f2pos == size()\fP, the \f2const\fP version returns \f2charT()\fP, the behavior of the non-\f2const\fP version is undefined. The reference returned by either version is invalidated by any call to \f2c_str()\fP, \f2data()\fP, or any non-\f2const\fP member function for the object. .RE .br basic_string& .br operator+= (const basic_string& s); .br basic_string& .br operator+= (const charT* s); .br basic_string& .br operator+= (charT c); .RE .RS 3 Concatenates a string onto the current contents of this string. The second member operator uses \f2traits::length()\fP to determine the number of elements from \f2s\fP to add. The third member operator adds the single character \f2c\fP. All return a reference to this string after completion. .RE .SH ITERATORS .br iterator begin (); .br const_iterator begin () const; .RE .RS 3 Returns an iterator initialized to the first element of the string. .RE .br iterator end (); .br const_iterator end () const; .RE .RS 3 Returns an iterator initialized to the position after the last element of the string. .RE .br reverse_iterator rbegin (); .br const_reverse_iterator rbegin () const; .RE .RS 3 Returns an iterator equivalent to \f2reverse_iterator(end())\fP. .RE .br reverse_iterator rend (); .br const_reverse_iterator rend () const; .RE .RS 3 Returns an iterator equivalent to \f2reverse_iterator(begin())\fP. .RE .SH ALLOCATOR .br const allocator_type get_allocator () const; .RE .RS 3 Returns a copy of the allocator used by self for storage management. .RE .SH MEMBER FUNCTIONS .br basic_string& .br append (const basic_string& s, size_type pos, .RE .RS 7 size_type npos); .RE .RS 0 basic_string& .br append (const basic_string& s); .br basic_string& .br append (const charT* s, size_type n); .br basic_string& .br append (const charT* s); .br basic_string& .br append (size_type n, charT c ); .br template .br basic_string& .br append (InputIterator first, InputIterator last); .RE .RS 3 Append another string to the end of this string. The first two functions append the lesser of \f2n\fP and \f2s.size() - pos\fP characters of \f2s\fP, beginning at position \f2pos\fP to this string. The second member throws an \f2out_of_range\fP exception if \f2pos > str.size()\fP. The third member appends \f2n\fP characters of the array pointed to by \f2s\fP. The fourth variation appends elements from the array pointed to by \f2s\fP up to, but not including, a \f2charT()\fP character. The fifth variation appends \f2n\fP repetitions of \f2c\fP. The final \f2append\fP function appends the elements specified in the range \f2[first, last)\fP. All functions throw a \f2length_error\fP exception if the resulting lengths exceed \f2max_size()\fP. All return a reference to this string after completion. .RE .br basic_string& .br assign (const basic_string& s); .br basic_string& .br assign (const basic_string& s, .RE .RS 7 size_type pos, size_type n); .RE .RS 0 basic_string& .br assign (const charT* s, size_type n); .br basic_string& .br assign (const charT* s); .br basic_string& .br assign (size_type n, charT c ); .br template .br basic_string& .br assign (InputIterator first, InputIterator last); .RE .RS 3 Replace the value of this string with the value of another. All versions of the function assign values to this string. The first two variations assign the lesser of \f2n\fP and \f2s.size() - pos \fPcharacters of \f2s\fP, beginning at position \f2pos\fP. The second variation throws an \f2out_of_range\fP exception if \f2pos > str.size()\fP. The third version of the function assigns \f2n\fP characters of the array pointed to by \f2s\fP. The fourth version assigns elements from the array pointed to by \f2s\fP up to, but not including, a \f2charT()\fP character. The fifth assigns one or \f2n\fP repetitions of \f2c\fP. The last variation assigns the members specified by the range \f2[first, last)\fP. All functions throw a \f2length_error\fP exception if the resulting lengths exceed \f2max_size()\fP. All return a reference to this string after completion. .RE .br const_reference .br at (size_type pos) const; .br reference .br at (size_type pos); .RE .RS 3 If \f2pos < size()\fP, returns the element at position \f2pos\fP in this string. Otherwise, an \f2out_of_range\fP exception is thrown. .RE .br size_type .br capacity () const; .RE .RS 3 Returns the current storage capacity of the string. This is guaranteed to be at least as large as \f2size()\fP. .RE .br int .br compare (const basic_string& str); .RE .RS 3 Returns the result of a lexicographical comparison between elements of this string and elements of \f2str\fP. The return value is: .HP 5 \f2<0 if size() < str.size()\fP .HP 0 .HP 4 \f20 if size() == str.size()\fP .HP 0 .HP 5 \f2>0 if size() > str.size()\fP .HP 0 .RE .br int .br compare (size_type pos1, size_type n1, .RE .RS 8 const basic_string& str) const; .RE .RS 0 int .br compare (size_type pos1, size_type n1, .RE .RS 8 const basic_string& str, .br size_type pos2, size_type n2) const; .RE .RS 0 int .br compare (charT* s) const; .br int .br compare (size_type pos, size_type n1, charT* s) const; .br int .br compare (size_type pos, size_type n1, charT* s, .RE .RS 8 size_type n2) const; .RE .RS 3 Returns the result of a lexicographical comparison between elements of this string and a given comparison string. The members return, respectively: basic_string(*this,pos1,n1).compare (str) .br basic_string(*this,pos1,n1).compare (basic_string .RE .RS 37 (str, pos2, n2)) .RE .RS 3 *this.compare (basic_string(s)) .br basic_string(*this,pos,n1).compare (basic_string .RE .RS 36 (s, npos)) .RE .RS 3 basic_string(*this,pos,n1).compare (basic_string (s,n2)) .RE .br size_type .br copy (charT* s, size_type n, size_type pos = 0) const; .RE .RS 3 Replaces elements in memory with copies of elements from this string. An \f2out_of_range\fP exception is thrown if \f2pos > size()\fP. The lesser of \f2n\fP and \f2size() - pos\fP elements of this string, starting at position \f2pos\fP, are copied into the array pointed to by \f2s\fP. No terminating null is appended to \f2s\fP. .RE .br const charT* .br c_str () const; .br const charT* .br data () const; .RE .RS 3 Returns a pointer to the initial element of an array whose first \f2size()\fP elements are copies of the elements in this string. A charT\f2()\fP element is appended to the end. The elements of the array may not be altered, and the returned pointer is only valid until a non-\f2const\fP member function of this string is called. If \f2size()\fP is zero, the \f2data()\fP function returns a non-\f2NULL\fP pointer. .RE .br bool empty () const; .RE .RS 3 Returns \f2size() == 0\fP. .RE .br basic_string& .br erase (size_type pos = 0, size_type n = npos); .br iterator .br erase (iterator p); .br iterator .br erase (iterator first, iterator last); .RE .RS 3 This function removes elements from the string, collapsing the remaining elements, as necessary, to remove any space left empty. The first version of the function removes the smaller of \f2n\fP and\f2 size() - pos \fPstarting at position \f2pos\fP. An \f2out_of_range \fPexception is thrown if \f2pos > size()\fP. For the second version, \f2p\fP must be a valid iterator on the string, and the function removes the character referred to by \f2p\fP. For the last version of \f2erase\fP, both \f2first\fP and \f2last\fP must be valid iterators on the string, and the function removes the characters defined by the range \f2[first, last)\fP. The destructors for all removed characters are called. All versions of \f2erase\fP return a reference to the string after completion. .RE .br size_type .br find (const basic_string& str, size_type pos = 0) const; .RE .RS 3 Searches for the first occurrence of the substring specified by \f2str\fP in this string, starting at position \f2pos\fP. If found, it returns the index of the first character of the matching substring. If not found, returns \f2npos\fP. Equality is defined by \f2traits::eq()\fP. .RE .br size_type .br find (const charT* s, size_type pos, size_type n) const; .br size_type .br find (const charT* s, size_type pos = 0) const; .br size_type .br find (charT c, size_type pos = 0) const; .RE .RS 3 Searches for the first sequence of characters in this string that match a specified string. The variations of this function return, respectively: .br find(basic_string(s,n), pos) .br find(basic_string(s), pos) .br find(basic_string(1, c), pos) .RE .br size_type .br find_first_not_of (const basic_string& str, .RE .RS 18 size_type pos = 0) const; .RE .RS 3 Searches for the first element of this string at or after position \f2pos\fP that is not equal to any element of \f2str\fP. If found, \f2find_first_not_of\fP returns the index of the non-matching character. If all of the characters match, the function returns \f2npos\fP. Equality is defined by \f2traits::eq()\fP. .RE .RE .RS 0 size_type .br find_first_not_of (const charT* s, .RE .RS 18 size_type pos, size_type n) const; .RE .RS 0 size_type .br find_first_not_of (const charT* s, .RE .RS 18 size_type pos = 0) const; .RE .RS 0 size_type .br find_first_not_of (charT c, size_type pos = 0) const; .RE .RS 3 Searches for the first element in this string at or after position \f2pos\fP that is not equal to any element of a given set of characters. The members return, respectively: .br find_first_not_of(basic_string(s,n), pos) .br find_first_not_of(basic_string(s), pos) .br find_first_not_of(basic_string(1, c), pos) .RE .br size_type .br find_first_of(const basic_string& str, .RE .RS 13 size_type pos = 0) const; .RE .RS 3 Searches for the first occurrence at or after position \f2pos\fP of any element of \f2str\fP in this string. If found, the index of this matching character is returned. If not found, \f2npos\fP is returned. Equality is defined by \f2traits::eq()\fP. .RE .RE .RS 0 size_type .br find_first_of(const charT* s, size_type pos, .RE .RS 13 size_type n) const; .RE .RS 0 size_type .br find_first_of(const charT* s, size_type pos = 0) const; .br size_type .br find_first_of (charT c, size_type pos = 0) const; .RE .RS 3 Searches for the first occurrence in this string of any element in a specified string. The \f2find_first_of\fP variations return, respectively: .br find_first_of(basic_string(s,n), pos) .br find_first_of(basic_string(s), pos) .br find_first_of(basic_string(1, c), pos) .RE .br size_type .br find_last_not_of(const basic_string& str, .RE .RS 16 size_type pos = npos) const; .RE .RS 3 Searches for the last element of this string at or before position \f2pos\fP that is not equal to any element of \f2str\fP. If \f2find_last_not_of\fP finds a non-matching element, it returns the index of the character. If all the elements match, the function returns \f2npos\fP. Equality is defined by \f2traits::eq()\fP. .RE .RE .RS 0 size_type .br find_last_not_of(const charT* s, .RE .RS 16 size_type pos, size_type n) const; .RE .RS 0 size_type .br find_last_not_of(const charT* s, size_type pos = npos) const; .br size_type .br find_last_not_of(charT c, size_type pos = npos) const; .RE .RS 3 Searches for the last element in this string at or before position \f2pos\fP that is not equal to any element of a given set of characters. The members return, respectively: .br find_last_not_of(basic_string(s,n), pos) .br find_last_not_of(basic_string(s), pos) .br find_last_not_of(basic_string(1, c), pos) .RE .br size_type .br find_last_of(const basic_string& str, .RE .RS 12 size_type pos = npos) const; .RE .RS 3 Searches for the last occurrence of any element of \f2str\fP at or before position \f2pos\fP in this string. If found, \f2find_last_of\fP returns the index of the matching character. If not found, \f2find_last_of\fP returns \f2npos\fP. Equality is defined by \f2traits::eq()\fP. .RE .RE .RS 0 size_type .br find_last_of(const charT* s, size_type pos, .RE .RS 12 size_type n) const; .RE .RS 0 size_type .br find_last_of(const charT* s, size_type pos = npos) const; .br size_type .br find_last_of(charT c, size_type pos = npos) const; .RE .RS 3 Searches for the last occurrence in this string of any element in a specified string. The members return, respectively: .br find_last_of(basic_string(s,n), pos) .br find_last_of(basic_string(s), pos) .br find_last_of(basic_string(1, c), pos) .RE .br basic_string& .br insert(size_type pos1, const basic_string& s); .br basic_string& .br insert(size_type pos, const basic_string& s, .RE .RS 6 size_type pos2 = 0, size_type n = npos); .RE .RS 0 basic_string& .br insert(size_type pos, const charT* s, size_type n); .br basic_string& .br insert(size_type pos, const charT* s); .br basic_string& .br insert(size_type pos, size_type n, charT c); .RE .RS 3 Inserts additional elements at position \f2pos\fP in this string. All of the variants of this function throw an \f2out_of_range\fP exception if \f2pos > size()\fP. All variants also throw a \f2length_error\fP if the resulting strings exceed \f2max_size()\fP. Elements of this string are moved apart as necessary to accommodate the inserted elements. All return a reference to this string after completion. The second variation of this function inserts the lesser of \f2n\fP and \f2s.size() - pos2\fP characters of \f2s\fP, beginning at position \f2pos2\fP in this string. This version throws an \f2out_of_range\fP exception if \f2pos2 > s.size()\fP. The third version inserts \f2n\fP characters of the array pointed to by \f2s\fP. The fourth inserts elements from the array pointed to by \f2s\fP up to, but not including, a charT\f2()\fP character. Finally, the fifth variation inserts \f2n\fP repetitions of \f2c\fP. .RE .br iterator .br insert(iterator p, charT); .br void .br insert(iterator p, size_type n, charT c); .br template .br void .br insert(iterator p, InputIterator first, InputIterator last); .RE .RS 3 Inserts additional elements in this string immediately before the character referred to by \f2p\fP. All of these versions of \f2insert\fP require that \f2p\fP is a valid iterator on this string. The first version inserts a copy of \f2c\fP. The second version inserts \f2n\fP repetitions of \f2c\fP. The third version inserts characters in the range \f2[first, last).\fP The first version returns \f2p\fP. .RE .br size_type .br length() const; .RE .RS 3 Returns the number of elements contained in this string. .RE .br size_type .br max_size() const; .RE .RS 3 Returns the maximum possible size of the string. .RE .br size_type .br rfind (const basic_string& str, size_type pos = npos) const; .RE .RS 3 Searches for the last occurrence of the substring specified by \f2str\fP in the string, where the index of the first character of the substring is less than \f2pos\fP. If found, the index of the first character that matches substring is returned. If not found, \f2npos\fP is returned. Equality is defined by \f2traits::eq()\fP. .RE .br size_type .br rfind(const charT* s, size_type pos, size_type n) const; .br size_type .br rfind(const charT* s, size_type pos = npos) const; .br size_type .br rfind(charT c, size_type pos = npos) const; .RE .RS 3 Searches for the last sequence of characters in this string matching a specified string. The \f2rfind\fP variations return, respectively: .br rfind(basic_string(s,n), pos) .br rfind(basic_string(s), pos) .br rfind(basic_string(1, c), pos) .RE .br basic_string& .br replace(size_type pos, size_type n1, const basic_string& s); .br basic_string& .br replace(size_type pos1, size_type n1, .RE .RS 7 const basic_string& str, .br size_type pos2, size_type n2); .RE .RS 0 basic_string& .br replace(size_type pos, size_type n1, const charT* s, .RE .RS 7 size_type n2); .RE .RS 0 basic_string& .br replace(size_type pos, size_type n1, const charT* s); .br basic_string& .br replace(size_type pos, size_type n1, size_type n2, charT c); .RE .RS 3 The \f2replace\fP function replaces selected elements of this string with an alternate set of elements. All of these versions insert the new elements in place of \f2n1\fP elements in this string, starting at position \f2pos\fP. They each throw an \f2out_of_range\fP exception if \f2pos1 > size() \fPand a \f2length_error\fP exception if the resulting string size exceeds \f2max_size()\fP. The second version replaces elements of the original string with \f2n2\fP characters from string \f2s\fP starting at position \f2pos2\fP. It throws the \f2out_of_range\fP exception if \f2pos2 > s.size().\fP The third variation of the function replaces elements in the original string with \f2n2\fP elements from the array pointed to by \f2s\fP. The fourth version replaces elements in the string with elements from the array pointed to by \f2s\fP, up to, but not including, a charT\f2()\fP character. The fifth replaces \f2n\fP elements with \f2n2\fP repetitions of character \f2c\fP. .RE .br basic_string& .br replace(iterator i1, iterator i2, .RE .RS 7 const basic_string& str); .RE .RS 0 basic_string& .br replace(iterator i1, iterator i2, const charT* s, .RE .RS 7 size_type n); .RE .RS 0 basic_string& .br replace(iterator i1, iterator i2, const charT* s); .br basic_string& .br replace(iterator i1, iterator i2, size_type n, .RE .RS 7 charT c); .RE .RS 0 template .br basic_string& .br replace(iterator i1, iterator i2, .RE .RS 7 InputIterator j1, InputIterator j2); .RE .RS 3 Replaces selected elements of this string with an alternative set of elements. All of these versions of \f2replace\fP require iterators \f2i1\fP and \f2i2\fP to be valid iterators on this string. The elements specified by the range \f2[i1, i2)\fP are replaced by the new elements. The first version shown here replaces all members in \f2str\fP. The second version starts at position \f2i1\fP, and replaces the next \f2n\fP characters with \f2n\fP characters of the array pointed to by \f2s\fP. The third variation replaces string elements with elements from the array pointed to by \f2s\fP up to, but not including, a\f2 \fPcharT\f2()\fP character. The fourth version replaces string elements with \f2n\fP repetitions of \f2c\fP. The last variation shown here replaces string elements with the members specified in the range \f2[j1, j2)\fP. .RE .RE .RS 0 void .br reserve(size_type res_arg=0); .RE .RS 3 Assures that the storage capacity is at least \f2res_arg\fP. Throws a \f2length_error\fP exception if res_arg\f2 > max_size()\fP. .RE .br void .br resize(size_type n, charT c); .br void .br resize(size_type n); .RE .RS 3 Changes the capacity of this string to \f2n\fP. If the new capacity is smaller than the current size of the string, then the string is truncated. If the capacity is larger, then the string is padded with \f2c \fPcharacters. The latter \f2resize\fP member pads the string with default characters specified by charT\f2()\fP. Throws a \f2length_error\fP exception if \f2n > max_size()\fP. .RE .br size type .br size() const; .RE .RS 3 Return the number of elements contained in this string. .RE .br basic_string .br substr(size_type pos = 0, size_type n = npos) const; .RE .RS 3 Returns a string composed of copies of the lesser of \f2n\fP and \f2size()\fP characters in this string starting at index \f2pos\fP. Throws an \f2out_of_range\fP exception if \f2pos > size().\fP .RE .br void .br swap(basic_string& s); .RE .RS 3 Swaps the contents of this string with the contents of \f2s\fP. .RE .SH NON-MEMBER OPERATORS .br template .br basic_string .br operator+(const basic_string& lhs, const basic_string& rhs); .RE .RS 3 Returns a string of length \f2lhs.size() + rhs.size()\fP, where the first \f2lhs.size()\fP elements are copies of the elements of \f2lhs\fP, and the next \f2rhs.size()\fP elements are copies of the elements of \f2rhs\fP. .RE .br template .br basic_string .br operator+(const charT* lhs, const basic_string& rhs); .br template .br basic_string .br operator+(charT lhs, const basic_string& rhs); .br template .br basic_string .br operator+(const basic_string& lhs, const charT* rhs); .br template .br basic_string .br operator+(const basic_string& lhs, charT rhs); .RE .RS 3 Returns a string that represents the concatenation of two string-like entities. These functions return, respectively: .br basic_string(lhs) + rhs .br basic_string(1, lhs) + rhs .br lhs + basic_string(rhs) .br lhs + basic_string(1, rhs) .RE .br template .br bool .br operator==(const basic_string& lhs, .RE .RS 10 const basic_string& rhs); .RE .RS 3 Returns a boolean value of \f2true\fP if \f2lhs\fP and \f2rhs\fP are equal, and \f2false\fP if they are not. Equality is defined by the \f2compare()\fP member function. .RE .RE .RS 0 template .br bool .br operator==(const charT* lhs, const basic_string& rhs); .br template .br bool .br operator==(const basic_string& lhs, const charT* rhs); .RE .RS 3 Returns a boolean value indicating whether \f2lhs\fP and \f2rhs\fP are equal. Equality is defined by the \f2compare()\fP member function. These functions return, respectively: .br basic_string(lhs) == rhs .br lhs == basic_string(rhs) .RE .br template .br bool .br operator!=(const basic_string& lhs, .RE .RS 10 const basic_string& rhs); .RE .RS 3 Returns a boolean value representing the inequality of \f2lhs\fP and \f2rhs\fP. Inequality is defined by the \f2compare()\fP member function. .RE .RE .RS 0 template .br bool .br operator!=(const charT* lhs, const basic_string& rhs); .br template .br bool .br operator!=(const basic_string& lhs, const charT* rhs); .RE .RS 3 Returns a boolean value representing the inequality of \f2lhs\fP and \f2rhs\fP. Inequality is defined by the \f2compare()\fP member function. The functions return, respectively: .br basic_string(lhs) != rhs .br lhs != basic_string(rhs) .RE .br template .br bool .br operator<(const basic_string& lhs, const basic_string& rhs); .RE .RS 3 Returns a boolean value representing the lexicographical less-than relationship of \f2lhs\fP and \f2rhs\fP. Less-than is defined by the \f2compare() \fPmember. .RE .br template .br bool .br operator<(const charT* lhs, const basic_string& rhs); .br template .br bool .br operator<(const basic_string& lhs, const charT* rhs); .RE .RS 3 Returns a boolean value representing the lexicographical less-than relationship of \f2lhs\fP and \f2rhs\fP. Less-than is defined by the\f2 compare() \fPmember function. These functions return, respectively: .br basic_string(lhs) < rhs .br lhs < basic_string(rhs) .RE .br template .br bool .br operator>(const basic_string& lhs, const basic_string& rhs); .RE .RS 3 Returns a boolean value representing the lexicographical greater-than relationship of \f2lhs\fP and \f2rhs\fP. Greater-than is defined by the\f2 compare()\fP member function. .RE .br template .br bool .br operator>(const charT* lhs, const basic_string& rhs); .br template .br bool .br operator>(const basic_string& lhs, const charT* rhs); .RE .RS 3 Returns a boolean value representing the lexicographical greater-than relationship of \f2lhs\fP and \f2rhs\fP. Greater-than is defined by the \f2compare()\fP member. The functions return, respectively: .br basic_string(lhs) > rhs .br lhs > basic_string(rhs) .RE .br template .br bool .br operator<=(const basic_string& lhs, .RE .RS 12 const basic_string& rhs); .RE .RS 3 Returns a boolean value representing the lexicographical less-than-or-equal relationship of \f2lhs\fP and \f2rhs\fP. Less-than-or-equal is defined by the \f2compare()\fP member function. .RE .RE .RS 0 template .br bool .br operator<=(const charT* lhs, const basic_string& rhs); .br template .br bool .br operator<=(const basic_string& lhs, const charT* rhs); .RE .RS 3 Returns a boolean value representing the lexicographical less-than-or-equal relationship of \f2lhs\fP and \f2rhs\fP. Less-than-or-equal is defined by the \f2compare()\fP member function. These functions return, respectively: .br basic_string(lhs) <= rhs .br lhs <= basic_string(rhs) .RE .br template .br bool .br operator>=(const basic_string& lhs, .RE .RS 10 const basic_string& rhs); .RE .RS 3 Returns a boolean value representing the lexicographical greater-than-or-equal relationship of \f2lhs\fP and \f2rhs\fP. Greater-than-or-equal is defined by the \f2compare()\fP member function. .RE .RE .RS 0 template .br bool .br operator>=(const charT* lhs, const basic_string& rhs); .br template .br bool .br operator>=(const basic_string& lhs, const charT* rhs); .RE .RS 3 Returns a boolean value representing the lexicographical greater-than-or-equal relationship of \f2lhs\fP and \f2rhs\fP. Greater-than-or-equal is defined by the \f2compare()\fP member. The functions return, respectively: .br basic_string(lhs) >= rhs .br lhs >= basic_string(rhs) .RE .br template .br void swap(basic_string& a, .RE .RS 9 basic_string& b); .RE .RS 3 Swaps the contents of \f2a\fP and \f2b\fP by calling \f2a\fP's swap function on \f2b\fP. .RE .RE .RS 0 template .br basic_istream& .br operator>>(basic_istream& is, .RE .RS 10 basic_string& str); .RE .RS 3 Reads\f2 str\fP from \f2is\fP using \f2traits::char_in\fP until a \f2traits::is_del()\fP element is read. All elements read, except the delimiter, are placed in \f2str\fP. After the read, the function returns \f2is\fP. .RE .RE .RS 0 template .br basic_ostream& .br operator<<(basic_ostream & os, .RE .RS 10 const str); .RE .RS 3 Writes all elements of \f2str\fP to \f2os\fP in order from first to last, using \f2traits::char_out()\fP. After the write, the function returns \f2os\fP. .RE .SH NON-MEMBER FUNCTIONS .RE .RS 0 template .RE .RS 0 basic_istream .br getline(basic_istream is, .RE .RS 8 str, charT delim); .RE .RS 3 An unformatted input function that extracts characters from \f2is\fP into \f2str\fP until \f2npos - 1\fP characters are read, the end of the input sequence is reached, or the character read is \f2delim\fP. The characters are read using \f2traits::char_in()\fP. .RE .SH EXAMPLE .RE .RS 0 // .br // string.cpp .br // .RE .RS 1 #include .br #include .RE .RS 0 using namespace std; .br .br int main() .RE .RS 1 { .RE .RS 2 string test; .RE .RS 0 .RE .RS 3 //Type in a string over five characters long .RE .RS 2 while(test.empty() || test.size() <= 5) .RE .RS 3 { .RE .RS 4 cout << "Type a string between 5 and 100 .RE .RS 13 characters long. " << endl; .RE .RS 4 cin >> test; .RE .RS 3 } .RE .RS 0 .RE .RS 3 //Test operator[] access .RE .RS 2 cout << "Changing the third character from " .RE .RS 8 << test[2] << " to * " << endl; .RE .RS 2 test[2] = '*'; .br cout << "now its: " << test << endl << endl; .RE .RS 0 .RE .RS 3 //Try the insertion member function .RE .RS 2 cout << "Identifying the middle: "; .br test.insert(test.size() / 2, "(the middle is here!)"); .br cout << test << endl << endl; .RE .RS 0 .br .RE .RS 3 //Try replacement .RE .RS 2 cout << "I didn't like the word 'middle',so " .RE .RS 11 "instead, I'll say:" << endl; .RE .RS 0 .RE .RS 2 test.replace(test.find("middle",0), 6, "center"); .br cout << test << endl; .RE .RS 0 .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br Type a string between 5 and 100 characters long. .br roguewave .br Changing the third character from g to * .br now its: ro*uewave .br Identifying the middle: ro*u(the middle is here!)ewave .br I didn't like the word 'middle', so instead, I'll say: .br ro*u(the center is here!)ewave .SH SEE ALSO allocator, string, wstring