char_traits, eq() example
char_traits, eq_int_type() and to_int_type() example
char_traits, find() example
char_traits, length() example
char_traits, lt() example
char_traits, move(), find() example
char_traits, not_eof() example
char_traits, to_char_type(), to_int_type and eq() example
char_traits Specializations
Using C++ wrapper for C 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 andVisual 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.
|
|
A continuation from previous Moduleā¦
eq()
// 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:
eq_int_type()
|
// 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;
}

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

The return value is the number of elements in the sequence being measured, not including the null terminator.
// 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;
}

The return value is true if the first character is less than the second character; otherwise false.
// 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;
}

The source and destination may overlap. Compare withcopy().
The return value is the first element is copied into the string or character array targeted to receive the copied sequence of characters.
// 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;
}
|
The return value is the int_type representation of the character tested, if the int_type of the character is not equal to that of the EOF character.
If the character int_type value is equal to the EOF int_type value, then it is false.
// 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;
}

The conversion operations to_int_type andto_char_type are inverse operation to each other. For example:
to_int_type(to_char_type(x)) == x
And for any int_type x:
to_char_type(to_int_type(x)) == x for any char_type x.
The return value is the char_type character corresponding to the int_type character.
A value that cannot be represented by the conversion will yield an unspecified result.
// 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;
}

The following table is a list of the char_traits class template specialization.
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 | |
The following are recompiling and re running of the C characters and strings program examples. Program examples are taken fromModule X. You can try other program examples as well.
// 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;
}

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

Program example compiled usingg++. Portability is not an issue here :o). g++ warmly warn you for constructs that are obsolete! Can we say that g++ is a better choice compared to the commercial versions?
// **********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
The source code for this tutorial is available atC++ Characters & Strings source codes.
Acomplete C++ Standard Library documentation that includes STL.
Check thebest selling C/C++ books at Amazon.com.