|< C++ String Class 3 | Main | C++ String Class 5 >| Site Index | Download |


 

 

MODULE 25b

C++ STRING CLASS 4

 

 

My Training Period: xx hours

 

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

 

C++ string class manipulation abilities that should be acquired:

 

         Able to understand and use the basic_string template class.

         Able to understand and use the member functions of the strings and characters class member functions.

         Able to recognize the Containers, Iterators and Algorithms of the STL.

 

What we have in this page?

  1. data(), length(), strlen() and c_str() example

  2. empty() example

  3. begin(), end() example

  4. erase() example

  5. find() example

  6. find_first_not_of() example

  7. find_first_of() example

  8. example compiled using g++

 

 

 

 

 

 

data()

  • Objects of type string belonging to the C++ template class basic_string <char> are not necessarily null terminated. The return type for data() is not a valid C-string, because no null character gets appended. The null character '\0' is used as a special character in a C-string to mark the end of the string, but has no special meaning in an object of type string and may be a part of the string object just like any other character.

  • There is an automatic conversion from const char* into strings, but the string class does not provide for automatic conversions from C-style strings to objects of type basic_string <char>.

  • The returned string should not be modified, because this could invalidate the pointer to the string, or deleted, because the string has a limited lifetime and is owned by the class string.

  • The return value is a pointer to the first element of the array containing the contents of the string, or, for an empty array, a non-null pointer that cannot be dereferenced.

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

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

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

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

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

    cout<<"The length of str1 = "<<unsigned int(str1.length())<<"\n\n";

    // converting a string to an array of characters

    const char *ptr1 = 0;

    ptr1= str1.data();

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

    cout<<"The modified ptr1 string object is: "<<ptr1<<endl;

    cout<<"Operation: strlen(ptr1)"<<endl;

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

    // converting a string to a C-style string

    const char *cstr1 = str1.c_str();

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

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

    cout<<"Operation: strlen(ptr1)"<<endl;

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

    return 0;

}

 

Output:

 

C++ STL character and string manipulation data()

 

 

empty()

// empty()

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

    bool Var1, Var2;

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

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

    Var1 = str1.empty();

    // test the emptiness

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

    if(Var1)

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

    else

        cout<<"str1 string object is not empty."<<"\n\n";

    // an example of an empty string object

    string str3;

    Var2 = str3.empty();

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

    // test the emptiness

    if(Var2)

        cout<<"str3 string object is empty."<<endl;

    else

        cout<<"str3 string object is not empty."<<endl;

    return 0;

}

 

Output:

 

C++ STL character and string manipulation empty()

 

 

end()

// begin(), end()

#include <string>

#include <iostream>

using namespace std;

 

int main( )

{

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

    basic_string <char>::iterator StrIter, Str1Iter;

   

    Str1Iter = str1.end();

    // minus the null character, so point to the real last character in the string...

    Str1Iter--;

    cout<<"Operation: str1.end() then Str1Iter--"<<endl;

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

    cout<<"The last character of the str1 string is: "<<*Str1Iter<<endl;

    // end() used to test when an iterator has reached the end of its string

    cout<<"Using forward iterator the str1 is: ";

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

        cout<<*StrIter;

    cout<<"\n\n";

    // the dereferenced iterator can be used to modify a character

    // the last event, this pointer point to the last character in the string

    *Str1Iter = 'F';

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

    cout<<"Now, the last character of the modified str1 is: "<<*Str1Iter<<endl;

    cout<<"Then the modified str1 string is: "<<str1<<endl;

    return 0;

}

 

Output:

 

C++ STL character and string manipulation begin() end()

 

 

erase()

// erase()

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

    // using a range...

    string str1("Testing the erase() part I");

    basic_string <char>::iterator Str1Iter;

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

    // don't forget the null character

    Str1Iter = str1.erase(str1.begin() + 4, str1.end() - 6);

    cout<<"Operation: str1.erase(str1.begin()+4, str1.end()-6)"<<endl;

    cout<<"The first element after those removed is: "<<*Str1Iter<<endl;

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

    // erasing a char pointed to by an iterator

    string str2("Testing the erase() part II");

    basic_string <char>::iterator Str2Iter;

    cout<<"\nstr2 string object is: "<<str2<<endl;

    Str2Iter = str2.erase(str2.begin() + 3);

    cout<<"Operation: str2.erase(str2.begin() + 3)"<<endl;

    cout<<"The first element after those removed is: "<<*Str2Iter<<endl;

    cout<<"The modified str2 string object is: \n"<<str2<<endl;

    // erasing a number of chars after a char

    string str3("Testing the erase() part III"), NewStr3;

    cout<<"\nThe original string object str3 is: \n"<<str3<<endl;

    NewStr3 = str3.erase(6, 8);

    cout<<"Operation: str3.erase(6, 6)"<<endl;

    cout<<"The modified NewStr3 string object is: \n"<<NewStr3<<endl;

    return 0;

}

 

Output:

 

C++ STL character and string manipulation erase()

 

 

find()

// find() part I

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

    // don't forget the null character, searching for a single character in a string

    string str1("Search part I, a character in a string");

    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('r', 2);

    cout<<"Operation: str1.find('r', 2)"<<endl;

    if(index1 != npos)

        cout<<"The index of the 1st 'r' found after the 2nd position in str1 is: "<<unsigned int(index1)<<endl;

    else

        cout<<"The character 'r' was not found in str1."<<endl;

    cout<<endl;

    index2 = str1.find('t');

    cout<<"Operation: str1.find('t')"<<endl;

    if(index2 != npos)

        cout<<"The index of the 't' found in str1 is: "<<unsigned int(index2)<<endl;

    else

        cout<<"The character 't' was not found in str1."<<endl;

    cout<<endl;

    // ---------------------------------------------------------------------

    // searching a string for a substring as specified by a C-string

    string str2("Search part II, a substring in string");

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

    basic_string <char>::size_type index3, index4;

    const char *cstr1 = "sub";

    index3 = str2.find(cstr1, 5);

    cout<<"Operation: str2.find(cstr1, 5)"<<endl;

    if(index3 != npos)

        cout<<"The index of the 1st element of 'sub' after\nthe 5th "

            <<"position in str2 is: "<<unsigned int(index3)<<endl;

    else

        cout<<"The substring 'sub' was not found in str2"<<endl;

    cout<<endl;

    const char *cstr2 = "bstring";

    index4 = str2.find(cstr2, 0);

    cout<<"Operation: str2.find(cstr2, 0)"<<endl;

    if(index4 != npos)

        cout<<"The index of the 1st element of 'bstring' "

           <<"after\nthe 0th position in str2 is: "<<unsigned int(index4)<<endl;

    else

    cout<<"The substring 'bstring' was not found in str2"<<endl;

    return 0;

}

 

Output:

 

C++ STL character and string manipulation find()

 

// find() part II

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

    // don't forget the null character

    // searching a string for a substring as specified by a C-string

    static const basic_string <char>::size_type npos = -1;

    string str3("Again, search part III");

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

    basic_string <char>::size_type index5, index6;

    const char *cstr3 = "part";

    index5 = str3.find(cstr3);

    cout<<"Operation: str3.find(cstr3)"<<endl;

    if(index5 != npos)

        cout<<"The index of the 1st element of 'part' in str3 is: "<<unsigned int(index5)<<endl;

    else

        cout<<"The substring 'part' was not found in str3"<<endl;

    cout<<endl;

    const char *cstr4 = "ar";

    index6 = str3.find(cstr4, index5 + 1, 2);

    cout<<"Operation: str3.find(cstr4, index5 + 1, 2)"<<endl;

    if(index6 != npos)

        cout<<"The index of the next occurrence of 'ar' in str3 begins at: "<<unsigned int(index6)<<endl;

    else

        cout<<"There is no next occurrence of 'ar' in str3."<<endl;

    cout<<endl;

    // --------------------------------------------------------------

    // searching a string for a substring as specified by a string

    string str4("Finally!, search part IV");

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

    basic_string <char>::size_type index7, index8;

    string str5("part");

    index7 = str4.find(str5, 4);

    cout<<"Operation: str4.find(str5, 4)"<<endl;

    if(index7 != npos)

        cout<<"The index of the 1st element of 'part' after\nthe 4th position in str4 is: "<<unsigned int(index7)<<endl;

    else

        cout<<"The substring 'part' was not found in str4"<<endl;

    cout<<endl;

    string str6("arch");

    index8 = str4.find(str6);

    cout<<"Operation: str4.find(str6)"<<endl;

    if(index8 != npos)

        cout<<"The index of the 1st element of 'arch' in str4 is: "<<unsigned int(index8)<<endl;

    else

        cout<<"The substring 'arch' was not found in str4"<<endl;

    return 0;

}

 

Output:

 

-------------------------------------------------------------------------------------------

C++ STL character and string manipulation find()

 

 

find_first_not_of()

// find_first_not_of() part I

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

    // searching a single character in a string

    string str1("Testing the find_first_not_of() part 1");

    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_first_not_of('_', 3);

    cout<<"Operation: str1.find_first_not_of('_', 3)"<<endl;

    if(index1 != npos)

        cout<<"The index of the 1st '_' found after the 3rd\nposition in str1 is: "<<unsigned int(index1)<<endl;

    else

        cout<<"The character '_' was not found in str1"<<endl;

    index2 = str1.find_first_not_of('T');

    cout<<"\nOperation: str1.find_first_not_of('T')"<<endl;

    if(index2 != npos)

        cout<<"The index of the 'non T' found in str1 is: "<<unsigned int(index2)<<endl;

    else

        cout<<"The character 'non T' was not found in str1."<<endl;

    cout<<endl;

    // ---------------------------------------------------------------------------

    // searching a string for a substring as specified by a C-string

    string str2("Testing the find_first_not_of() part 2");

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

    basic_string <char>::size_type index3, index4;

    const char *cstr2 = "df";

    index3 = str2.find_first_not_of(cstr2, 4);

    cout<<"Operation: str2.find_first_not_of(cstr2, 4)"<<endl;

    if(index3 != npos)

        cout<<"The index of the 1st occurrence of an element\nof 'df' in str2 after the 4th "

          <<"position is: "<<unsigned int(index3)<<endl;

    else

         cout<<"Elements of the substring 'df' were not\nfound in str2 after the 4th position."<<endl;

    const char *cstr3 = "gz";

    index4 = str2.find_first_not_of(cstr3);

    cout<<"\nOperation: str2.find_first_not_of(cstr3)"<<endl;

    if(index4 != npos)

        cout<<"The index of the 1st element of 'gz' after\nthe 0th position in str2 is: <<unsigned int(index4)<<endl;

    else

        cout<<"The substring 'gz' was not found in str2"<<endl;

    return 0;

}

 

Output:

 

C++ STL character and string manipulation find_first_not_of()

-------------------------------------------------------------------------------------------------

 

// find_first_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("Testing the find_first_not_of() part 3");

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

    basic_string <char>::size_type index5, index6;

    static const basic_string <char>::size_type npos = -1;

    const char *cstr4 = "nro";

    index5 = str3.find_first_not_of(cstr4);

    cout<<"Operation: str3.find_first_not_of(cstr4)"<<endl;

    if(index5 != npos)

        cout<<"The index of the 1st occurrence of an element in str3\nother than one of the "

          <<"characters in 'nro' is: "<<unsigned int(index5)<<endl;

    else

        cout<<"Elements in str3 contain only characters in the string 'nro'. "<<endl;

    const char *cstr5 = "nro";

    index6 = str3.find_first_not_of(cstr5, index5+1, 2);

    cout<<"\nOperation: str3.find_first_not_of(cstr5, index5+1, 2)"<<endl;

    if(index6 != npos)

        cout<<"The index of the second occurrence of an element\nof 'nro' in str3 after the 0th "

          <<"position is: "<<unsigned int(index6)<<endl<<endl;

    else

        cout<<"Elements in str3 contain only characters in the string 'nro'"<<endl;

    cout<<endl;

    // --------------------------------------------------------------------

    // searching a string for a substring as specified by a string

    string str4("Testing the find_first_not_of() part 4");

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

    basic_string <char>::size_type index7, index8;

    string str5("tf7");

    index7 = str4.find_first_not_of(str5, 3);

    cout<<"Operation: str4.find_first_not_of(str5, 3)"<<endl;

    if(index7 != npos)

          cout<<"The index of the 1st non occurrence of an element\nof 'tf7' "

             <<"in str4 after the 3rd position is: "<<unsigned int(index7)<<endl;

    else

          cout<<"Elements other than those in the substring 'tf7' were not found in the string str4."<<endl;

    string str6("in");

    index8 = str4.find_first_not_of(str6);

    cout<<"\nOperation: str4.find_first_not_of(str6)"<<endl;

    if(index8 != npos)

        cout<<"The index of the 1st occurrence of an element of\n'in' in str4 after the 0th "

              <<"position is: "<<unsigned int(index8)<<endl;

    else

         cout<<"Elements other than those in the substring 'in' were not found in the string str4."<<endl;

    return 0;

}

 

Output:

 

C++ STL character and string manipulation find_first_not_of()

 

 

find_first_of()

// find_first_of() part I

#include <string>

#include <iostream>

using namespace std;

 

int main( )

{

   // searching for a single character in a string

   string str1("find_first_of()");

   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_first_of('r', 3);

   cout<<"Operation: str1.find_first_of('r', 3)"<<endl;

   if(index1 != npos)

      cout<<"The index of the 1st 'r' found after the 3rd\n"

           <<"position in str1 is: "<<unsigned int(index1)<<endl;

   else

      cout<<"The character 'r' was not found in str1"<<endl;

   index2 = str1.find_first_of('z');

   cout<<"\nOperation: 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;

   // --------------------------------------------------------

   // searching a string for a substring as specified by a C-string

   string str2("Testing 123...Testing 123");

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

   basic_string <char>::size_type index3, index4;

   const char *cstr = "s1";

   index3 = str2.find_first_of(cstr, 3);

   cout<<"Operation: str2.find_first_of(cstr, 3)"<<endl;

   if(index3 != npos)

      cout<<"The index of the 1st occurrence of an element\nof 's1' in str2 after the 3rd "

           <<"position is: "<<unsigned int(index3)<<endl;

   else

      cout<<"Elements of the substring 's1' were not\nfound in str2 after the 3rd position."<<endl;

   const char *cstr1 = "g3";

   index4 = str2.find_first_of(cstr1);

   cout<<"\nOperation: str2.find_first_of(cstr1)"<<endl;

   if(index4 != npos)

      cout <<"The index of the 1st 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 manipulation find_first_of()

 

// find_first_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 456...Testing 456...789");

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

   basic_string <char>::size_type index5, index6;

   static const basic_string <char>::size_type npos = -1;

   const char *cstr2 = "t6";

   index5 = str3.find_first_of(cstr2);

   cout<<"Operation: str3.find_first_of(cstr2)"<<endl;

   if(index5 != npos)

      cout<<"The index of the 1st occurrence of an element\nof 't6' in str3 after the 0th "

          <<"position is: "<<unsigned int(index5)<<endl;

   else

      cout<<"Elements of the substring 't6' were not\nfound in str3 after the 0th position."<<endl;

   const char *cstr3 = "t68";

   index6 = str3.find_first_of(cstr3, index5 + 1, 2);

       cout<<"\nOperation: str3.find_first_of(cstr3, index5 + 1, 2)"<<endl;

   if(index6 != npos)

      cout<<"The index of the second occurrence of an element\nof 't68' in str3 after the 0th "

          <<"position is: "<<unsigned int(index6)<<endl;

   else

      cout<<"Elements of the substring 't68' were not\nfound in str3 after the first occurrence."<<endl;

   cout<<endl;

   //------------------------------------------------------------

   //searching a string for a substring as specified by a string

   string str4("find_first_of() and find_first_of()");

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

   basic_string <char>::size_type index7, index8;

   string str5("dfz");

   index7 = str5.find_first_of(str5, 3);

   cout<<"Operation: str5.find_first_of(str5, 3)"<<endl;

   if(index7 != npos)

      cout<<"The index of the 1st occurrence of an element\nof 'dfz' in str4 after the 3rd "

           <<"position is: "<<unsigned int(index7)<<endl;

   else

      cout<<"Elements of the substring 'dfz' were not\n"

           <<"found in str4 after the 3rd position."<<endl;

   string str6("fo");

   index8 = str4.find_first_of(str6);

   cout<<"\nOperation: str4.find_first_of(str6)"<<endl;

   if(index8 != npos)