The <string> Specialized Template Functions
swap() example
The <string> Functions
getline() example
The <string> Template Classes
basic_string Template Class
basic_string Template Class Typedef
basic_string Template Class Member Functions
basic_string Template Class Operators
basic_string Program Examples
append() example
|
| My Training Period: xx hours
25.5 <string> Specialized Template Functions
// swap() #include <string> #include <iostream> using namespace std;
int main() { // declaring an object of type basic_string<char> string str1("StringOne"); string str2("StringTwo"); cout<<"Before swapping string str1 and str2:"<<endl; cout<<"str1 string is = "<<str1<<endl; cout<<"str2 string is = "<<str2<<endl; swap(str1, str2); cout<<"\nOperation: swap(str1, str2)"<<endl; cout<<"After swapping string str1 and str2:"<<endl; cout<<"str1 string is = "<<str1<<endl; cout<<"str2 string is = "<<str2<<endl; return 0; }
Output:
| ||||||
Function | Description |
getline() | The getline() function creates a string containing all of the characters from the input stream until one of the following situations occurs:
|
Table 25.4: getline() function | |
A program example
// getline()
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str;
string str1;
string str2;
cout<<"Enter a line of text: ";
getline(cin, str);
cout<<"You entered: "<<str<<endl;
cout<<"Enter a line of text, <space> as the delimiter: "<<endl;
getline(cin, str1, ' ');
cout<<"You entered: "<<str1<<endl;
return 0;
}

With<string>, we are provided with two string template classes as shown in the following table.
Class | Description |
basic_string | A template class that describes objects that can store a sequence of arbitrary character-like objects. |
char_traits | A template class that describes attributes associated with a character of type CharType. |
Table 25.5: <string> classes | |
The sequences controlled by an object of template classbasic_string are the Standard C++ string class and are usually referred to as strings, but they should not be confused with the null-terminated C-strings used throughout the Standard C++ Library.
The string class is a container that enables the use of strings as normal types, such as using comparison and concatenation operations, iterators and STL algorithms. The basic_string template structure is shown below.
template < class CharType, class Traits = char_traits<CharType>, class Allocator = allocator<CharType> >
Where:
Entity | Description |
CharType | The data type of a single character to be stored in the string. The Standard C++ Library provides two specializations of this template class:
|
Traits | Various important properties of theCharType elements in abasic_string specialization are described by the class Traits. |
Allocator | The type that represents the stored allocator object that encapsulates details about the string's allocation and de-allocation of memory. The default value is allocator<Type>. |
Table 25.6: basic_string template parameters | |
The following table is the list of the typedef used in basic_string template class. Their usages are presented in the program examples section.
Typedef | Brief Description |
allocator_type | A type that represents the allocator class for a string object. The type is a synonym for the template parameter Allocator. |
const_iterator | A type that provides a random-access iterator that can access and read a const element in the string. A type const_iterator cannot be used to modify the value of a character and is used to iterate through a string in a forward direction. |
const_pointer | A type that provides a pointer to aconstelement in a string. The type is a synonym for allocator_type::const_pointer. For type string, it is equivalent to char*. Pointers that are declared const must be initialized when they are declared.const pointers always point to the same memory location and may point to constant or non constant data. |
const_reference | A type that provides a reference to a const element stored in a string for reading and performing const operations. A type const_reference cannot be used to modify the value of an element. The type is a synonym for allocator_type::const_reference. For string type, it is equivalent to const char&. |
const_reverse_iterator | A type that provides a random-access iterator that can read any constelement in the string. A typeconst_reverse_iterator cannot modify the value of a character and is used to iterate through a string in reverse. |
difference_type | A type that provides the difference between two iterators those refer to elements within the same string. The signed integer type describes an object that can represent the difference between the addresses of any two elements in the controlled sequence. For type string, it is equivalent to ptrdiff_t. |
iterator | A type that provides a random-access iterator that can read or modify any element in a string. A type iterator can be used to modify the value of a character and is used to iterate through a string in a forward direction. |
npos | An unsigned integral value initialized to –1 that indicates either "not found" or "all remaining characters" when a search function fails. When the return value is to be checked for the npos value, it might not work unless the return value is of typesize_type and not either int or unsigned. |
pointer | A type that provides a pointer to a character element in a string or character array. The type is a synonym for allocator_type::pointer. For type string, it is equivalent to char*. |
reference | A type that provides a reference to an element stored in a string. A typereference can be used to modify the value of an element. The type is a synonym for allocator_type::reference. For type string, it is equivalent to chr&. |
reverse_iterator | A type that provides a random-access iterator that can read or modify an element in a reversed string. A type reverse_iterator can be used to modify the value of a character and is used to iterate through a string in reverse. |
size_type | An unsigned integral type for the number of elements in a string. It is equivalent to allocator_type::size_type. For type string, it is equivalent tosize_t. |
traits_type | A type for the character traits of the elements stored in a string. The type is a synonym for the second template parameter Traits. For type string, it is equivalent to char_traits<char>. |
value_type | A type that represents the type of characters stored in a string. It is equivalent to traits_type::char_type and is equivalent to char for objects of type string. |
Table 25.7: basic_string typedefs | |
The following table is a list of member functions available inbasic_string template class.
Member Function | Brief Description |
append() | Adds characters to the end of a string. |
assign() | Assigns new character values to the contents of a string. |
at() | Returns a reference to the element at a specified location in the string. |
basic_string() | Constructs a string that is empty or initialized by specific characters or that is a copy of all or part of some other string object or C-string. |
begin() | Returns an iterator addressing the first element in the string. |
c_str() | Converts the contents of a string as a C-style, null-terminated, string. |
capacity() | Returns the largest number of elements that could be stored in a string without increasing the memory allocation of the string. |
clear() | Erases all elements of a string. |
compare() | Compares a string with a specified string to determine if the two strings are equal or if one is lexicographically less than the other. |
copy() | Copies at most a specified number of characters from an indexed position in a source string to a target character array. |
data() | Converts the contents of a string into an array of characters. |
empty() | Tests whether the string contains characters or not. |
end() | Returns an iterator that addresses the location succeeding the last element in a string. |
erase() | Removes an element or a range of elements in a string from specified positions. Notice the different with clear(). |
find() | Searches a string in a forward direction for the first occurrence of a substring that matches a specified sequence of characters. |
find_first_not_of() | Searches through a string for the first character that is not any element of a specified string. |
find_first_of() | Searches through a string for the first character that matches any element of a specified string. |
find_last_not_of() | Searches through a string for the last character that is not any element of a specified string. |
find_last_of() | Searches through a string for the last character that is an element of a specified string. |
get_allocator() | Returns a copy of the allocator object used to construct the string. |
insert() | Inserts an element or a number of elements or a range of elements into the string at a specified position. |
length() | Returns the current number of elements in a string. |
max_size() | Returns the maximum number of characters a string could contain. |
push_back() | Adds an element to the end of the string. |
rbegin() | Returns an iterator to the first element in a reversed string. |
rend() | Returns an iterator that point just beyond the last element in a reversed string. |
replace() | Replaces elements in a string at a specified position with specified characters or characters copied from other ranges or strings or C-strings. |
reserve() | Sets the capacity of the string to a number at least as great as a specified number. |
resize() | Specifies a new size for a string, appending or erasing elements as required. |
rfind() | Searches a string in a backward direction for the first occurrence of a substring that matches a specified sequence of characters. |
size() | Returns the current number of elements in a string. |
substr() | Copies a substring of at most some number of characters from a string beginning from a specified position. |
swap() | Exchange the contents of two strings. |
Table 25.8: basic_string member functions | |
The following table is a list of the operators available inbasic_string template class.
Operator | Brief Description |
operator+= | Appends characters to a string. |
operator= | Assigns new character values to the contents of a string. |
operator[ ] | Provides a reference to the character with a specified index in a string. |
Table 25.9: basic_string operator | |
Let try program examples demonstrating the basic_string template class member functions and other creatures readily available for us.
Characters may be appended to a string using the operator+= or the member functions append() orpush_back(). operator+= appends single-argument values while the multiple-argumentappend() member function allows a specific part of a string to be specified for adding.
// append()
#include <string>
#include <iostream>
using namespace std;
int main()
{
// appending a C-string to a string
string str1("Playing ");
const char *str2 = "with a string";
cout<<"str1 is: "<<str1<<endl;
cout<<"str2, C string is: "<<str2<<endl;
str1.append(str2);
cout<<"Operation: str1.append(str2)"<<endl;
cout<<"Appending str2 to str1: "<<str1<<endl;
// appending part of a C-string to a string
string str3 ("Replaying ");
const char *str4 = "the string ";
cout<<"\nstr3 string is: "<<str3<<endl;
cout<<"str4 C-string is: "<<str4<<endl;
str3.append(str4, 6);
cout<<"Operation: str3.append(str4, 6)"<<endl;
cout<<"Appending part of the str4 to string str3: \n"<<str3<<endl;
// appending part of one string to another
string str5("Again "), str6("string manipulation");
cout<<"\nstr5 is: "<<str5<<endl;
cout<<"str6 is: "<<str6<<endl;
str5.append(str6, 4, 6);
cout<<"Operation: str5.append(str6, 4, 6)"<<endl;
cout<<"The appended string is: "<<str5<<endl;
// appending one string to another in two ways, comparing append and operator [ ]
string str7("First "), str8("Second "), str9("Third ");
cout<<"\nstr7 is: "<<str7<<"\nstr8 is: "<<str8<<"\nstr9 is: "<<str9<<endl;
str7.append(str8);
cout<<"Operation: str7.append(str8)"<<endl;
cout<<"The appended string str7 is: "<<str7<<endl;
str7 += str9;
cout<<"Operation: str7 += str9"<<endl;
cout<<"The re appended string is: "<<str7<<endl;
// appending characters to a string
string str10("What string");
cout<<"\nstr10 string is: "<<str10<<endl;
str10.append(3, '?');
cout<<"Operation: str10.append(3, '?')"<<endl;
cout<<"str10 string appended with ? is: "<<str10<<endl;
// appending a range of one string to another
string str11("Finally "), str12("comes the END ");
cout<<"\nstr11 is: "<<str11<<" str12 is: "<<str12<<endl;
str11.append(str12.begin() + 6, str12.end() - 1);
cout<<"Operation:\nstr11.append(str12.begin() + 6, str12.end() - 1)"<<endl;
cout<<"The appended str11 String is: "<<str11<<endl;
return 0;
}

tenouk C++ STL string and character programming
The source code for this tutorial is available atC++ Characters & Strings source codes.
Acomplete C++ Standard Library documentation that includes STL.
Check thebest selling C / C++, STL and UML books at Amazon.com.