============================Module25======================================= | | | 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++. Examples compiled using VC++/VC++ .Net and gcc or 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========================== //char and wchar_t types #include #include using namespace std; int main( ) { const basic_string str1("TEST"); //Uses the typedef for string word //synonym to basic_string string str2("TEST"); //simple comparison between two //objects of type basic_string cout<<"String str1: "< str3(L"TESTING"); //Uses the typedef for wstring word //synonym to basic_string wstring str4(L"JUMPING"); //simple comparison between two objects //of type basic_string cout<<"\nString str3: TESTING \nString str4: JUMPING"< #include using namespace std; int main( ) { //Declaring an objects of type basic_string string str1("testingone"); string str2("testingtwo"); cout<<"str1 string is = "< operators #include #include using namespace std; int main() { //Declaring objects of type basic_string string str1("testingthree"); string str2("testingtwo"); cout<<"str1 is = "< str2)"< str2) cout<<"str1 is greater then string str2."< str2)"< str2) cout<<"str3 is greater then string str2."< str3)"< str3) cout<<"str1 is greater then string str3."<= and <= operators #include #include using namespace std; int main( ) { //Declaring an objects of type basic_string string str1("testingone"); string str2("testingtwo"); cout<<"str1 string is = "<= str2)"<= str2) cout<<"str1 is greater than or equal to str2."<= str2)"<= str2) cout<<"str3 is greater than or equal to str2."<= str3)"<= str3) cout<<"str1 is greater than or equal to str3."<> operators #include #include using namespace std; int main() { string Sample = "Testing the << and >> operators."; string Var1, Var2; cout<>Var1; cout<<"Enter another string or a word: "; cin>>Var2; cout<<"The strings entered are: "< #include using namespace std; int main() { //Declaring an object of type basic_string string str1("StringOne"); string str2("StringTwo"); //Declaring a C-style string char *str3 = "StringThree"; //Declaring a character constant char chr = '?'; cout<<"str1 string is = "< as the delimiter: "< #include using namespace std; int main() { //appending a C-string to a string string str1("Playing "); const char *str2 = "with a string"; cout<<"str1 is: "< #include 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: "< #include using namespace std; int main() { string str1("Operation"), str2("Desert Storm"); const string constr1("Making cakes"), constr2("Start eating"); cout<<"str1 string is: "<::reference RefStr1 = str1[4]; basic_string ::reference RefStr2 = str2.at(7); //index starts from 0 lor! cout<<"\n---str1[5]---"<::const_reference cRefStr1 = constr1[constr1.length()]; basic_string ::const_reference cRefStr2 = constr2.at(8); cout<<"---constr1.length()---"< #include using namespace std; int main() { //initializing with a C-string const char *str1 = "The basic_string"; basic_string str2(str1, 5); cout<<"str1 string is: "< str4(str3, 6, 10); cout<<"Operation: str4(str3, 6, 10)"< str5(6, '7'); cout<<"Operation: str5(6, '7')"< str6; string str7; //allocate a storage basic_string str8(str7.get_allocator()); //test the emptiness cout<<"Operation: str8.empty()"< str11(str10.begin()+5, str10.end()); cout<<"str11 initialized by another range of characters is: \n"< #include using namespace std; int main() { string Str1("Testing the begin() and end()"), Str2; basic_string ::iterator Str1Iter; //const_iterator… basic_string ::const_iterator Str1CIter; //...an error because the iterator Str1CIter is const //*Str1CIter = 'Z'; cout<<"String Str1 is: "< #include using namespace std; int main( ) { string str1("Testing the c_str"); cout<<"The original string object str1 is: "<::size_type SizeStr1, LenStr1; SizeStr1 = str1.size(); LenStr1 = str1.length(); basic_string ::size_type CapStr1, MaxSizeStr1; CapStr1 = str1.capacity(); MaxSizeStr1 = str1.max_size(); //Compare size, length, capacity & max_size of a string cout<<"\nOperation: str1.size()"< #include using namespace std; int main( ) { string str1("Testing the clear()"); basic_string ::iterator StrIter; //Normal... cout<<"str1 string is :"< #include 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: "< #include 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: "< #include using namespace std; int main() { string str1("Testing the copy()"); basic_string ::iterator StrIter; //declare and initialize arrays to 0 char Array1[20] = {0}; char Array2[10] = {0}; basic_string :: pointer Array1Ptr = Array1; basic_string :: value_type *Array2Ptr = Array2; //iterate character by character... cout<<"str1 string is: "; for(StrIter = str1.begin(); StrIter != str1.end(); StrIter++) cout<<*StrIter; cout<::size_type NewArray1; NewArray1 = str1.copy(Array1Ptr, 18); cout<<"Operation: str1.copy(Array1Ptr, 18)"<::size_type NewArray2; NewArray2 = str1.copy(Array2Ptr, 9, 2); cout<<"Operation: str1.copy(Array2Ptr, 9, 2)"< #include using namespace std; int main() { string str1("Testing the data()"); cout<<"str1 string object is: "< #include using namespace std; int main( ) { string str1("Testing the end()"); basic_string ::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--"< #include using namespace std; int main() { //using a range... string str1("Testing the erase() part I"); basic_string ::iterator Str1Iter; cout<<"str1 string object is: "<< str1<::iterator Str2Iter; cout<<"\nstr2 string object is: "< #include 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: "<::size_type index1, index2; static const basic_string ::size_type npos = -1; index1 = str1.find('r', 2); cout<<"Operation: str1.find('r', 2)"<::size_type index3, index4; const char *cstr1 = "sub"; index3 = str2.find(cstr1, 5); cout<<"Operation: str2.find(cstr1, 5)"< #include 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 ::size_type npos = -1; string str3("Again, search part III"); cout<<"str3 string is: "<::size_type index5, index6; const char *cstr3 = "part"; index5 = str3.find(cstr3); cout<<"Operation: str3.find(cstr3)"<::size_type index7, index8; string str5("part"); index7 = str4.find(str5, 4); cout<<"Operation: str4.find(str5, 4)"< #include 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: "<::size_type index1, index2; static const basic_string ::size_type npos = -1; index1 = str1.find_first_not_of('_', 3); cout<<"Operation: str1.find_first_not_of('_', 3)"<::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)"< #include 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: "<::size_type index5, index6; static const basic_string ::size_type npos = -1; const char *cstr4 = "nro"; index5 = str3.find_first_not_of(cstr4); cout<<"Operation: str3.find_first_not_of(cstr4)"<::size_type index7, index8; string str5("tf7"); index7 = str4.find_first_not_of(str5, 3); cout<<"Operation: str4.find_first_not_of(str5, 3)"< #include using namespace std; int main( ) { //searching for a single character in a string string str1("find_first_of()"); cout<<"str1 string is: "<::size_type index1, index2; static const basic_string ::size_type npos = -1; index1 = str1.find_first_of('r', 3); cout<<"Operation: str1.find_first_of('r', 3)"<::size_type index3, index4; const char *cstr = "s1"; index3 = str2.find_first_of(cstr, 3); cout<<"Operation: str2.find_first_of(cstr, 3)"< #include 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: "<::size_type index5, index6; static const basic_string ::size_type npos = -1; const char *cstr2 = "t6"; index5 = str3.find_first_of(cstr2); cout<<"Operation: str3.find_first_of(cstr2)"<::size_type index7, index8; string str5("dfz"); index7 = str5.find_first_of(str5, 3); cout<<"Operation: str5.find_first_of(str5, 3)"< #include using namespace std; int main() { //appending a C-string to a string string str1("Playing "); const char *str2 = "with a string"; cout<<"str1 is: "<