United States |
![]() |
![]() |
|
Previous | Contents | Index |
const charT* c_str() const;
const charT* data() const;
Returns a pointer to the char* representation of the string.const allocator_type& get_allocator() const;
Returns a reference to the string's allocator object.size_type find(const basic_string& str, size_type pos = 0) const;
Returns the lowest position at which str is embedded in the string starting at position pos in the string; if no such position exists, the function returns npos.size_type find(const charT* s, size_type pos = 0) const;
Returns the lowest position at which s is embedded in the string starting at position pos in the string; if no such position exists, the funtion returns npos.size_type find(charT c, size_type pos = 0) const;
Returns the lowest position at which c is embedded in the string starting at position pos in the string; if no such position exists, the function returns npos.size_type find(const charT* s, size_type pos, size_type n) const;
Returns the lowest position at which the first n characters of s are embedded in the string starting at position pos in the string; if no such position exists, the function returns npos.The function rfind() is similar to the find() function except that it returns the highest position in the string.
size_type find_first_of(const basic_string<charT, traits, Allocator>& str,
Returns the first position in the string that matches one of the characters contained in the argument str. For example:
size_type pos = npos);
string s = "abcdef"; size_t index = s.find_first_of("cehb"); // index of 'b' is 2If the pos argument is supplied, no character in the string beyond position pos will be considered.
size_type find_first_of(const charT* s, size_type pos = npos);
Calls the find_first_of() function (described above) constructing the str argument from s.size_type find_first_of(const charT* s, size_type pos, size_type n);
Calls the find_first_of() function (described above) constructing the str argument from the first n characters of s.size_type find_first_of(charT c, size_type pos = npos);
Calls the find_first_of() function (described above) constructing the str argument from the character c.size_type find_last_of(const basic_string<charT, traits, Allocator>& str,
Returns the last position in the string that matches one of the characters contained in the argument str. For example:
size_type pos = npos);
string s = "abcdef"; size_t index = s.find_last_of("cehb"); // index of 'e' is 4If the pos argument is supplied, no character in the string beyond position pos will be considered.
size_type find_last_of(const charT* s, size_type pos = npos);
Calls the find_last_of() function (described above) constructing the str argument from s.size_type find_last_of(const charT* s, size_type pos, size_type n);
Calls the find_last_of() function (described above) constructing the str argument from the first n characters of s.size_type find_last_of(charT c, size_type pos = npos);
Calls the find_last_of() function (described above) constructing the str argument from the character c.The find_first_not_of() and find_last_not_of() functions are similar to the functions described above except that they return the first (or last) position of one of the characters not in the string. For example:
string s = "abcdef"; size_t index = s.find_last_not_of("cfehb"); // index of 'd' is 4 index = s.find_first_not_of("cda"); // index of 'b' is 2basic_string substr(size_type pos = 0, size_type n = npos) const;
Returns the substring from pos to n. If pos is greater than the current size of the string, this function throws an out-of-range exception.int compare(const basic_string& str);
Compares each corresponding character starting at the beginning of the string and str. The number of elements used for the comparison is the smaller of the size of str or the size of the string. If the character in the string is less than the corresponding character in str, a negative value is returned. If the character in the string is greater than the corresponding character in str, a positive value is returned. If all of the elements are equal a negative value is returned if the value of the size of the string is less than the size of str, 0 if they are equal, and a positive value of the size if the string is greater than the size of str.int compare(charT* s) const;
Calls the compare() function (described above) by constructing the string str out of s.int compare(size_type pos, size_type n1, charT* s, size_type n2 = npos);
Calls the compare() function (described above). The string to compare is created from the first n1 elements of the string beginning at position pos, and the argument str is created from the first n2 elements of s.int compare(size_type pos, size_type n1, const basic_string& str);
Calls the compare() function (described above). The string to compare is created from the first n1 elements of the string beginning at position pos and is compared to str.int compare(size_type pos, size_type n1, const basic_string& str, size_type pos2, size_type n2);
Calls the compare() function (described above). The string to compare is created from the first n1 elements of the string beginning at position pos and the argument str is created from the first n2 elements of str beginning at position pos2.
iterator begin();
const_iterator begin() const;iterator rbegin();
Returns a random access iterator. Dereferencing the return value will give the character at the beginning of the string. The iterator returned by the begin() function will move forward through the string with operator++, while the rbegin() will move forward through the string with operator -- .
const_iterator rbegin() const;iterator end();
const_iterator end() const;iterator rend();
Returns a random access iterator to one-past the end of the string. Dereferencing the return value minus 1 will give the character at the end of the string. The iterator returned by the end() function will move backward through the string with operator -- , and the rend() function will move backward through the string with operator++.
const_iterator rend() const;The following is an example of a simple use of iterators:
#include <string> string str("azbzc"); int count = 0; // count the number of z's for (string::iterator iter = str.begin(); iter!= str.end(); iter++) if ((*(iter)=='z') count++;
The following operator+ functions return the string constructed from adding the character or characters in lhs to the character or characters in rhs:
template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator+( const basic_string<charT, traits, Allocator>& lhs, const basic_string<charT, traits, Allocator>& rhs); template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator+(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs); template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator+( const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator+(const charT lhs, const basic_string<charT, traits, Allocator>& rhs); template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator+( const basic_string<charT, traits, Allocator>& lhs, const charT rhs); |
The following operator:=,= functions return a value of TRUE if lhs :=,= rhs, where lhs and rhs are converted into strings if necessary; otherwise, the following return a value of FALSE:
template <class charT, class traits, class Allocator> bool operator==( const basic_string<charT, traits, Allocator>& lhs, const basic_string<charT, traits, Allocator>& rhs); template <class charT, class traits, class Allocator> bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs); template <class charT, class traits, class Allocator> bool operator==( const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); |
The following operator!= functions return a value of TRUE if lhs != rhs, where lhs and rhs are converted into strings if necessary; otherwise, the following return a value of FALSE:
template <class charT, class traits, class Allocator> bool operator!=( const basic_string<charT, traits, Allocator>& lhs, const basic_string<charT, traits, Allocator>& rhs); template <class charT, class traits, class Allocator> bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs); template <class charT, class traits, class Allocator> bool operator!=( const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); |
The following operator< functions return a value of TRUE if lhs < rhs, where lhs and rhs are converted into strings if necessary; otherwise, the following return a value of FALSE:
template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator<( const basic_string<charT, traits, Allocator>& lhs, const basic_string<charT, traits, Allocator>& rhs); template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator<(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs); template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator<( const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); |
The following operator> functions return a value of TRUE if lhs > rhs, where lhs and rhs are converted into strings if necessary; otherwise, the following return a value of FALSE:
template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator>( const basic_string<charT, traits, Allocator>& lhs, const basic_string<charT, traits, Allocator>& rhs); template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator>(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs); template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator>( const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); |
The following operator<= functions return a value of TRUE if lhs <= rhs, where lhs and rhs are converted into strings if necessary; otherwise, the following return a value of FALSE:
template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator<=( const basic_string<charT, traits, Allocator>& lhs, const basic_string<charT, traits, Allocator>& rhs); template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs); template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator<=( const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); |
The following operator>= functions return a value of TRUE if lhs >= rhs, where lhs and rhs are converted into strings if necessary; otherwise, the following return a value of FALSE:
template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator>=( const basic_string<charT, traits, Allocator>& lhs, const basic_string<charT, traits, Allocator>& rhs); template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs); template <class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator>=( const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); |
The following swaps the contents (that is, the data members) of lhs and rhs:
template <class charT, class traits, class Allocator> void swap(basic_string<charT, traits, Allocator>& lhs, basic_string<charT, traits, Allocator>& rhs); |
The following extracts characters from stream is into str until either end-of-file occurs on is or whitespace is encountered in is; any leading whitespace is skipped and str is erased before the extraction:
template <class charT, class traits, class Allocator> istream& operator>>(istream& is, basic_string<charT,traits,Allocator>& str); |
The following writes str into the output stream os:
template <class charT, class traits, class Allocator> ostream& operator<<(ostream& os, const basic_string<charT,traits,Allocator> & str); |
The following extracts up until the delim (or end-of-file) into the is; it does not skip leading whitespace and str is erased before the extraction:
template <class charT, class traits, class Allocator> istream& getline(istream& is, basic_string<charT,traits,Allocator>& str, charT delim = '\n'); |
The following typedefs are provided in the basic_string class:
traits
Is the type used to represent the character traits class used in the string. The traits class provides character primitives such as assignment, comparison, find(), length(), copy(), move(), and assign().value_type
Is the type of the charT elements in the string, for example char or wchar_t.allocator_type
Is the type of the allocator. The default is allocator<void>.size_type
Is the type used to represent the index into the string. The default is size_t.difference_type
Is the type used to represent the result of arithmetic operations on the iterators. The default is ptrdiff_t.reference
const_reference
Is the type used to represent a reference (or const reference) of value_type.pointer
const_pointer
Is the type used to represent a pointer (or const pointer) to value_type.iterator
const_iterator
Is the type used to iterate over elements of the string.reverse_iterator
const_reverse_iterator
Is the type used to iterate over elements of the string. This typedef differs from iterator in that ++ will move backward through the string and -- will move forward through the string.
The Compaq C++ basic_string Library is designed to replace the nonstandard Compaq C++ String Package.
The following list guides you through upgrading nonstandard code to use the new basic_string Library.
string s("abc"); char* cp = s; // not allowed const char* cp = s.data(); // ok |
template <class charT, class traits, class Allocator> inline basic_string<charT, traits, Allocator> upper(const basic_string<charT,traits, Allocator>& str) { basic_string<charT, traits, Allocator> newstr(str); for (size_t index = 0; index < str.length(); index++) if (islower(str[index])) newstr[index] = toupper(str[index]); return newstr; } template <class charT, class traits, class Allocator> inline basic_string<charT, traits, Allocator> lower(const basic_string<charT,traits, Allocator>& str) { basic_string<charT, traits, Allocator> newstr(str); for (size_t index = 0; index < str.length(); index++) if (isupper(str[index])) newstr[index] = tolower(str[index]); return newstr; } |
s2 = s1.upper(); // does not compile s2 = upper(s1); // ok |
string s1("abcdef"); string s2("abcdgf"); assert(s1.match(s2)==4); // does not compile pair<string::iterator,string::iterator> p(0,0); // ok p=mismatch(s1.begin(),s1.end(),s2.begin()); assert(p.first-s1.begin()==4); string s3 = s1; p=mismatch(s1.begin(),s1.end(),s3.begin()); assert(p.first == s1.end()); // everything matched |
string s("abcde"); string s2 = s1(1,3); // does not compile string s2 = s1.substr(1,3); // ok |
This section describes differences between the Compaq C++ basic_string Library and the string class described in the STL Tutorial and Reference Guide (the Guide string class). The Guide string class was included with the original STL implementation provided by the Hewlett-Packard Company.
The major difference between the basic_string Library and the Guide string class is that the basic_string Library can be treated directly as an STL container. The Guide string class can be used as an STL container only if you explicitly apply the conversion operator to vector<char>. For example:
// string class in the Guide #include <bstring.h> #include <vector.h> main() { vector<char> v = string("Hello"); // call an STL algorithm sort(v.begin(),v.end()); for (vector<char>::iterator iter = v.begin(); iter!= v.end(); iter++) cout << (*iter) ; cout << endl; } // equivalent code using the basic_string Library #include <string> main() { string s("Hello"); // call an STL algorithm sort(s.begin(),s.end()); for (string::iterator iter = s.begin(); iter!= s.end(); iter++) cout << (*iter) ; cout << endl; } |
A minor difference is that the basic_string Library uses the header file <string> not <bstring.h>. Also, the header file <bstring.h> automatically included <vector>, whereas <string> does not.
The basic_string Library is also fully functional; that is, it supports the functions size(), at(), and swap(), plus those member functions that take STL iterators as arguments.
Previous | Next | Contents | Index |
|