|< C++ String Class 6 | Main | C++ STL Container 1 - vector, deque >| Site Index | Download |


 

 

 

 

 

 

 

MODULE 26a_1

C++ STRING CLASS CODE EXAMPLES 7

 

 

 

 

 

 

What do we have in this session?

  1. char_traits, eq() example

  2. char_traits, eq_int_type() and to_int_type() example

  3. char_traits, find() example

  4. char_traits, length() example

  5. char_traits, lt() example

  6. char_traits, move(), find() example

  7. char_traits, not_eof() example

  8. char_traits, to_char_type(), to_int_type and eq() example

  9. char_traits Specializations

  10. Using C++ wrapper for C 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.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

A continuation from previous Moduleā€¦

 

eq()

  • The return value is true if the first character is equal to the second character; otherwise false.

// char_traits, eq()

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

   char_traits<char>::char_type chr1 = 'P';

   char_traits<char>::char_type chr2 = 'Q';

   char_traits<char>::char_type chr3 = 'P';

   // testing for equality

   bool Var1 = char_traits<char>::eq(chr1, chr2);

   cout<<"Operation: eq(chr1, chr2)"<<endl;

   if(Var1)

      cout<<"The character chr1 and chr2 is equal."<<endl;

   else

      cout<<"The character chr1 and chr2 is not equal."<<endl;

   // alternatively...

   cout<<"\nOperation: using \'==\' operator, chr1==chr3"<<endl;

   if(chr1 == chr3)

      cout<<"The character chr1 and chr3 is equal."<<endl;

   else

      cout<<"The character chr1 and chr3 is not equal."<<endl;

   return 0;

}

 

Output:

 

C++ STL character and string manipulation eq()

 

 

eq_int_type()

  • The return value is true if the first character is equal to the second character; otherwise false.

// char_traits, eq_int_type() and to_int_type()

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

   char_traits<char>::char_type chr1 = 'P';

   char_traits<char>::char_type chr2 = 'Q';

   char_traits<char>::char_type chr3 = 'P';

   char_traits<char>::char_type chr4 = 'r';

   // char_type to int_type conversion

   char_traits<char>::int_type int1, int2, int3, int4;

   int1 = char_traits<char>::to_int_type(chr1);

   int2 = char_traits<char>::to_int_type(chr2);

   int3 = char_traits<char>::to_int_type(chr3);

   int4 = char_traits<char>::to_int_type(chr4);

   cout<<"Operation:  to_int_type(character)"<<endl;

   cout<<"The char_types and corresponding int_types are:\n";

   cout<<chr1<<" = "<<int1<<endl;

   cout<<chr2<<" = "<<int2<<endl;

   cout<<chr4<<" = "<<int4<<endl;

   // equality of int_type representations test

   cout<<"\nOperation:  eq_int_type(int1, int2)"<<endl;

   bool var1 = char_traits<char>::eq_int_type(int1, int2);

   if(var1)

      cout<<"The int_type representation of characters chr1\nand chr2 is equal."<<endl;

   else

     cout<<"The int_type representation of characters chr1\nand chr2 is not equal."<<endl;

   // alternatively...

   cout<<"\nOperation:  int1 == int3"<<endl;

   if(int1 == int3)

      cout<<"The int_type representation of characters chr1\nand chr3 is equal."<<endl;

   else

      cout<<"The int_type representation of characters chr1\nand chr3 is not equal."<<endl;

   return 0;

}

 

Output:

 

C++ STL character and string manipulation eq_int_type()

 

 

 

find()

 

// char_traits, find()

#include <string>

#include <iostream>

using namespace std;

 

int main( )

{

   const char* str = "Testing the char_traits, find()";

   const char* result1;

   cout<<"The string to be searched is:\n"<<str<<endl;

   // searching for a 'a' in the first 20 positions of string str

   cout<<"\nOperation: find(str, 20, 'a')"<<endl;

   result1 = char_traits<char>::find(str, 20, 'a');

   cout<<"Searching character \'"<<*result1<<"\'."<<endl;

   cout<<"The string beginning with the first occurrence\nof the character 'a' is: "<<result1<<endl;

   // when no match is found the NULL value is returned

   const char* result2;

   result2 = char_traits<char>::find(str, 20, 'z');

   cout<<"\nOperation: find(str, 20, 'z')"<<endl;

   if(result2 == NULL)

      cout<<"The character 'z' was not found."<<endl;

   else

      cout<<"The result of the search is: "<<result2<<endl;

   return 0;

}

 

Output:

 

C++ STL character and string manipulation find()

 

 

length()

// char_traits, length()

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

   const char* str1= "Testing 1...2...3";

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

   size_t LenStr1;

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

   LenStr1 = char_traits<char>::length(str1);

   cout<<"The length of str1 is: "<<unsigned int(LenStr1)<<endl;

   return 0;

}

 

Output:

 

C++ STL character and string manipulation length()

 

 

lt()

// char_traits, lt()

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

   char_traits<char>::char_type chr1 = '1';

   char_traits<char>::char_type chr2 = 'q';

   char_traits<char>::char_type chr3 = 'R';

   char_traits<char>::int_type int1, int2, int3;

   int1 = char_traits<char>::to_int_type(chr1);

   int2 = char_traits<char>::to_int_type(chr2);

   int3 = char_traits<char>::to_int_type(chr3);

   // char_type to int_type conversion, for testing

   cout<<"chr1 = "<<chr1<<", chr2 = "<<chr2<<", chr3 = "<<chr3<<endl;

   cout<<"chr1 = "<<int1<<", chr2 = "<<int2<<", chr3 = "<<int3<<endl;

   // testing for less than

   cout<<"\nOperation: lt(chr1, chr2)"<<endl;

   bool var1 = char_traits<char>::lt(chr1, chr2);

   if(var1)

      cout<<"The chr1 is less than the chr2."<<endl;

   else

      cout<<"The chr1 is not less than the chr2."<<endl;

   // alternatively...

   cout<<"\nOperation: chr2 < chr3"<<endl;

   if(chr2 < chr3)

      cout<<"The chr2 is less than the chr3."<<endl;

   else

      cout<<"The chr2 is not less than the chr3."<<endl;

   return 0;

}

 

Output:

 

C++ STL character and string manipulation lt()

 

 

move()

// char_traits, move(), find()

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

   char_traits<char>::char_type str1[25] = "The Hell Boy";

   char_traits<char>::char_type str2[25] = "Something To ponder";

   char_traits<char>::char_type *result1;

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

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

   result1 = char_traits<char>::move(str2, str1, 10);

   cout<<"\nOperation: move(str2, str1, 10)"<<endl;

   cout<<"The result1 = "<<result1<<endl;

   // when source and destination overlap

   char_traits<char>::char_type str3[30] = "Testing the move()";

   char_traits<char>::char_type *result2;

   cout << "The source/destination str3 string is: "<<str3<<endl;

   cout<<"\nOperation: str4 = find(str3, 12, 'h')"<<endl;

   const char *str4 = char_traits<char>::find(str3, 12, 'h');

   cout<<"Operation: move(str3, str4, 9)"<<endl;

   result2 = char_traits<char>::move(str3, str4, 9);

   cout<<"The result2 = "<<result2<<endl;

   return 0;

}

 

Output:

 

C++ STL character and string manipulation move()

 

 

 

not_eof()

// char_traits, not_eof()

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

   char_traits<char>::char_type chr1 = 'w';

   char_traits<char>::int_type int1;

   int1 = char_traits<char>::to_int_type(chr1);

   cout<<"Operation: to_int_type(chr1)"<<endl;

   cout<<"The char_type "<<chr1<<" = int_type "<<int1<<endl;

   // EOF

   char_traits <char>::int_type int2 = char_traits<char>::eof();

   cout<<"\nOperation: char_traits<char>::eof()"<<endl;

   cout<<"The eof return is: "<<int2<<endl;

   // testing for EOF

   char_traits <char>::int_type eofTest1, eofTest2;

   eofTest1 = char_traits<char>::not_eof(int1);

   cout<<"\nOperation: not_eof(int1)"<<endl;

   if(!eofTest1)

      cout<<"The eofTest1 indicates "<<chr1<<" is an EOF character."<<endl;

   else

      cout<<"The eofTest1 returns: "<<eofTest1<<", which is the character: "<<char_traits<char>::to_char_type(eofTest1)<<endl;

   eofTest2 = char_traits<char>::not_eof(int2);

   cout<<"\nOperation: not_eof(int2)"<<endl;

   if(!eofTest2)

      cout<<"The eofTest2 indicates "<<chr1<<" is an EOF character."<<endl;

   else

      cout<<"The eofTest1 returns: "<<eofTest2<<", which is the character "<<char_traits<char>::to_char_type(eofTest2)<<endl;

   return 0;

}

 

Output:

 

C++ STL character and string manipulation not_eof()

 

 

to_char_type() and to_int_type()

to_int_type(to_char_type(x)) == x

to_char_type(to_int_type(x)) == x   for any char_type x.

// char_traits, to_char_type(), to_int_type and eq()

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

   char_traits<char>::char_type chr1 = '3';

   char_traits<char>::char_type chr2 = 'C';

   char_traits<char>::char_type chr3 = '#';

   cout<<"chr1 = "<<chr1<<", chr2 = "<<chr2<<", chr3 = "<<chr3<<endl;

   // converting from char_type to int_type

   char_traits<char>::int_type int1, int2, int3;

   int1 =char_traits<char>::to_int_type(chr1);

   int2 =char_traits<char>::to_int_type(chr2);

   int3 =char_traits<char>::to_int_type(chr3);

   cout<<"Operation: to_int_type(character)"<<endl;

   cout<<"The char_types and corresponding int_types are:\n";

   cout<<chr1<<" ==> "<<int1<<endl;

   cout<<chr2<<" ==> "<<int2<<endl;

   cout<<chr3<<" ==> "<<int3<<endl;

   // int_type to char_type re conversion

   char_traits<char>::char_type rev_chr1;

   rev_chr1 = char_traits<char>::to_char_type(int1);

   char_traits<char>::char_type rev_chr2;

   rev_chr2 = char_traits<char>::to_char_type(int2);

   cout<<"\nOperation: to_char_type(integer)"<<endl;

   cout<<"The inverse conversion are:\n"; 

   cout<<int1<<" ==> "<<rev_chr1<<endl;

   cout<<int2<<" ==> "<<rev_chr2<<endl;

   // test for conversions, they are just inverse operations

   cout<<"\nOperation: eq(rev_chr1, chr1)"<<endl;

   bool var1 = char_traits<char>::eq(rev_chr1, chr1);

   if(var1)

      cout<<"The rev_chr1 is equal to the original chr1."<<endl;

   else

     cout<<"The rev_chr1 is not equal to the original chr1."<<endl;

   // alternatively...

   if(rev_chr2 == chr2)

     cout<<"The rev_chr2 is equal to the original chr2."<<endl;

   else

     cout<<"The rev_chr2 is not equal to the original chr2."<<endl;

   return 0;

}

 

Output:

 

C++ STL character and string manipulation to_char_type() to_int_type() eq()

 

26.6  char_traits Specializations

Class Specialization

Brief Description

char_traits<char> class

A class that is a specialization of the template class char_traits<CharType> to an element of type char.

char_traits<wchar_t> class

A class that is a specialization of the template class char_traits<CharType> to an element of type wchar_t.

 

Table 26.3:  char_traits class template specialization

 

26.7  Using C++ wrapper for C String and Character Manipulation

// strtok(), using the C++ wrappers

#include <cstdio>

#include <string>

using namespace std;

 

int main()

{

       char string[ ] = "Is this sentence has 6 tokens?";

       char *tokenPtr;

       printf("      Using strtok()\n");

       printf("      --------------\n");

       printf("The string to be tokenized is:\n%s\n", string);

       printf("\nThe tokens are: \n\n");

       tokenPtr = strtok(string, " ");

       while (tokenPtr != NULL)

       {

            printf("%s\n", tokenPtr);

            tokenPtr  =  strtok(NULL, " ");

        }

       return   0;

}

 

Output:

 

C++ STL character and string manipulation C++ wrapper

 

// using strspn()

#include <cstdio>

#include <string>

using namespace std;

 

int main()

{

       char *string1 = "The initial value is 3.14159";

       char *string2 = "aehilsTuv";

       printf("      Using strspn()\n");

       printf("      ---------------\n");

       printf("string1 = %s\n", string1);

       printf("string2 = %s\n", string2);

       printf("\nThe length of the initial segment of string1\n");

       printf("containing only characters from string2 is = %u\n", strspn(string1, string2));

       return 0;

}

 

Output:

 

C++ STL character and string manipulation C header in C++

// **********string2.cpp************

// insert()

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

}

 

[bodo@bakawali ~]$ g++ string2.cpp -o string2

[bodo@bakawali ~]$ ./string2

 

str1 = e insert() testing

cstr1 = Th

Operation: str1.insert(0, cstr1)

Inserting a C-string at position 0 is:

The insert() testing

 

str2 = Test

cstr2 = ing an insert()

Operation: str2.insert(4, cstr2, 15)

Inserting a C-string at the end is:

Testing an insert()

 

str3 =  the insert()

str4 = Testing

Operation: str3.insert(0, str4)

Inserting string at position 0 is:

Testing the insert()

 

str5 = Testing

str6 =  the insert()

Operation: str5.insert(7, str6, 4, 9)

Inserting part of a string at position 9 is:

Testing insert()

 

tenouk C++ STL String class tutorial

 

 

 

 

 

 

 

 

 

 

 

 

 

Further C++ string class related reading:

 

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

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

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

 

 

 

 

 

 

|< C++ String Class 6 | Main | C++ STL Container 1 - vector, deque >| Site Index | Download |


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