|< C++ String Class 4 | Main | C++ String Class 6 >| Site Index | Download |


 

 

 

 

 

MODULE 26

THE C++ STRING CLASS CODE EXAMPLES 5

 

 

 

 

 

 

 

 

 

 

 

 

 

What do we have in this session?

  1. find_last_not_of() example

  2. find_last_of() example

  3. get_allocator() example

  4. insert() example

  5. push_back() example

  6. rbegin() and rend() example

  7. replace() example

  8. reserve() example

 

 

 

My Training Period: xx hours

 

This is a continuation from the previous C++ STL String & Character manipulation.  Program examples in this Module compiled using Visual 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 at C++ 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()

  • The return value is the index of the first character of the substring searched for when successful; otherwise npos.

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

}

 

Output:

 

C++ STL character and string find_last_not_of()

 

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

}

 

Output:

 

C++ STL character and string find_last_not_of()

 

 

find_last_of()

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

}

 

Output:

 

C++ STL character and string find_last_of()

 

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

}  

 

Output:

 

C++ STL character and string find_last_of()

 

 

get_allocator()

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

 

 

 

insert()

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

}

 

Output:

C++ STL character and string insert()  

 

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

}

 

Output:

 

C++ STL character and string insert()

 

push_back()

 

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

}

 

Output:

 

C++ STL character and string push_back()

 

 

 

rbegin() and rend()

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

}

 

Output:

 

C++ STL character and string rbegin() and rend()

 

 

replace()

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

}

 

Output:

 

C++ STL character and string replace()

 

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

}

 

Output:

 

C++ STL character and string replace()

 

 

 

reserve()

// reserve()

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

   string str1("Testing the reserve()");

   cout<<"str1 string is: "<<str1<<endl;

   basic_string <char>::size_type SizeStr1, Size1Str1;

   SizeStr1 = str1.size();

   basic_string <char>::size_type CapaStr1, Capa1Str1;

   CapaStr1 = str1.capacity();

   // compare size & capacity of the original string

   cout<<"The size of str1 string is: "<<SizeStr1<<endl;

   cout<<"The capacity of str1 string is: "<<CapaStr1<<"\n\n";

   // compare size & capacity of the string with added capacity

   cout<<"Operation: str1.reserve(20)"<<endl;

   str1.reserve(20);

   Size1Str1 = str1.size();

   Capa1Str1 = str1.capacity();

   cout<<"str1 with increased capacity is: "<<str1<<endl;

   cout<<"The size of str1 string is: "<<Size1Str1<<endl;

   cout<<"The increased capacity of str1 string is: "<<Capa1Str1<<"\n\n";

   // compare size & capacity of the string with downsized capacity. Without any parameter,

   // it should shrink to fit the number of the characters currently in the string

   cout<<"Operation: str1.reserve()"<<endl;

   str1.reserve();

   basic_string <char>::size_type Size2Str1;

   basic_string <char>::size_type Capa2Str1;

   Size2Str1 = str1.size();

   Capa2Str1 = str1.capacity();

   cout<<"str1 with downsized capacity is: "<<str1<<endl;

   cout<<"The size of str1 string is: "<<Size2Str1<<endl;

   cout<<"The reduced capacity of str1 string is: "<<Capa2Str1<<endl;

   return 0;

}

 

Output:

 

C++ STL character and string reserve()

 

tenouk C++ STL string class programming tutorial

 

 

 

 

 

 

 

 

 

 

 

 

 

Further C++ string related reading:

 

  1. The source code for this tutorial is available at C++ Characters & Strings source codes.

  2. Check the best selling C / C++ books at Amazon.com.

  3. A complete C & C++ Standard Library documentation that includes STL.

 

 

 

 

 

 

|< C++ String Class 4 | Main | C++ String Class 6 >| Site Index | Download |


C++ String Class:  Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7