============================MODULE26======================================= | | | The program examples' source codes have been arranged in the same | | order that appeared in the Tutorial. This is unedited and unverified | | compilation. Published as is basis for educational, reacretional and | | brain teaser purposes. All trademarks, copyrights and IPs, wherever | | exist, are the sole property of their respective owner and/or | | holder. Any damage or loss by using the materials presented in this | | tutorial is USER responsibility. Part or full distribution, | | reproduction and modification is granted to any body. | | Copyright 2003-2005 © Tenouk, Inc. All rights reserved. | | Distributed through http://www.tenouk.com | | | | | =========================================================================== Originally programs compiled using Borland C++. Example compiled using g++ are given at the end of every Module. For example if you want to compile C++ codes using VC++/VC++ .Net, change the header file accordingly. Just need some modification for the header files...: ------------------------------------------------- #include //for system() #include ... { C++ codes... } ------------------------------------------------- should be changed to: ------------------------------------------------- #include //use C++ wrapper to call C functions from C++ programs... #include using namespace std; ... { C++ codes... } ------------------------------------------------- In VC++/VC++ .Net the iostream.h (header with .h) is not valid anymore. It should be C++ header, so that it comply to the standard. In older Borland C++ compiler this still works, but not proper any more... and for standard C/C++ the portability should be no problem or better you read Module23 at http://www.tenouk.com/Module23.html to get the big picture...For C codes, they still C codes :o) ========================================================================= ============================HERE, ALL C++ codes========================== //find_last_not_of() part I #include #include using namespace std; int main() { //searching for a single character in a string string str1("daddy donkey is dead"); cout<<"str1 string is: "<::size_type index1, index2; static const basic_string ::size_type npos = -1; index1 = str1.find_last_not_of('d', 2); cout<<"Operation: str1.find_last_not_of('d', 2)"<::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)"< #include 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: "<::size_type index5, index6; static const basic_string ::size_type npos = -1; const char *cstr2 = "PTB"; index5 = str3.find_last_not_of(cstr2); cout<<"Operation: str3.find_last_not_of(cstr2)"<::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)"< #include using namespace std; int main() { //searching for a single character in a string string str1("Testing 1234 Testing 1234"); cout<<"str1 string is: "<::size_type index1, index2; static const basic_string ::size_type npos = -1; index1 = str1.find_last_of('g', 24); cout<<"Operation: str1.find_last_of('g', 24)"<::size_type index3, index4; const char *cstr = "t1"; index3 = str2.find_last_of(cstr, 25); cout<<"Operation: str2.find_last_of(cstr, 25)"< #include 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: "<::size_type index5; static const basic_string ::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)"<::size_type index6, index7; string str5("416"); index6 = str4.find_last_of(str5, 25); cout<<"Operation: str4.find_last_of(str5, 25)"< #include using namespace std; int main() { //using the default allocator. string str1; basic_string str2; basic_string , allocator > str3; //str4 will use the same allocator class as str1 basic_string str4(str1.get_allocator()); basic_string ::allocator_type xchar = str1.get_allocator(); //You can now call functions on the allocator class xchar used by str1 string str5(xchar); return 0; } ---------------------------------------------------------------------------------------------------------------- //insert() part I #include #include using namespace std; int main() { //inserting a C-string at a given position basic_string str1("e insert() testing"); const char *cstr1 = "Th"; cout<<"str1 = "< str2("Test"); const char *cstr2 = "ing an insert()"; cout<<"str2 = "< str3(" the insert()"); string str4("Testing"); cout<<"str3 = "< str5("Testing "); string str6(" the insert()"); cout<<"str5 = "< #include using namespace std; int main() { //inserting a number of characters at a specified position in the string string str7("Testing the insert()?"); cout<<"str7 = "< #include using namespace std; int main() { string str1("Testing the push_back()"); basic_string ::iterator StrIter, Str1Iter; cout<<"str1 string is: "; for(StrIter = str1.begin(); StrIter != str1.end(); StrIter++) cout<<*StrIter; cout< #include using namespace std; int main() { string str1("The reverse begin, rbegin()"), str2; basic_string ::reverse_iterator StrIter, Str1Iter; basic_string ::const_reverse_iterator str1_rcIter; //well, no need to minus the null character huh? cout<<"Operation: str1.rbegin()"< #include 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: "< #include 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: "<::iterator Iter1, Iter2; cout<<"Operation: str13.begin()"<::iterator Iter3, Iter4; cout<<"Operation: str16.begin()"<::iterator Iter5, Iter6; Iter5 = str18.begin(); Iter6 = str18.begin() + 3; str17 = str18.replace(Iter5, Iter6, 4, cstr5); cout<<"The new string is: "<::iterator Iter7, Iter8, Iter9, Iter10; cout<<"Operation: str20.begin() + 1"< #include using namespace std; int main() { string str1("Testing the reserve()"); cout<<"str1 string is: "<::size_type SizeStr1, Size1Str1; SizeStr1 = str1.size(); basic_string ::size_type CapaStr1, Capa1Str1; CapaStr1 = str1.capacity(); //Compare size & capacity of the original string cout<<"The size of str1 string is: "<::size_type Size2Str1; basic_string ::size_type Capa2Str1; Size2Str1 = str1.size(); Capa2Str1 = str1.capacity(); cout<<"str1 with downsized capacity is: "< #include using namespace std; int main() { string str1("Testing the resize()"); cout<<"str1 string is: "<::size_type SizeStr1; SizeStr1 = str1.size(); basic_string ::size_type CapaStr1; CapaStr1 = str1.capacity(); //Compare size & capacity of the original string cout<<"The size of str1 string is: "< #include using namespace std; int main() { //searching for a single character in a string string str1("Testing the rfind() 1..2..3"); cout<<"str1 string is: "<::size_type index1, index2; static const basic_string ::size_type npos = -1; cout<<"Operation: str1.rfind('i', 18)"<::size_type index3, index4; const char *cstr1 = "find"; cout<<"Operation: str2.rfind(cstr1, 25)"< #include using namespace std; int main() { //searching a string for a substring as specified by a C-string string str3("Another test. Testing the rfind() the 123"); cout<<"The str3 string is: "<::size_type npos = -1; basic_string ::size_type index5, index6; const char *cstr3 = "test"; cout<<"Operation: str3.rfind(cstr3)"<::size_type index7, index8; string str5("2...3"); cout<<"Operation: str4.rfind(str5, 30)"< #include using namespace std; int main() { string str1("Testing the substr()"); cout<<"str1 string is: "< str2 = str1.substr(4, 7); cout<<"The substring str1 copied is: "< str3 = str1.substr(); cout<<"The default str3 substring is: "< #include using namespace std; int main() { //assigning a character value to another character char chr1 = 'P'; const char chr2 = 'Q'; cout<<"The initial characters (chr1, chr2) are: ("<::assign(chr1, chr2); cout<<"The new characters (chr1, chr2) are: ("<::char_type* str1 = "Testing assign()"; char_traits::char_type* result; cout<<"\nThe target string str1 is: "<::assign(str1, 5, '#'); cout<<"The result = "< #include using namespace std; int main() { char_traits::char_type* str1 = "TEST"; char_traits::char_type* str2 = "RETEST"; char_traits::char_type* str3 = "RETEST"; char_traits::char_type* str4 = "TESTING"; cout<<"str1 string is: "<::compare(str1, str2, 2); comp2 = char_traits::compare(str2, str3, 3); comp3 = char_traits::compare(str3, str4, 4); comp4 = char_traits::compare(str4, str3, 4); cout<<"compare(str1, str2, 2) = "<::char_type* str2 = "Fucking"; char_traits::char_type* result; cout<<"The str1, source string is: "<::copy(str1, str2, 7); cout<<"The result is: "< #include using namespace std; int main( ) { char_traits ::int_type int0 = char_traits::eof(); cout<<"The eof return is: "<::char_type chs = 'R'; char_traits::int_type int1; int1 =char_traits::to_int_type(chs); cout<<"char_type chs "<::int_type int2 = char_traits::eof(); cout<<"The eof return is: "< #include using namespace std; int main() { char_traits::char_type chr1 = 'P'; char_traits::char_type chr2 = 'Q'; char_traits::char_type chr3 = 'P'; //Testing for equality bool Var1 = char_traits::eq(chr1, chr2); cout<<"Operation: eq(chr1, chr2)"< #include using namespace std; int main() { char_traits::char_type chr1 = 'P'; char_traits::char_type chr2 = 'Q'; char_traits::char_type chr3 = 'P'; char_traits::char_type chr4 = 'r'; //char_type to int_type conversion char_traits::int_type int1, int2, int3, int4; int1 = char_traits::to_int_type(chr1); int2 = char_traits::to_int_type(chr2); int3 = char_traits::to_int_type(chr3); int4 = char_traits::to_int_type(chr4); cout<<"Operation: to_int_type(character)"<::eq_int_type(int1, int2); if(var1) cout<<"The int_type representation of characters chr1\n" <<"and chr2 is equal."< #include using namespace std; int main( ) { const char* str = "Testing the char_traits, find()"; const char* result1; cout<<"The string to be searched is:\n"<::find(str, 20, 'a'); cout<<"Searching character \'"<<*result1<<"\'."<::find(str, 20, 'z'); cout<<"\nOperation: find(str, 20, 'z')"< #include using namespace std; int main() { const char* str1= "Testing 1...2...3"; cout<<"str1 C-string is: "<::length(str1); cout<<"The length of str1 is: "< #include using namespace std; int main() { char_traits::char_type chr1 = '1'; char_traits::char_type chr2 = 'q'; char_traits::char_type chr3 = 'R'; char_traits::int_type int1, int2, int3; int1 = char_traits::to_int_type(chr1); int2 = char_traits::to_int_type(chr2); int3 = char_traits::to_int_type(chr3); //char_type to int_type conversion, for testing cout<<"chr1 = "<::lt(chr1, chr2); if(var1) cout<<"The chr1 is less than " <<"the chr2."< #include using namespace std; int main() { char_traits::char_type str1[25] = "The Hell Boy"; char_traits::char_type str2[25] = "Something To ponder"; char_traits::char_type *result1; cout<<"The source str1 string is: "<::move(str2, str1, 10); cout<<"\nOperation: move(str2, str1, 10)"<::char_type *result2; cout << "The source/destination str3 string is: "<::find(str3, 12, 'h'); cout<<"Operation: move(str3, str4, 9)"<::move(str3, str4, 9); cout<<"The result2 = "< #include using namespace std; int main() { char_traits::char_type chr1 = 'w'; char_traits::int_type int1; int1 = char_traits::to_int_type(chr1); cout<<"Operation: to_int_type(chr1)"<::int_type int2 = char_traits::eof(); cout<<"\nOperation: char_traits::eof()"<::int_type eofTest1, eofTest2; eofTest1 = char_traits::not_eof(int1); cout<<"\nOperation: not_eof(int1)"<::to_char_type(eofTest1)<::not_eof(int2); cout<<"\nOperation: not_eof(int2)"<::to_char_type(eofTest2)< #include using namespace std; int main() { char_traits::char_type chr1 = '3'; char_traits::char_type chr2 = 'C'; char_traits::char_type chr3 = '#'; cout<<"chr1 = "< "< "< "<::char_type rev_chr1; rev_chr1 = char_traits::to_char_type(int1); char_traits::char_type rev_chr2; rev_chr2 = char_traits::to_char_type(int2); cout<<"\nOperation: to_char_type(integer)"< "< "<::eq(rev_chr1, chr1); if(var1) cout<<"The rev_chr1 is equal to the original chr1."< #include 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 #include 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; } ------------------------------------------G++---------------------------------------------------------- //**********string2.cpp************ //insert() part I #include #include using namespace std; int main() { //inserting a C-string at a given position basic_string str1("e insert() testing"); const char *cstr1 = "Th"; cout<<"str1 = "< str2("Test"); const char *cstr2 = "ing an insert()"; cout<<"str2 = "< str3(" the insert()"); string str4("Testing"); cout<<"str3 = "< str5("Testing "); string str6(" the insert()"); cout<<"str5 = "<