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


 

 

 

 

 

MODULE 25a

C++ STRING CLASS PROGRAMMING 3

 

 

 

 

 

My Training Period: xx hours

 

This is a continuation from the previous Module, compiled using Visual C++ 6.0 with SP6 and Visual C++ .Net.  g++ (GNU C++ run on my Fedora 3 machine) example is given at the end of this Module.  The source code for this tutorial is available at C++ Characters & Strings source codes. Take note also for the codes that span more than one line, which they are not supposed to.

 

The C++ string and character programming abilities that should be acquired:

 

 

What do we have in this session?

  1. assign() example

  2. at() example

  3. The basic_string::basic_string

  4. basic_string<>

  5. begin() example

  6. c_str() example

  7. capacity() example

  8. clear() example

  9. compare() example

  10. copy() example

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

assign()

 

  • The strings can be assigned new character values. The new value can be either a string and C-string or a single character.

  • The operator= may be used if the new value can be described by a single parameter; otherwise the member function assign(), which has multiple parameters, can be used to specify which part of the string is to be assigned to a target string.

// assign(), string assignment

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

    // assigning the characters of a C-string to a string

    string str1;

    const char *str2 = "StRiNg assign()";

   

    cout<<"str2, C string is: "<<str2<<endl;

    str1.assign(str2);

    cout<<"Operation: str1.assign(str2)"<<endl;

    cout<<"Assigning the C-string str2 to str1 string: \n"<<str1<<endl;

   

    // assigning a number of a C-string characters to a string

    string  str3;

    const char *str4 = "Another StRiNg assign()";

    cout<<"\nstr4 C string is: "<<str4<<endl;

    str3.assign(str4, 11);

    cout<<"Operation: str3.assign(str4, 11)"<<endl;

    cout<<"Assigning some portion of the str4 to str3 string: \n"<<str3<<endl;

   

    // assigning a number of the characters from one string to another string

    string str5("First "), str6("Second sTrInG");

    cout<<"\nstr5 string is: "<<str5<<endl;

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

    str5.assign(str6, 7, 6);

    cout<<"Operation: str5.assign(str6, 7, 6)"<<endl;

    cout<<"Newly assigned str5 string is: "<<str5<<endl;

   

    // assigning the characters from one string to another string

    // in two equivalent ways, comparing the assign and operator =

    string str7("First"), str8("Second"), str9("Third");

    cout<<"\nstr7 string is: "<<str7<<endl;

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

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

    str7.assign(str8);

    cout<<"Operation: str7.assign(str8)"<<endl;

    cout<<"Newly assigned str7 with str8 string is: "<<str7<<endl;

    str7 = str9;

    cout<<"Operation: str7 = str9"<<endl;

    cout<<"String str7 reassigned with str9 string is: "<<str7<<endl;

   

    // assigning a specific number of characters of a certain value to a string

    string str10("Working STrInG");

    cout<<"\nstr10 string is: "<<str10<<endl;

    str10.assign(3, '!');

    cout<<"Operation: str10.assign(3, '!')"<<endl;

    cout<<"str10 string assigned with character '!' is: "<<str10<<endl;

   

    // assigning a value from a range of one string to another string

    string str11("Comes "), str12("the END ");

    cout<<"\nstr11 string is: "<<str11<<endl;

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

    str11.assign(str12.begin() + 4, str12.end() - 1);

    cout<<"Operation: str11.assign(str12.begin()+4, str12.end()-1)"<<endl;

    cout<<"str11 assigned a range of str12 string is: \n"<<str11<<endl;

    return 0;

}

 

Output:

 

C++ STL character and string manipulation assign()

 

 

at()

// at()

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

    string str1("Operation"), str2("Desert Storm");

    const string  constr1("Making cakes"), constr2("Start eating");

   

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

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

    cout<<"constr1, const string is: "<<constr1<<endl;

    cout<<"constr2, const string is: "<<constr2<<endl;

    // element access of the non const strings

    basic_string <char>::reference RefStr1 = str1[4];

    basic_string <char>::reference RefStr2 = str2.at(7);

    // index starts from 0 lor!

    cout<<"\n---str1[5]---"<<endl;

    cout<<"The 5th character of str1 is: "<<RefStr1<<endl;

    cout<<"---str2.at(7)---"<<endl;

    cout<<"The 7th character of str2 is: "<<RefStr2<<endl;

    // element access to the const strings

    basic_string <char>::const_reference cRefStr1 = constr1[constr1.length()];

    basic_string <char>::const_reference cRefStr2 = constr2.at(8);

    cout<<"---constr1.length()---"<<endl;

    cout<<"The length of the constr1 string is: "<<unsigned int(constr1.length())<<endl;

    cout<<"\nTesting the null character..."<<endl;

    cout<<"---cRefStr1 = constr1[constr1.length()]---"<<endl;

    cout<<"Operation: (cRefStr1 == \'\\0\')"<<endl;

    if(cRefStr1 == '\0')

        cout<<"The null character is returned."<<endl;

    else

        cout<<"The null character is not returned."<<endl;

    cout<<"\n---constr2.at(8)---"<<endl;

    cout<<"The 8th character of the constr2 is: "<<cRefStr2<<endl;

    return 0;

}

 

Output:

 

C++ STL character and string manipulation at()

 

 

basic_string::basic_string

 

  1. basic_string( const value_type* _Ptr, size_type _Count = npos, const allocator_type& _Al = Allocator() );
  2. basic_string( const basic_string& _Right, size_type _Roff = 0, size_type _Count = npos );
  3. basic_string( const basic_string& _Right,  size_type _Roff = 0,  size_type _Count = npos, const allocator_type& _Al = Allocator() );
  4. basic_string( size_type _Count, value_type _Ch, const allocator_type& _Al = Allocator() );
  5. explicit basic_string( const allocator_type& _Al = Allocator() );
  6. template <class InputIterator>basic_string( InputIterator _First, InputIterator _Last, const allocator_type& _Al = Allocator() );
  1. The first member function creates a string that is initialized by all or part of a C-string.

  2. The second member function creates a string that is initialized by all or part of an object of type basic_string.

  3. The third member function creates a string that is initialized by a specific number of characters of a parameter stipulated value.

  4. The fourth member function creates an empty string.

  5. The fifth member function creates a string that is initialized by the characters in the range whose boundaries are delimited by input iterators.

// basic_string

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

    // initializing with a C-string

    const char *str1 = "The basic_string";

    basic_string <char> str2(str1, 5);

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

    cout<<"Operation: str2(str1, 5)"<<endl;

    cout<<"str2 initialized by str1 is: "<<str2<<"\n\n";

    // initializing with a string

    string  str3("Initialize with a StRinG?");

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

    basic_string <char> str4(str3, 6, 10);

    cout<<"Operation: str4(str3, 6, 10)"<<endl;

    cout<<"str4 initialized by part of the str3 string is: \n"<<str4<<"\n\n";

    // initializing a string with a number of characters of a specific value

    basic_string <char> str5(6, '7');

    cout<<"Operation: str5(6, '7')"<<endl;

    cout<<"str5 initialized by six number of 7s is: "<<str5<<"\n\n";

    // creating an empty string and string with a specified allocator

    basic_string <char> str6;

    string str7;

    // allocate a storage

    basic_string <char> str8(str7.get_allocator());

    // test the emptiness

    cout<<"Operation: str8.empty()"<<endl;

    if(str8.empty())

       cout<<"The string str8 is empty."<<endl;

    else

       cout<<"The string str8 is not empty."<<endl;

    // fill up some string

    str8 = "Not empty!";

    cout<<"Operation: str8 = Not empty!"<<endl;

    // retest again...

    if(str8.empty())

       cout<<"The string str8 is empty."<<endl;

    else

       cout<<"The string str8 is not empty."<<endl;

    cout<<endl;

    // initializing a string from another range of characters

    string str10("Test StRiNg");

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

    cout<<"Operation: \nstr11(str10.begin()+5, str10.end())"<<endl;

    basic_string <char> str11(str10.begin()+5, str10.end());

    cout<<"str11 initialized by another range of characters is: \n"<<str11<<endl;

    return 0;

}

 

Output:

C++ STL character and string manipulation basic_string  

 

 

 

begin()

// begin(), end()

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

  

   string Str1("Testing the begin() and end()"), Str2;

   basic_string <char>::iterator Str1Iter;

   // const_iterator…

   basic_string <char>::const_iterator Str1CIter;

   // ...an error because the iterator Str1CIter is const...

   // *Str1CIter = 'Z';

   cout<<"String Str1 is: "<<Str1<<endl;

   Str1Iter = Str1.begin();

   cout<<"Operation: Str1Iter = Str1.begin()"<<endl;

   cout<<"The first character of the string Str1 is: "<<*Str1Iter<<"\n\n";

   // using dereferenced iterator to modify a character

   *Str1Iter = 'F';

   cout<<"Operation: *Str1Iter = 'F'"<<endl;

   cout<<"Now, the first character of the new Str1 is: "<<*Str1Iter<<endl;

   cout<<"The full modified string Str1 is now: \n"<<Str1<<"\n\n";

   // for an empty string, begin() == end()

   cout<<"Operation: if(Str2.begin() == Str2.end())"<<endl;

   if(Str2.begin() == Str2.end())

      cout<<"The string Str2 is empty."<<endl;

   else

      cout<<"The string Str2 is not empty."<<endl;

   cout<<endl;

   // fill up some string and retest...

   Str2 = "Not empty";

   cout<<"Operation: Str2 = \"Not empty\""<<endl;

   if(Str2.begin() == Str2.end())

      cout<<"Now the string Str2 is empty."<<endl;

   else

      cout<<"Now the string Str2 is not empty."<<endl;

   return 0;

}

 

Output:

 

C++ STL character and string manipulation begin()

 

 

c_str()

// c_str(), length(), data(), strlen()

#include <string>

#include <iostream>

using namespace std;

 

int main( )

{

   string  str1("Testing the c_str");

   cout<<"The original string object str1 is: "<<str1<<endl;

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

   cout<<"The length of the string object str1 = "<<str1.length()<<"\n\n";

   // a string to an array of characters conversion

   const char *ptr1 = 0;

   ptr1= str1.data();

   cout<<"Operation: ptr1= str1.data()"<<endl;

   cout<<"The string object pointed by ptr1 is: \n"<<ptr1<<endl;

   cout<<"\nOperation: strlen(ptr1)"<<endl;

   cout<<"The length of character array str1 = "<<strlen(ptr1)<<"\n\n";

   // a string to a C-style string conversion

   const char *cstr1 = str1.c_str();

   cout<<"Operation: *cstr1 = str1.c_str()"<<endl;

   cout<<"The C-style string c_str1 is: "<<cstr1<<endl;

   cout<<"\nOperation: strlen(cstr1)"<<endl;

   cout<<"The length of C-style string str1 = "<<strlen(cstr1)<<endl;

   return 0;

}

 

Output:

 

C++ STL character and string manipulation c_str()

 

 

capacity()

// capacity(), size(), erase(), length(), max_size()

#include <string>

#include <iostream>

using namespace std;

 

int main( )

{

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

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

    // the size and length member functions differ in name only

    basic_string <char>::size_type SizeStr1, LenStr1;

    SizeStr1 = str1.size();

    LenStr1 = str1.length();

    basic_string <char>::size_type CapStr1, MaxSizeStr1;

    CapStr1 = str1.capacity();

    MaxSizeStr1 = str1.max_size();

    // compare size, length, capacity & max_size of a string

    cout<<"\nOperation: str1.size()"<<endl;

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

    cout<<"\nOperation: str1.length()"<<endl;

    cout<<"The length of str1 is: "<<LenStr1<<" characters"<<endl;

    cout<<"\nOperation: str1.capacity()"<<endl;

    cout<<"The capacity of str1 is: "<<CapStr1<<" characters"<<endl;

    cout<<"\nOperation: str1.max_size()"<<endl;

    cout<<"The max_size of str1 is: "<<MaxSizeStr1<<" characters"<<endl;

    // erase some characters...

    str1.erase(6, 5);

    // re test...

    cout<<"\nOperation: str1.erase(6, 5)"<<endl;

    cout<<"The new str1 string is: "<<str1<<"\n\n";

    SizeStr1 = str1.size();

    LenStr1 = str1.length();

    CapStr1 = str1.capacity();

    MaxSizeStr1 = str1.max_size();

    // compare size, length, capacity & max_size of a string after erasing part of the original string

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

    cout<<"The new length of str1 is: "<<LenStr1<<" characters"<<endl;

    cout<<"The new capacity of str1 is: "<<CapStr1<<" characters"<<endl;

    cout<<"The new max_size of str1 is: "<<MaxSizeStr1<<" characters"<<endl;

    return 0;

}

 

Output:

 

C++ STL character and string manipulation capacity()

 

 

clear()

// clear()

#include <string>

#include <iostream>

using namespace std;

 

int main( )

{

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

    basic_string <char>::iterator StrIter;

    // normal...

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

    // using iterator....iterate character by character...

    cout<<"str1 string is: ";

    for (StrIter = str1.begin(); StrIter != str1.end(); StrIter++)

        cout<<*StrIter;

    cout<<endl;

    // str1.clear();

    cout<<"\nErasing part of the string using erase(13)"<<endl;

    str1.erase(13);

    cout<<"Operation: str1.erase(13)"<<endl;

    cout<<"The modified str1 string is: ";

    // using iterator...

    for(StrIter = str1.begin(); StrIter != str1.end(); StrIter++)

        cout<<*StrIter;

    cout<<endl;

    // for an empty string, begin is equivalent to end

    cout<<"\nOperation: str1.begin()==str1.end()"<<endl;

    if(str1.begin() == str1.end())

        cout<<"The str1 string is empty."<<endl;

    else

        cout<<"The str1 string has some data"<<endl;

    // erasing all the data...

    cout<<"\nOperation: str1.erase(13)"<<endl;

    cout<<"Erasing all the data using erase()"<<endl;

    str1.erase();

    // re test...

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

    cout<<"\nOperation: str1.begin()==str1.end()"<<endl;

    if(str1.begin() == str1.end())

        cout<<"The str1 string is empty."<<endl;

    else

        cout<<"The str1 string has some data"<<endl;

    return 0;

}

 

Output:

 

C++ STL character and string manipulation clear()

 

compare()

// compare() program example part I

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

    // comparing a string to a string, the character index start from 0

    int str1;

    string str2("First");

    string str3("First");

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

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

    // compare str2 and str3 then assign the result to str1

    cout<<"Operation: str2.compare(str3)"<<endl;

    str1 = str2.compare(str3);

    if(str1 < 0)

        cout<<"The str2 string is less than the str3 string."<<endl;

    else if(str1 == 0)

        cout<<"The str2 string is equal to the str3 string."<<endl;

    else

        cout<<"The str2 string is greater than the str3 string."<<endl;

    cout<<endl;

    // comparing part of a string to a string

    int str4, str5;

    string str6("SecondThird");

    string str7("Third");

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

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

    cout<<"Operation: str6.compare(6, 5, str7)"<<endl;

    str4 = str6.compare(6, 5, str7);

    if(str4 < 0)

       cout<<"The last 5 characters of the str6 string are less than\nthe str7 string."<<endl;

    else if(str4 == 0)

       cout<<"The last 5 characters of the str6 string are equal to\nthe str7 string."<<endl;

    else

       cout<<"The last 5 characters of the str6 string is greater than\nthe str7 string."<<endl;

    cout<<endl;

    cout<<"Operation: str6.compare(0, 6, str7)"<<endl;

    str5 = str6.compare(0, 6, str7);

    if(str5 < 0)

       cout<<"The first 6 characters of the str6 \nstring are less than the str7 string."<<endl;

    else if(str5 == 0)

       cout<<"The first 6 characters of the str6 \nstring are equal to the str7 string."<<endl;

    else

       cout<<"The first 6 characters of the str6 \nstring is greater than the str7 string."<<endl;

    return 0;

}

 

Output:

 

C++ STL character and string manipulation compare()

 

// compare() program example part II

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

    // comparing part of a string to part of a string

    int str8;

    string str9("TestFourth");

    string str10("TFourthT");

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

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

    str8 = str9.compare(4, 6, str10, 1, 6);

    cout<<"Operation: str9.compare(4, 6, str10, 1, 6)"<<endl;

    if(str8 < 0)

        cout<<"The 6 characters from position 4 of the str9 string \nare less than "

        <<"the 6 characters str10 string from position 1."<<endl;

    else if(str8 == 0)

        cout<<"The 6 characters from position 4 of the str9 string\nare equal to "

        <<"the 6 characters str10 string from position 1."<<endl;

    else

        cout<<"The 6 characters from position 4 of the str9 string\nis greater than "

        <<"the 6 characters str10 string from position 1."<<endl;

    cout<<endl;

    // comparing a string to a C-string

    int str11;

    string str12("Fifth");

    const char* str13 = "Sixth";

    cout<<"The string str12 is: "<<str12<<endl;

    cout<<"The C-string str13 is: "<<str13<<endl;

    str11 = str12.compare(str13);

    cout<<"Operation: str12.compare(str13)"<<endl;

    if(str11 < 0)

        cout<<"The str12 string is less than the str13 C-string."<<endl;

    else if(str11 == 0)

        cout<<"The str12 string is equal to the str13 C-string."<<endl;

    else

        cout<<"The str12 string is greater than the str13 C-string."<<endl;

    cout << endl;

    // comparing part of a string to a C-string

    int str14;

    string str15("SeventhEight");

    const char* str16 = "Eight";

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

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

    str14 = str15.compare(7, 5, str16);

    cout<<"Operation: str15.compare(7, 5, str16)"<<endl;

    if(str14 < 0)

        cout<<"The last 5 characters of the str15 \nstring are less than the str16 C-string."<<endl;

    else if(str14 == 0)

        cout<<"The last 5 characters of the str15 \nstring are equal to the str16 C-string."<<endl;

    else

        cout<<"The last 5 characters of the str15 \nstring is greater than the str16 C-string."<<endl;

    cout << endl;

    // comparing part of a string to part of an equal length of a C-string

    int str17;

    string str18("ReTestEighth");

    const char* str19 = "TestEighth";

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

    cout<<"str19 C-string is: "<<str19<<endl;

    str17 = str18.compare(2, 4, str19, 4);

    cout<<"Operation: str18.compare(4, 6, str19, 6)"<<endl;

    if(str17 < 0)

        cout<<"The 4 characters from position 2 of the str18 string \nare less than "

           <<"the first 4 characters of the str19 C-string."<<endl;

    else if(str17 == 0)

        cout<<"The 4 characters from position 2 of the str18 string \nare equal to "

           <<"the first 4 characters of the str19 C-string."<<endl;

    else

        cout<<"The 4 characters from position 2of the str18 string \nis greater than "

            <<"the first 4 characters of the str19 C-string."<<endl;

    return 0;

}

 

Output:

 

C++ STL character and string manipulation compare()

 

 

copy()

// copy()

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

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

   basic_string <char>::iterator StrIter;

   // declare and initialize arrays to 0

   char Array1[20] = {0};

   char Array2[10] = {0};

   basic_string <char>:: pointer Array1Ptr = Array1;

   basic_string <char>:: value_type *Array2Ptr = Array2;

   // iterate character by character...

   cout<<"str1 string is: ";

   for(StrIter = str1.begin(); StrIter != str1.end(); StrIter++)

      cout<<*StrIter;

   cout<<endl;

   basic_string <char>::size_type NewArray1;

   NewArray1 = str1.copy(Array1Ptr, 18);

   cout<<"Operation: str1.copy(Array1Ptr, 18)"<<endl;

   cout<<"The number of copied characters in Array1 is: "<<unsigned int(NewArray1)<<endl;

   cout<<"Now, Array1 is: "<<Array1<<"\n\n";

   basic_string <char>::size_type NewArray2;

   NewArray2 = str1.copy(Array2Ptr, 9, 2);

   cout<<"Operation: str1.copy(Array2Ptr, 9, 2)"<<endl;

   cout<<"The number of copied characters in Array2 is: "<<unsigned int(NewArray2)<<endl;

   cout<<"Now, Array2 is: "<<Array2Ptr<<endl;

   return 0;

}

 

Output:

C++ STL character and string manipulation copy()

 

 

tenouk C++ STL programming tutorial

 

 

 

 

 

 

 

 

 

 

 

 

Further C++ string related reading:

 

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

  2. Check the best selling C / C++, STL and UML books at Amazon.com.

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

 

 

 

 

 

 

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


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