find_last_not_of() example
find_last_of() example
get_allocator() example
insert() example
push_back() example
rbegin() and rend() example
replace() example
reserve() example
|
| My Training Period: xx hours
This is a continuation from the previousC++ STL String & Character manipulation. Program examples in this Module compiled usingVisual C++ 6.0 with SP6 and Visual C++ .Net. Some program examples may generate warning and runtime errors caused by buffer/stack overflow. The good compilers have some protection for errors :o). g++ (run on Fedora 3 machine) examples, given at the end of this Module. The source code for this tutorial is available atC++ Characters & Strings source codes.
The C++ string programming ability that should be covered: Able to understand and use string template classes of the <string> in manipulating character and string in C++.
26.1 Continuation from previous Moduleā¦
find_last_not_of()
// find_last_not_of() part I #include <string> #include <iostream> using namespace std;
int main() { // searching for a single character in a string string str1("daddy donkey is dead"); cout<<"str1 string is: "<<str1<<endl; basic_string <char>::size_type index1, index2; static const basic_string <char>::size_type npos = -1; index1 = str1.find_last_not_of('d', 2); cout<<"Operation: str1.find_last_not_of('d', 2)"<<endl; if(index1 != npos) cout<<"The index of the last non 'd'\nfound before the 2nd position in str1 is: "<<unsigned int(index1)<< endl; else cout<<"The non 'd' character was not found."<<endl; index2 = str1.find_last_not_of('d'); cout<<"\nOperation: str1.find_last_not_of('d')"<<endl; if(index2 != npos) cout<<"The index of the non 'd' found in str1 is: <<unsigned int(index2)<<endl; else cout<<"The Character 'non d' was not found in str1."<<endl; cout<<endl; // ---------------------------------------------------------------- // searching a string for a substring as specified by a C-string string str2("Testing Testing Testing"); cout<<"str2 string is: "<<str2<<"\n"; basic_string <char>::size_type index3, index4; const char *cstr = "ei"; index3 = str2.find_last_not_of(cstr, 12); cout<<"Operation: str2.find_last_not_of(cstr, 12)"<<endl; if(index3 != npos) cout<<"The index of the last occurrence of an element not\nof 'ei' in str2 before the 12th " <<"position is: "<<unsigned int(index3)<<endl; else cout<<"Elements not of the substring 'ei' were not \n found in str2 before the 12th position."<<endl; const char *cstr1 = "g t"; index4 = str2.find_last_not_of(cstr1); cout<<"\nOperation: str2.find_last_not_of(cstr1)"<<endl; if(index4 != npos) cout<<"The index of the last element not in 'g t'\nis: "<<unsigned int(index4)<<endl; else cout<<"The elements of the substring 'g t' were not found in str2"<<endl; return 0; }
|

// find_last_not_of() part II
#include <string>
#include <iostream>
using namespace std;
int main()
{
// searching a string for a substring as specified by a C-string
string str3("Playing Testing Boring");
cout<<"str3 string is: "<<str3<<"\n";
basic_string <char>::size_type index5, index6;
static const basic_string <char>::size_type npos = -1;
const char *cstr2 = "PTB";
index5 = str3.find_last_not_of(cstr2);
cout<<"Operation: str3.find_last_not_of(cstr2)"<<endl;
if(index5 != npos)
cout<<"The index of the last occurrence of an element in str3\nother than one of the "
<<"characters in 'PTB' is: "<<unsigned int(index5)<<endl;
else
cout<<"Elements in str3 contain only characters in the string 'PTB'"<<endl;
const char *cstr3 = "gTB";
index6 = str3.find_last_not_of(cstr3, 6, index5-1);
cout<<"\nOperation: str3.find_last_not_of(cstr3, 6, index5-1)"<<endl;
if(index6 != npos)
cout<<"The index of the occurrence of an element\nnot in 'gTB' in str3 is: "<<unsigned int(index6)<<endl;
else
cout<<"Elements in str3 contains only characters in the string 'gTB'."<<endl;
cout<<endl;
// -----------------------------------------------------------
// searching a string for a substring as specified by a string
string str4("Testing 123 Testing 123");
cout<<"str4 string is: "<<str4<<"\n";
basic_string <char>::size_type index7, index8;
string str5("3 1");
index7 = str4.find_last_not_of(str5, 18);
cout<<"Operation: str4.find_last_not_of(str5, 18)"<<endl;
if(index7 != npos)
cout<<"The index of the last occurrence of an element not\nin '3 1' in str4 before the 18th "
<<"position is: "<<unsigned int(index7)<<endl;
else
cout<<"Elements other than those in the substring '3 1' were not found in the string str4"<<endl;
string str6("Testing");
index8 = str4.find_last_not_of(str6);
cout<<"\nOperation: str4.find_last_not_of(str6)"<<endl;
if(index8 != npos)
cout<<"The index of the last occurrence of an element not in\n'Testing' in str4 before the end "
<<"position is: "<<unsigned int(index8)<<endl;
else
cout<<"Elements other than those in the substring\n'Testing' were not found in the string str4"<<endl;
return 0;
}

The return value is the index of the last character of the substring searched for when successful; otherwise npos.
// find_last_of() part I
#include <string>
#include <iostream>
using namespace std;
int main()
{
// searching for a single character in a string
string str1("Testing 1234 Testing 1234");
cout<<"str1 string is: "<<str1<<endl;
basic_string <char>::size_type index1, index2;
static const basic_string <char>::size_type npos = -1;
index1 = str1.find_last_of('g', 24);
cout<<"Operation: str1.find_last_of('g', 24)"<<endl;
if(index1 != npos)
cout<<"The index of the last 'g' found before\nthe 24th position in str1 is: "<<unsigned int(index1)<<endl;
else
cout<<"The character 'g' was not found in str1"<<endl;
index2 = str1.find_first_of('z');
cout<<"\nOperation: index2 = str1.find_first_of('z')"<<endl;
if(index2 != npos)
cout<<"The index of the 'z' found in str1 is: "<<unsigned int(index2)<<endl;
else
cout<<"The character 'z' was not found in str1"<<endl;
cout<<endl;
// --------------------------------------------------
// searching a string for a substring as specified by a C-string
string str2("Testing 1234 Testing 1234");
cout<<"str2 string is: "<<str2<<endl;
basic_string <char>::size_type index3, index4;
const char *cstr = "t1";
index3 = str2.find_last_of(cstr, 25);
cout<<"Operation: str2.find_last_of(cstr, 25)"<<endl;
if(index3 != npos)
cout<<"The index of the last occurrence of an element\nof 't1' in str2 before the 25th "
<<"position is: "<<unsigned int(index3)<<endl;
else
cout<<"Elements of the substring 't1' were not\nfound in str2 before the 25th position."<<endl;
const char *cstr1 = "g3";
index4 = str2.find_last_of(cstr1);
cout<<"\nOperation: str2.find_last_of(cstr1)"<<endl;
if(index4 != npos)
cout<<"The index of the last element of 'g3'\nafter the 0th position in str2 is: "
<<unsigned int(index4)<<endl;
else
cout<<"The substring 'g3' was not found in str2."<<endl;
return 0;
}

// find_last_of() part II
#include <string>
#include <iostream>
using namespace std;
int main()
{
// searching a string for a substring as specified by a C-string
string str3("Testing 1234 Testing 1234");
cout<<"str3 string is: "<<str3<<endl;
basic_string <char>::size_type index5;
static const basic_string <char>::size_type npos = -1;
const char *cstr2 = "s1";
index5 = str3.find_last_of(cstr2, 20, 20);
cout<<"Operation: str3.find_last_of(cstr2, 20, 20)"<<endl;
if(index5 != npos)
cout<<"The index of the last occurrence of an element\nof 's1' in str3 before the 20th "
<<"position is: "<<unsigned int(index5)<<endl;
else
cout<<"Elements of the substring 's1' were not\nfound in str3 before the 20th position."<<endl;
cout<<endl;
// -------------------------------------------------------
// searching a string for a substring as specified by a string
string str4("Testing 1234 Testing 1234");
cout<<"str4 string is: "<<str4<<endl;
basic_string <char>::size_type index6, index7;
string str5("416");
index6 = str4.find_last_of(str5, 25);
cout<<"Operation: str4.find_last_of(str5, 25)"<<endl;
if(index6 != npos)
cout<<"The index of the last occurrence of an element\nof '416' in str4 before the 25th "
<<"position is: "<<unsigned int(index6)<<endl;
else
cout<<"Elements of the substring '416' were not\nfound in str4 after the 0th position"<<endl;
string str6("1g");
index7 = str4.find_last_of(str6);
cout<<"\nOperation: str4.find_last_of(str6)"<<endl;
if(index7 != npos)
cout<<"The index of the last occurrence of an element\nof '1g' in str4 before the 0th "
<<"position is: "<<unsigned int(index7)<<endl;
else
cout<<"Elements of the substring '1g' were not\nfound in str4 after the 0th position"<< endl;
return 0;
}

This member function returns the stored allocator object.
Allocators for the string class specify how the class manages storage. The default allocators supplied with container classes are sufficient for most programming needs. Writing and using your own allocator class is an advanced C++ topic and its usage is very specific.
The return value is the allocator used by the string.
// get_allocator()
#include <string>
#include <iostream>
using namespace std;
int main()
{
// using the default allocator.
string str1;
basic_string <char> str2;
basic_string <char, char_traits<char>, allocator<char> > str3;
// str4 will use the same allocator class as str1
basic_string <char> str4(str1.get_allocator());
basic_string <char>::allocator_type xchar = str1.get_allocator();
// you can now call functions on the allocator class xchar used by str1
string str5(xchar);
return 0;
}
// no output
The return value is either a reference to the string object that is being assigned new characters by the member function or, in the case of individual character insertions, an iterator addressing the position of the character inserted, or none, depending on the particular member function.
// insert() part I
#include <string>
#include <iostream>
using namespace std;
int main()
{
// inserting a C-string at a given position
basic_string <char> str1("e insert() testing");
const char *cstr1 = "Th";
cout<<"str1 = "<<str1<<endl;
cout<<"cstr1 = "<<cstr1<<endl;
str1.insert(0, cstr1);
cout<<"Operation: str1.insert(0, cstr1)"<<endl;
cout<<"Inserting a C-string at position 0 is:\n"<<str1<<endl;
cout<<endl;
// inserting a C-string at a given position for a specified number of elements
basic_string <char> str2("Test");
const char *cstr2 = "ing an insert()";
cout<<"str2 = "<<str2<<endl;
cout<<"cstr2 = "<<cstr2<<endl;
str2.insert(4, cstr2, 15);
cout<<"Operation: str2.insert(4, cstr2, 15)"<<endl;
cout<<"Inserting a C-string at the end is:\n"<<str2<<endl;
cout<<endl;
// inserting a string at a given position
basic_string <char> str3(" the insert()");
string str4("Testing");
cout<<"str3 = "<<str3<<endl;
cout<<"str4 = "<<str4<<endl;
str3.insert(0, str4);
cout<<"Operation: str3.insert(0, str4)"<<endl;
cout<<"Inserting string at position 0 is:\n"<<str3<<endl;
cout<<endl;
// inserting part of a string at a given position
basic_string <char> str5("Testing ");
string str6(" the insert()");
cout<<"str5 = "<<str5<<endl;
cout<<"str6 = "<<str6<<endl;
str5.insert(7, str6, 4, 9);
cout<<"Operation: str5.insert(7, str6, 4, 9)"<<endl;
cout<<"Inserting part of a string at position 9 is:\n"<<str5<<endl;
return 0;
}
![]() |
// insert() part II
#include <string>
#include <iostream>
using namespace std;
int main()
{
// inserting a number of characters at a specified position in the string
string str7("Testing the insert()?");
cout<<"str7 = "<<str7<<endl;
str7.insert(20, 4, '!');
cout<<"Operation: str7.insert(20, 4, '!')"<<endl;
cout<<"Inserting characters: \n"<<str7<<endl;
cout<<endl;
// inserting a character at a specified position in the string
string str8("Tesing the insert()");
cout<<"str8 = "<<str8<<endl;
basic_string <char>::iterator StrIter = (str8.begin() + 3);
str8.insert(StrIter, 't');
cout<<"Operation: str8.insert(StrIter, 't')"<<endl;
cout<<"Inserting missing character: \n"<<str8<<endl;
cout<<endl;
// inserts a range at a specified position in the string
string str9("First part");
string str10("Second partition");
cout<<"str9 = "<<str9<<endl;
cout<<"str10 = "<<str10<<endl;
basic_string <char>::iterator Str9Iter = (str9.begin() + 5);
str9.insert(Str9Iter, str10.begin()+6, str10.end()-4);
cout<<"Operation: str9.insert(Str9Iter, str10.begin()+6,\nstr10.end()-4)"<<endl;
cout<<"Inserting a range of character: \n"<<str9<<endl;
cout<<endl;
// inserting a number of characters at a specified position in the string
string str11("Final insert() test");
cout<<"str11 = "<<str11<<endl;
basic_string <char>::iterator Str11Iter = (str11.begin() + 15);
str11.insert(Str11Iter, 5, 'a');
cout<<"Operation: str11.insert(Str11Iter, 5, 'a')"<<endl;
cout<<"A range of character inserted in the string: \n"<<str11<<endl;
return 0;
}

// push_back()
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str1("Testing the push_back()");
basic_string <char>::iterator StrIter, Str1Iter;
cout<<"str1 string is: ";
for(StrIter = str1.begin(); StrIter != str1.end(); StrIter++)
cout<<*StrIter;
cout<<endl;
cout<<"Move the pointer to the end of string..."<<endl;
Str1Iter = str1.end();
cout<<"Then add an element to the end of the string..."<<endl;
str1.push_back('T');
cout<<"\nOperation: str1.end() then str1.push_back('T')"<<endl;
cout<<"The last character of str1 string is: "
<<*Str1Iter;
cout<<endl;
cout<<"\nMove the pointer from the beginning to the end..."<<endl;
cout<<"Now, str1 string is: ";
for(StrIter = str1.begin(); StrIter != str1.end(); StrIter++)
cout<<*StrIter;
cout<<endl;
return 0;
}

rbegin() is used with a reversed string just as begin is used with a string.
If the return value of rbegin() is assigned to a const_reverse_iterator, the string object cannot be modified. If the return value of rbegin() is assigned to a reverse_iterator, the string object can be modified.
rbegin() can be used to initialize an iteration through a string backwards.
The return value of the rbegin() is a random-access iterator to the first element in a reversed string, addressing what would be the last element in the corresponding un-reversed string.
rend() is used with a reversed string just as end is used with a string.
If the return value of rend() is assigned to aconst_reverse_iterator, the string object cannot be modified. If the return value of rend() is assigned to a reverse_iterator, the string object can be modified.
rend() can be used to test whether a reverse iterator has reached the end of its string.
The return value of the rend() is a reverse random-access iterator that addresses the location succeeding the last element in a reversed string. The value returned by rend() should not be dereferenced.
// rbegin() and rend()
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str1("The reverse begin, rbegin()"), str2;
basic_string <char>::reverse_iterator StrIter, Str1Iter;
basic_string <char>::const_reverse_iterator str1_rcIter;
// well, no need to minus the null character huh?
cout<<"Operation: str1.rbegin()"<<endl;
Str1Iter = str1.rbegin();
cout<<"The first character of the reversed str1 string is: "
<<*Str1Iter<<endl;
cout<<"The full reversed str1 string is:\n";
// rbegin() should be with rend()
for(StrIter = str1.rbegin(); StrIter != str1.rend(); StrIter++)
cout<<*StrIter;
cout<<"\n\n";
// the dereferenced iterator can be used to modify a character
cout<<"Operation: *Str1Iter = 'Z'"<<endl;
*Str1Iter = 'Z';
cout<<"The first character of the new str1 is: "
<<*Str1Iter<<endl;
cout<<"The full new reversed str1 is:\n";
for(StrIter = str1.rbegin(); StrIter != str1.rend(); StrIter++)
cout<<*StrIter;
cout<<"\n\n";
// the following line will generate error because iterator is const
// *str1_rcIter = 'T';
// for an empty string, rbegin() is equivalent to rend()
cout<<"Operation: str2.rbegin() == str2.rend()"<<endl;
if(str2.rbegin() == str2.rend())
cout<<"The string str2 is empty."<<endl;
else
cout<<"The stringstr2 is not empty."<<endl;
return 0;
}

The return value is the operand string with the replacement made.
// replace() part I
#include <string>
#include <iostream>
using namespace std;
int main()
{
// replacing part of the string with characters of a string or C-string
// remember that index start from 0!
string str1, str2;
string str3("TESTING");
string str4("ABC");
const char* cstr = "DEF";
cout<<"str3 string is: "<<str3<<endl;
cout<<"str4 string is: "<<str4<<endl;
cout<<"cstr C-string is: "<<cstr<<endl;
str1 = str3.replace(1, 3, str4);
cout<<"Operation: str3.replace(1, 3, str4)"<<endl;
cout<<"The new string is: "<<str1<<endl;
cout<<"\nOperation: str3.replace(5, 3, cstr)"<<endl;
str2 = str3.replace(5, 3, cstr);
cout<<"The new string is: "<<str2<<"\n\n";
// replacing part of the string with characters form part of a string or C-string
string str5, str6;
string str7 ("TESTING");
string str8 ("123");
const char* cstr1 = "456";
cout<<"str7 string is: "<<str7<<endl;
cout<<"str8 string is: "<<str8<<endl;
cout<<"cstr1 C-string is: "<<cstr1<<endl;
cout<<"Operation: str7.replace(1, 3, str8, 1, 2)"<<endl;
str5 = str7.replace(1, 3, str8, 1, 2);
cout<<"The new string is: "<<str5<<endl;
cout<<"\nOperation: str7.replace(4, 3, cstr1, 1)"<<endl;
str6 = str7.replace(4, 3, cstr1, 1);
cout<<"The new string is: "<<str6<<"\n\n";
// replacing part of the string with characters
string str9;
string str10 ("TESTING");
char cstr2 = 'R';
cout<<"str10 string is: "<<str10<<endl;
cout<<"cstr2 character is: "<<cstr2<<endl;
cout<<"Operation: str10.replace(2, 4, 5, cstr2)"<<endl;
str9 = str10.replace(2, 4, 5, cstr2);
cout<<"The new string is: "<<str9<<endl;
return 0;
}

// replace() part II
#include <string>
#include <iostream>
using namespace std;
int main()
{
// replacing part of the string, delineated with iterators, with a string or C-string
string str11, str12;
string str13("TESTING1");
string str14("123");
const char* cstr3 = "AAA";
cout<<"str13 string is: "<<str13<<endl;
cout<<"str14 string is: "<<str14<<endl;
cout<<"cstr3 C-string is: "<<cstr3<<endl;
basic_string<char>::iterator Iter1, Iter2;
cout<<"Operation: str13.begin()"<<endl;
cout<<"Operation: str13.begin() + 3"<<endl;
cout<<"Operation: str13.replace(Iter1, Iter2, str14)"<<endl;
Iter1 = str13.begin();
Iter2 = str13.begin() + 3;
str11 = str13.replace(Iter1, Iter2, str14);
cout<<"The new string is: "<<str11<<endl;
cout<<"Operation: str13.replace(Iter1, Iter2, cstr3)"<<endl;
str12 = str13.replace(Iter1, Iter2, cstr3);
cout<<"The new string is: "<<str12<<"\n\n";
// replacing part of the string delineated with iterators with a number of C-string characters
string str15;
string str16("TESTING2");
const char* cstr4 = "1234AA";
cout<<"str16 string is: "<<str16<<endl;
cout<<"cstr4 C-string is: "<<cstr4<<endl;
basic_string<char>::iterator Iter3, Iter4;
cout<<"Operation: str16.begin()"<<endl;
cout<<"Operation: str16.begin() + 4"<<endl;
cout<<"Operation: str16.replace(Iter3, Iter4, cstr4, 4)"<<endl;
Iter3 = str16.begin();
Iter4 = str16.begin() + 4;
str15 = str16.replace(Iter3, Iter4, cstr4, 4);
cout<<"The new string is: "<<str15<<"\n\n";
// replacing part of the string delineated with iterators with specified characters
string str17;
string str18("TESTING3");
char cstr5 = 'u';
cout<<"str18 string is: "<<str18<<endl;
cout<<"cstr5 character is: "<<cstr5<<endl;
basic_string<char>::iterator Iter5, Iter6;
Iter5 = str18.begin();
Iter6 = str18.begin() + 3;
str17 = str18.replace(Iter5, Iter6, 4, cstr5);
cout<<"The new string is: "<<str17<<"\n\n";
// replacing part of the operand string delineated with iterators
// with part of a parameter string delineated with iterators
string str19;
string str20("TESTING4");// operand
string str21("1234"); // parameter
cout<<"str20 string is: "<<str20<<endl;
cout<<"str21 string is: "<<str21<<endl;
basic_string<char>::iterator Iter7, Iter8, Iter9, Iter10;
cout<<"Operation: str20.begin() + 1"<<endl;
cout<<"Operation: str20.begin() + 3"<<endl;
cout<<"Operation: str21.begin()"<<endl;
cout<<"Operation: str21.begin() + 2"<<endl;
Iter7 = str20.begin() + 1;
Iter8 = str20.begin() + 3;
Iter9 = str21.begin();
Iter10 = str21.begin() + 2;
cout<<"Operation: str20.replace(Iter7, Iter8, Iter9, Iter10)"<<endl;
str19 = str20.replace(Iter7, Iter8, Iter9, Iter10);
cout<<"The new string is: "<<str19<<endl;
return 0;
}

Having sufficient capacity is important because reallocations is a time-consuming process and invalidates all references, pointers, and iterators that refer to characters in a string.
Unlike vector (another STL), the member function reserve() may be called to shrink the capacity of an object. The request is non binding and may or may not happen.
The default value for the parameter is zero, a call of reserve() is a non-binding request to shrink the capacity of the string to fit the number of characters currently in the string. The capacity is never reduced below the current number of characters.

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