/* The following code example is taken from the book * "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */ #if 0 ////////// string/string1.cpp p472 #include #include using namespace std; int main (int argc, char* argv[]) { string filename, basename, extname, tmpname; const string suffix("tmp"); /* for each command-line argument * (which is an ordinary C-string) */ for (int i=1; i " << tmpname << endl; } return 0; } // D:\projects\StdLib\Debug> stdlib prog.dat mydir hello. oops.tmp end.dat // prog.dat => prog.tmp // mydir => mydir.tmp // hello. => hello.tmp // oops.tmp => oops.xxx // end.dat => end.tmp ////////// string/string2.cpp p476 #include #include using namespace std; int main (int argc, char** argv) { const string delims(" \t,.;"); string line; // for every line read successfully while (getline(cin,line)) { string::size_type begIdx, endIdx; // search beginning of the first word begIdx = line.find_first_not_of(delims); // while beginning of a word found while (begIdx != string::npos) { // search end of the actual word endIdx = line.find_first_of (delims, begIdx); if (endIdx == string::npos) { // end of word is end of line endIdx = line.length(); } // print characters in reverse order for (int i=endIdx-1; i>=static_cast(begIdx); --i) { cout << line[i]; } cout << ' '; // search beginning of the next word begIdx = line.find_first_not_of (delims, endIdx); } cout << endl; } return 0; } // pots & pans, I saw a reed. // ^Z // stop & snap I was a deer // ^Z ////////// string/iter1.cpp p497 #include #include #include #include using namespace std; int main() { // create a string string s("The zip code of Hondelage in Germany is 38108"); cout << "original: " << s << endl; // lowercase all characters transform (s.begin(), s.end(), // source s.begin(), // destination tolower); // operation cout << "lowered: " << s << endl; // uppercase all characters transform (s.begin(), s.end(), // source s.begin(), // destination toupper); // operation cout << "uppered: " << s << endl; return 0; } // original: The zip code of Hondelage in Germany is 38108 // lowered: the zip code of hondelage in germany is 38108 // uppered: THE ZIP CODE OF HONDELAGE IN GERMANY IS 38108 ////////// string/iter2.cpp p499 #include #include #include using namespace std; bool nocase_compare (char c1, char c2) { return toupper(c1) == toupper(c2); } int main() { string s1("This is a string"); string s2("STRING"); // compare case insensitive if (s1.size() == s2.size() && // ensure same sizes equal (s1.begin(),s1.end(), // first source string s2.begin(), // second source string nocase_compare)) { // comparison criterion cout << "the strings are equal" << endl; } else { cout << "the strings are not equal" << endl; } // search case insensitive string::iterator pos; pos = search (s1.begin(),s1.end(), // source string in which to search s2.begin(),s2.end(), // substring to search nocase_compare); // comparison criterion if (pos == s1.end()) { cout << "s2 is not a substring of s1" << endl; } else { cout << '"' << s2 << "\" is a substring of \"" << s1 << "\" (at index " << pos - s1.begin() << ")" << endl; } return 0; } // the strings are not equal // "STRING" is a substring of "This is a string" (at index 10) ////////// string/iter3.cpp p501 #include #include #include using namespace std; int main() { // create constant string const string hello("Hello, how are you?"); // initialize string s with all characters of string hello string s(hello.begin(),hello.end()); // iterate through all of the characters string::iterator pos; for (pos = s.begin(); pos != s.end(); ++pos) { cout << *pos; } cout << endl; // reverse the order of all characters inside the string reverse (s.begin(), s.end()); cout << "reverse:: " << s << endl; // sort all characters inside the string sort (s.begin(), s.end()); cout << "ordered:: " << s << endl; /* remove adjacent duplicates * - unique() reorders and returns new end * - erase() shrinks accordingly */ s.erase (unique(s.begin(), s.end()), s.end()); cout << "no dupls: " << s << endl; return 0; } // Hello, how are you? // reverse:: ?uoy era woh ,olleH // ordered:: ,?Haeehlloooruwy // no dupls: ,?Haehloruwy ////////// string/unique.cpp p502 #include #include #include #include #include using namespace std; class bothWhiteSpaces { private: const locale& loc; // locale public: /* constructor * - save the locale object */ bothWhiteSpaces (const locale& l) : loc(l) { } /* function call * - returns whether both characters are whitespaces */ bool operator() (char elem1, char elem2) { return isspace(elem1,loc) && isspace(elem2,loc); } }; int main() { vector contents; // don't skip leading whitespaces cin.unsetf (ios::skipws); // read all characters while compressing whitespaces unique_copy(istream_iterator(cin), // beginning of source istream_iterator(), // end of source back_inserter(contents), // destination bothWhiteSpaces(cin.getloc())); // criterion for removing // process contents // - here: write it to the standard output string str( contents.begin(), contents.end() ); cout << str; return 0; } // 123 abc 456 def // ^Z // 123 abc 456 def ////////// string/icstring1.cpp p505 /********* #include "icstring.hpp" *********/ #include #include #include using namespace std; /* replace functions of the standard char_traits * so that strings behave in a case-insensitive way */ struct ignorecase_traits : public std::char_traits { // return whether c1 and c2 are equal static bool eq(const char& c1, const char& c2) { return toupper(c1)==toupper(c2); } // return whether c1 is less than c2 static bool lt(const char& c1, const char& c2) { return toupper(c1) < toupper(c2); } // compare up to n characters of s1 and s2 static int compare(const char* s1, const char* s2, string::size_type n) { for (string::size_type i=0; i icstring; /* define an output operator * because the traits type is different than that for std::ostream */ inline std::ostream& operator << (std::ostream& strm, const icstring& s) { // simply convert the icstring into a normal string return strm << std::string(s.data(),s.length()); } /********* #include "icstring.hpp" *********/ int main() { using std::cout; using std::endl; icstring s1("hallo"); icstring s2("otto"); icstring s3("hALLo"); cout << std::boolalpha; cout << s1 << " == " << s2 << " : " << (s1==s2) << endl; cout << s1 << " == " << s3 << " : " << (s1==s3) << endl; icstring::size_type idx = s1.find("All"); if (idx != icstring::npos) { cout << "index of \"All\" in \"" << s1 << "\": " << idx << endl; } else { cout << "\"All\" not found in \"" << s1 << endl; } return 0; } // hallo == otto : false // hallo == hALLo : true // index of "All" in "hallo": 1 #endif