Top of document
©Copyright 1999 Rogue Wave Software

basic_string


     Strings Library

Summary

A templated class for handling sequences of character-like entities. string and wstring are specialized versions of basic_string for chars and wchar_ts, respectively.

typedef basic_string <char> string;
typedef basic_string <wchar_t> wstring;

Contents

Synopsis

#include <string>
template <class charT,
          class traits = string_char_traits<charT>,
          class Allocator = allocator>
class basic_string;

Description

basic_string<charT, traits, Allocator> is a homogeneous collection of character-like entities. It provides general string functionality such as compare, append, assign, insert, remove, and replace , along with various searches. basic_string also functions as an STL sequence container, providing 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 string_char_traits class is provided or the default traits class is applicable.

Interface

template <class charT,
          class traits = string_char_traits<charT>,
          class Allocator = allocator>
class basic_string {
public:
// Types
typedef traits                               traits_type;
typedef typename traits::char_type           value_type;
typedef Allocator                            allocator_type;
typename size_type;
typename difference_type;
typename reference;
typename const_reference;
typename pointer;
typename const_pointer;
typename iterator;
typename const_iterator;
typename const_reverse_iterator;
typename reverse_iterator;
static const size_type npos = -1;
// Constructors/Destructors
explicit basic_string(const Allocator& = Allocator());
basic_string(const basic_string&, size_type, size_type = npos);
basic_string(const charT*, size_type,
             const Allocator& = Allocator());
basic_string(const charT*, Allocator& = Allocator());
basic_string(size_type, charT,
             const Allocator& = Allocator());
template <class InputIterator>
basic_string(InputIterator, InputIterator,
             const Allocator& = Allocator());
~basic_string();
// Assignment operators
 basic_string& operator=(const basic_string&);
 basic_string& operator=(const charT*);
 basic_string& operator=(charT);
// Iterators
 iterator       begin();
 const_iterator begin() const;
 iterator       end();
 const_iterator end() const;
 reverse_iterator       rbegin();
 const_reverse_iterator rbegin() const;
 reverse_iterator       rend();
 const_reverse_iterator rend() const;
// Capacity
   size_type       size() const;
   size_type       length() const;
   size_type       max_size() const;
   void            resize(size_type, charT);
   void            resize(size_type);
   size_type       capacity() const;
   void            reserve(size_type);
   bool            empty() const;
// Element access
   charT           operator[](size_type) const;
   reference       operator[](size_type);
   const_reference at(size_type) const;
   reference       at(size_type);
// Modifiers
   basic_string& operator+=(const basic_string&);
   basic_string& operator+=(const charT*);
   basic_string& operator+=(charT);
   
   basic_string& append(const basic_string&);
   basic_string& append(const basic_string&,
                        size_type, size_type);
   basic_string& append(const charT*, size_type);
   basic_string& append(const charT*);
   basic_string& append(size_type, charT);
   template<class InputIterator>
    basic_string& append(InputIterator, InputIterator);
   
   basic_string& assign(const basic_string&);
   basic_string& assign(const basic_string&,
                        size_type, size_type);
   basic_string& assign(const charT*, size_type);
   basic_string& assign(const charT*);
   basic_string& assign(size_type, charT);
   template<class InputIterator>
    basic_string& assign(InputIterator, InputIterator);
   
   basic_string& insert(size_type, const basic_string&);
   basic_string& insert(size_type, const basic_string&,
                        size_type, size_type);
   basic_string& insert(size_type, const charT*, size_type);
   basic_string& insert(size_type, const charT*);
   basic_string& insert(size_type, size_type, charT);
   iterator insert(iterator, charT = charT());
   void insert(iterator, size_type, charT);
   template<class InputIterator>
    void insert(iterator, InputIterator,
               InputIterator);
   basic_string& erase(size_type = 0, size_type= npos);
   iterator erase(iterator);
   iterator erase(iterator, iterator);
   basic_string& replace(size_type, size_type,
                         const basic_string&);
   basic_string& replace(size_type, size_type,
                         const basic_string&,
                         size_type, size_type);
   basic_string& replace(size_type, size_type,
                         const charT*, size_type);
   basic_string& replace(size_type, size_type,
                         const charT*);
   basic_string& replace(size_type, size_type,
                         size_type, charT);
   basic_string& replace(iterator, iterator,
                         const basic_string&);
   basic_string& replace(iterator, iterator,
                         const charT*, size_type);
   basic_string& replace(iterator, iterator,
                         const charT*);
   basic_string& replace(iterator, iterator,
                         size_type, charT);
   template<class InputIterator>
    basic_string& replace(iterator, iterator,
                         InputIterator, InputIterator);
   size_type copy(charT*, size_type, size_type = 0);
   void swap(basic_string<charT, traits, Allocator>&);
// String operations
   const charT* c_str() const;
   const charT* data() const;
   const allocator_type& get_allocator() const;
   
   size_type find(const basic_string&,
                  size_type = 0) const;
   size_type find(const charT*,
                  size_type, size_type) const;
   size_type find(const charT*, size_type = 0) const;
   size_type find(charT, size_type = 0) const;
   size_type rfind(const basic_string&,
                   size_type = npos) const;
   size_type rfind(const charT*,
                   size_type, size_type) const;
   size_type rfind(const charT*,
                   size_type = npos) const;
   size_type rfind(charT, size_type = npos) const;
   size_type find_first_of(const basic_string&,
                           size_type = 0) const;
   size_type find_first_of(const charT*,
                           size_type, size_type) const;
   size_type find_first_of(const charT*,
                           size_type = 0) const;
   size_type find_first_of(charT, size_type = 0) const;
   size_type find_last_of(const basic_string&,
                          size_type = npos) const;
   size_type find_last_of(const charT*,
                          size_type, size_type) const;
   size_type find_last_of(const charT*, size_type = npos) const;
   size_type find_last_of(charT, size_type = npos) const;
   size_type find_first_not_of(const basic_string&,
                               size_type = 0) const;
   size_type find_first_not_of(const charT*,
                               size_type, size_type) const;
   size_type find_first_not_of(const charT*, size_type = 0) const;
   size_type find_first_not_of(charT, size_type = 0) const;
   size_type find_last_not_of(const basic_string&,
                              size_type = npos) const;
   size_type find_last_not_of(const charT*,
                              size_type, size_type) const;
   size_type find_last_not_of(const charT*, 
                              size_type = npos) const;
   size_type find_last_not_of(charT, size_type = npos) const;
   basic_string substr(size_type = 0, size_type = npos) const;
   int compare(const basic_string&) const;
   int compare(size_type, size_type, const basic_string&) const;
   int compare(size_type, size_type, const basic_string&,
               size_type, size_type) const;
   int compare(size_type, size_type, charT*) const;
   int compare(charT*) const;
   int compare(size_type, size_type, const charT*, size_type) const;
};
// Non-member Operators
template <class charT, class traits, class Allocator>
 basic_string operator+ (const basic_string&,
                         const basic_string&);
template <class charT, class traits, class Allocator>
 basic_string operator+ (const charT*, const basic_string&);
template <class charT, class traits, class Allocator>
 basic_string operator+ (charT, const basic_string&);
template <class charT, class traits, class Allocator>
 basic_string operator+ (const basic_string&, const charT*);
template <class charT, class traits, class Allocator>
 basic_string operator+ (const basic_string&, charT);
template <class charT, class traits, class Allocator>
 bool operator== (const basic_string&, const basic_string&);
template <class charT, class traits, class Allocator>
 bool operator== (const charT*, const basic_string&);
template <class charT, class traits , class Allocator>
 bool operator== (const basic_string&, const charT*);
template <class charT, class traits, class Allocator>
 bool operator< (const basic_string&, const basic_string&);
template <class charT, class traits, class Allocator>
 bool operator< (const charT*, const basic_string&);
template <class charT, class traits, class Allocator>
 bool operator< (const basic_string&, const charT*);
template <class charT, class traits, class Allocator>
 bool operator!= (const basic_string&, const basic_string&);
template <class charT, class traits, class Allocator>
 bool operator!= (const charT*, const basic_string&);
template <class charT, class traits, class Allocator>
 bool operator!= (const basic_string&, const charT*);
template <class charT, class traits, class Allocator>
 bool operator> (const basic_&, const basic_string&);
template <class charT, class traits, class Allocator>
 bool operator> (const charT*, const basic_string&);
template <class charT, class traits, class Allocator>
 bool operator> (const basic_string&, const charT*);
template <class charT, class traits, class Allocator>
 bool operator<= (const basic_string&, const basic_string&);
template <class charT, class traits, class Allocator>
 bool operator<= (const charT*, const basic_string&);
template <class charT, class traits, class Allocator>
 bool operator<= (const basic_string&, const charT*);
template <class charT, class traits, class Allocator>
 bool operator>= (const basic_string&, const basic_string&);
template <class charT, class traits, class Allocator>
 bool operator>= (const charT*, const basic_string&);
template <class charT, class traits, class Allocator>
 bool operator>= (const basic_string&, const charT*);
template<class charT, class traits, class Allocator>
 istream& operator>> (istream&, basic_string&);
template <class charT, class traits, class Allocator>
 ostream& operator<< (ostream&, const basic_string&);
template <class Stream, class charT, 
          class traits, class Allocator>
 Stream& getline (Stream&, basic_string&, charT);

Constructors and Destructors

In all cases, the Allocator parameter will be used for storage management.

explicit 
basic_string (const Allocator& a = Allocator());
data() a non-null pointer that is copyable and can have 0 added to it
size() 0
capacity() an unspecified value
basic_string (const basic_string<T, traits, Allocator>& str);
basic_string (const basic_string &str, size_type pos,
              size_type n= npos);
data() points at the first element of an allocated copy of rlen elements of the string controlled by str beginning at position pos
size() rlen
capacity() a value at least as large as size()
get_allocator() str.get_allocator()


basic_string (const charT* s, size_type n,
              const Allocator& a = Allocator());
data() points at the first element of an allocated copy of the array whose first element is pointed at by s
size() n
capacity() a value at least as large as size()


basic_string (const charT * s, 
              const Allocator& a = Allocator());
data() points at the first element of an allocated copy of the array whose first element is pointed at by s
size() traits::length(s)
capacity() a value at least as large as size()
basic_string (size_type n, charT c, 
              const Allocator& a  = Allocator());
data() points at the first element of an allocated array of n elements, each storing the initial value c
size() n
capacity() a value at least as large as size()
template <class InputIterator>
basic_string  (InputIterator first, InputIterator last,
               const Allocator& a = Allocator());
data() points at the first element of an allocated copy of the elements in the range [first,last)
size() distance between first and last
capacity() a value at least as large as size()
~basic_string ();

Operators

basic_string&
operator= (const basic_string& str);
data() points at the first element of an allocated copy of the array whose first element is pointed at by str.size()
size() str.size()
capacity() a value at least as large as size()
basic_string& 
operator= (const charT * s);
basic_string& 
operator= (charT c);
charT 
operator[] (size_type pos) const;
reference 
operator[] (size_type pos);
basic_string& 
operator+= (const basic_string& s);
basic_string& 
operator+= (const charT* s);
basic_string& 
operator+= (charT c);

Iterators

iterator begin ();
const_iterator begin () const;
iterator end ();
const_iterator end () const;
reverse_iterator rbegin ();
const_reverse_iterator rbegin () const;
reverse_iterator rend ();
const_reverse_iterator rend () const;

Allocator

const allocator_type get_allocator () const;

Member Functions

basic_string& 
append (const basic_string& s, size_type pos, size_type npos);
basic_string&
append (const basic_string& s);
basic_string& 
append (const charT* s, size_type n);
basic_string& 
append (const charT* s);
basic_string& 
append (size_type n, charT c );
template<class InputIterator>
basic_string&  
append (InputIterator first, InputIterator last);
basic_string&
assign (const basic_string& s);
basic_string& 
assign (const  basic_string& s,
       size_type pos, size_type n);
basic_string& 
assign (const charT* s, size_type n);
basic_string&
assign (const charT* s);
basic_string&
assign (size_type n, charT c );
template<class InputIterator>
basic_string&  
assign (InputIterator first, InputIterator last);
const_reference 
at (size_type pos) const;
reference 
at (size_type pos);
size_type 
capacity () const;
int 
compare (const basic_string& str);
int
compare (size_type pos1, size_type n1, 
         const basic_string& str) const;
int
compare (size_type pos1, size_type n1, const basic_string& str,
         size_type pos2, size_type n2) const;
int 
compare (charT* s) const;
int
compare (size_type pos, size_type n1, charT* s) const;
int
compare (size_type pos, size_type n1, charT* s, 
         size_type n2) const;
size_type 
copy (charT* s, size_type n,  size_type pos = 0) const;
const charT* 
c_str () const;
const charT* 
data () const;
bool empty () const;
basic_string& 
erase (size_type pos = 0, size_type n = npos);
iterator 
erase (iterator p);
iterator 
erase (iterator first, iterator last);
size_type 
find (const basic_string& str, size_type pos = 0) const;
size_type
find (const charT* s, size_type pos, size_type n) const;
size_type 
find (const charT* s, size_type pos = 0) const;
size_type 
find (charT c, size_type pos = 0) const;
size_type
find_first_not_of (const basic_string& str,
                   size_type pos = 0) const;
size_type 
find_first_not_of  (const charT* s, 
                    size_type pos, size_type n) const;
size_type 
find_first_not_of (const charT* s, 
                   size_type pos = 0) const;
size_type 
find_first_not_of (charT c, size_type pos = 0) const;
size_type
find_first_of (const basic_string& str,
               size_type pos = 0) const;
size_type 
find_first_of (const charT*  s,  size_type  pos,
               size_type n) const;
size_type 
find_first_of (const charT* s, size_type pos = 0) const;
size_type
find_first_of  (charT c, size_type pos = 0) const;
size_type 
find_last_not_of (const basic_string& str,
                  size_type pos = npos) const;
size_type 
find_last_not_of (const charT* s,
                  size_type pos, size_type n) const;
size_type 
find_last_not_of (const charT* s, size_type pos = npos) const;
size_type 
find_last_not_of (charT c, size_type pos = npos) const;
size_type 
find_last_of (const basic_string& str,
              size_type pos = npos) const; 
size_type
find_last_of (const charT* s, size_type pos,
              size_type n) const;
size_type 
find_last_of (const charT* s, size_type pos = npos) const;
size_type 
find_last_of (charT c, size_type pos = npos) const;
basic_string&
insert (size_type pos1, const basic_string& s);
basic_string& 
insert (size_type pos, const  basic_string& s,
        size_type pos2 = 0, size_type n = npos);
basic_string&
insert (size_type pos, const charT* s, size_type n);
basic_string&
insert (size_type pos, const charT* s);
basic_string& 
insert (size_type pos, size_type n, charT c);
iterator 
insert (iterator p, charT c = charT());
void 
insert (iterator p, size_type n, charT c);
template<class InputIterator>
void
insert (iterator p, InputIterator first, InputIterator last);
size_type 
length () const;
size_type 
max_size () const;
size_type 
rfind (const basic_string& str, size_type pos  = npos) const;
size_type 
rfind (const charT* s, size_type pos,  size_type n) const;
size_type 
rfind (const charT* s, size_type pos = npos) const;
size_type 
rfind (charT c, size_type pos = npos) const;
basic_string&
replace (size_type pos, size_type n1, const basic_string& s);
basic_string& 
replace (size_type pos1, size_type n1, const basic_string& str,
         size_type pos2, size_type n2);
basic_string& 
replace (size_type pos, size_type n1, const charT* s,
         size_type n2);
basic_string& 
replace (size_type pos, size_type n1, const charT* s);
basic_string& 
replace (size_type pos, size_type n1, size_type n2, charT c);
basic_string& 
replace (iterator i1, iterator i2, 
         const basic_string& str);
basic_string& 
replace (iterator i1, iterator i2, const charT* s,
         size_type n);
basic_string& 
replace (iterator i1, iterator i2, const charT* s);
basic_string&  
replace (iterator i1, iterator i2, size_type n,
         charT c);
template<class InputIterator>
basic_string&  
replace (iterator  i1,  iterator  i2,
         InputIterator j1, InputIterator j2);
void reserve (size_type res_arg);
void
resize (size_type n, charT c);
void
resize (size_type n);
size type
size () const;
basic_string 
substr (size_type pos = 0, size_type n = npos) const;
void 
swap (basic_string& s);

Non-member Operators

template<class charT, class traits, class Allocator>
basic_string  
operator+ (const basic_string&  lhs, const basic_string& rhs);
template<class charT, class traits, class Allocator>
basic_string
operator+ (const charT* lhs, const basic_string& rhs);
template<class charT, class traits, class Allocator>
basic_string
operator+ (charT lhs, const basic_string& rhs);
template<class charT, class traits, class Allocator>
basic_string  
operator+ (const basic_string&  lhs, const charT* rhs);
template<class charT, class traits, class Allocator>
basic_string 
operator+ (const basic_string& lhs, charT rhs);
template<class charT, class traits, class Allocator>
bool  
operator== (const basic_string& lhs, const basic_string& rhs);
template<class charT, class traits, class Allocator>
bool   
operator== (const charT* lhs, const basic_string& rhs);
template<class charT, class traits, class Allocator>
bool  
operator== (const basic_string& lhs, const charT* rhs);
template<class charT, class traits, class Allocator>
bool  
operator!=  (const basic_string& lhs,
             const basic_string& rhs);
template<class charT, class traits, class Allocator>
bool 
operator!= (const charT* lhs, const basic_string& rhs);
template<class charT, class traits, class Allocator>
bool  
operator!= (const basic_string& lhs, const charT* rhs);
template<class charT, class traits, class Allocator>
bool  
operator< (const basic_string& lhs, const basic_string& rhs);
template<class charT, class traits, class Allocator>
bool 
operator< (const charT* lhs, const basic_string& rhs);
template<class charT, class traits, class Allocator>
bool  
operator< (const basic_string& lhs, const charT* rhs);
template<class charT, class traits, class Allocator>
bool  
operator> (const basic_string& lhs, const basic_string& rhs);
template<class charT, class traits, class Allocator>
bool 
operator> (const charT* lhs, const basic_string& rhs);
template<class charT, class traits, class Allocator>
bool 
operator> (const basic_string& lhs, const charT* rhs);
template<class charT, class traits, class Allocator>
bool 
operator<= (const basic_string& lhs,
             const basic_string& rhs);
template<class charT, class traits, class Allocator>
bool   
operator<= (const charT* lhs, const basic_string& rhs);
template<class charT, class traits, class Allocator>
bool  
operator<= (const basic_string& lhs, const charT* rhs);
template<class charT, class traits, class Allocator>
bool  
operator>= (const basic_string& lhs, const basic_string& rhs);
template<class charT, class traits, class Allocator>
bool 
operator>= (const charT* lhs, const basic_string& rhs);
template<class charT, class traits, class Allocator>
bool 
operator>= (const basic_string& lhs, const charT* rhs);
template<class charT, class traits, class Allocator>
istream&
operator>> (istream& is, basic_string& str);
template<class charT, class traits, class Allocator>
ostream&
operator<< (ostream& os, const basic_string& str);

Non-member Function

template <class Stream, class charT, class traits,
          class Allocator>
Stream& 
getline (Stream& is, basic_string& str, charT delim);

Example

//
// string.cpp
//
 #include<string>
 #include <iostream.h>
 int main()
 {
   string test;
   //Type in a string over five characters long
   while(test.empty() ||  test.size() <= 5)
   {
     cout << "Type a string between 5 and 100 characters long. "
          << endl;
     cin >> test;
   }
   //Test operator[] access
   cout << "Changing the third character from " << test[2] << 
           " to * " << endl;
   test[2] = '*';
   cout << "now its: " << test << endl << endl;
   //Try the insertion member function
   cout << "Identifying the middle: ";
   test.insert(test.size() / 2, "(the middle is here!)");
   cout << test << endl << endl;
   //Try replacement
   cout << "I didn't like the word 'middle',so instead,I'll say:"            << endl;
   test.replace(test.find("middle",0), 6, "center");
   cout << test << endl; 
   return 0;
 }
Output :
Type a string between 5 and 100 characters long.
roguewave
Changing the third character from g to *
now its: ro*uewave
Identifying the middle: ro*u(the middle is here!)ewave
I didn't like the word 'middle', so instead, I'll say:
ro*u(the center is here!)ewave

See Also

allocator


Top of document