United States    
COMPAQ STORE | PRODUCTS |
SERVICES | SUPPORT | CONTACT US | SEARCH
cxxtitle.gif (12116 bytes)
Compaq C++

Compaq C++
Using Compaq C++ for Tru64 UNIX Systems


Previous Contents Index

6.5.1.5 Operations

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,
size_type pos = npos);

Returns the first position in the string that matches one of the characters contained in the argument str. For example:


 string s = "abcdef"; 
 size_t index = s.find_first_of("cehb"); // index of 'b' is 2  

If 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,
size_type pos = npos);

Returns the last position in the string that matches one of the characters contained in the argument str. For example:


 string s = "abcdef"; 
 size_t index = s.find_last_of("cehb"); // index of 'e' is 4  

If 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 2 

basic_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.

6.5.1.6 Iterators

iterator begin();
const_iterator begin() const;

iterator rbegin();
const_iterator rbegin() const;

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 -- .

iterator end();
const_iterator end() const;

iterator rend();
const_iterator rend() const;

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++.

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++; 

6.5.2 The basic_string Nonmember Functions

operator+

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); 

Relational Operators

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 swap Function

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); 

Inserters and Extractors

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'); 

6.5.3 The basic_string typedefs

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.

6.5.4 Upgrading from Nonstandard Compaq C++ String Package Code

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.

6.5.5 Differences Between the basic_string Library and the Guide string Class

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
  

1.800.AT.COMPAQ

privacy and legal statement