Character string strings-related code in Chapter 11th of C ++ standard library: self-repair tutorials and Reference Manual

Source: Internet
Author: User

Header files and namespaces involved:

#include <string>#include <cctype>#include <locale>#include <iostream>#include <iterator>#include <algorithm>using namespace std;
Int main (INT argc, char * argv []) {string filename, basename, extname, tmpname; const string suffix ("tmp"); For (INT I = 1; I <argc; I ++) {filename = argv [I]; string: size_type idx = filename. find ('. '); If (idx = string: NPOs) // {tmpname = filename +' is not found '. '+ suffix;} else {basename = filename. substr (0, idx); // basic name extname = filename. substr (idx + 1); // If (extname. empty () // The extension is null {tmpname = filename; tmpname + = suffix;} else if (extname = suffix) {tmpname = filename; tmpname. replace (idx + 1, extname. size (), "XXX"); // Replace the extension} else {tmpname = filename; tmpname. replace (idx + 1, string: NPOs, suffix) ;}} cout <FILENAME <"=>" <tmpname <Endl;} return 0 ;}
Int main (INT argc, char * argv []) {const string delims ("\ t ,.; "); // tab comma-ending semicolon string inline; while (Getline (CIN, inline) // wait for the input {string: size_type begidx, endidx; begidx = inline. find_first_not_of (delims); // search for the index of the first character of a word while (begidx! = String: NPOs) // check successful {endidx = inline. find_first_of (delims, begidx); // end position of the word if (endidx = string: NPOs) // {endidx = inline. length () ;}for (INT I = endidx-1; I >= static_cast <int> (begidx); -- I) // reverse output {cout <inline [I];} cout <''; begidx = inline. find_first_not_of (delims, endidx); // find the start position of the next word} cout <Endl;} return 0 ;}
Int main (INT argc, char * argv []) {string STR ("the zip code hondelage in Germany is 38108"); cout <"Original: "<STR <Endl; transform (Str. begin (), str. end (), str. begin (), tolower); cout <"lowercase:" <STR <Endl; transform (Str. begin (), str. end (), str. begin (), toupper); cout <"Capital:" <STR <Endl; return 0 ;}
Int main (INT argc, char * argv []) {const string Hello ("Hello, how are you? "); String S (hello. Begin (), hello. End (); string: iterator Pos; For (Pos = S. Begin (); pos! = S. end (); ++ POS) {cout <* Pos;} cout <Endl; reverse (S. begin (), S. end (); cout <"after reversal:" <S <Endl; sort (S. begin (), S. end (); cout <"after sorting:" <S <Endl; S. erase (unique (S. begin (), S. end (), S. end (); // Delete the cout <"deduplication:" <S <Endl; return 0 ;}
class BothWhiteSpaces{private: const locale& Loc;public: BothWhiteSpaces(const locale& l):Loc(l){} bool operator()(char Elem1,char Elem2) {  return isspace(Elem1,Loc) && isspace(Elem2,Loc); }};int main(int argc, char* argv[]){ string Contents; cin.unsetf(ios::skipws); unique_copy(istream_iterator<char>(cin),istream_iterator<char>(),back_inserter(Contents),BothWhiteSpaces(cin.getloc())); cout<<Contents; return 0;}
struct IgnoreTraits : public std::char_traits<char>{ static bool Equal(const char& C1,const char& C2) {  return std::toupper(C1) == std::toupper(C2); } static bool LessThan(const char& C1,const char& C2) {  return std::toupper(C1) < std::toupper(C2); } static int compare(const char* S1,const char* S2,std::size_t n) {  for(std::size_t i=0;i<n;i++)  {   if(!Equal(S1[i],S2[i]))    return LessThan(S1[i],S2[i]) ? -1 : 1;  }  return 0; } static const char* find(const char* S,std::size_t N,const char& C) {  for (std::size_t i=0;i<N;i++)  {   if(Equal(S[i],C))   {    return &(S[i]);   }  }  return 0; }};typedef std::basic_string<char,IgnoreTraits> iCString;inline std::ostream& operator << (std::ostream& Strm,const iCString& S){ return Strm<<std::string(S.data(),S.length());}int main(int argc, char* argv[]){ using std::cout; using std::endl; iCString S1("Hallo"); iCString S2("Ottoo"); iCString S3("HALLo"); cout<<std::boolalpha; cout<<S1<<" == "<<S2<<" : "<<(S1 == S2)<<endl; cout<<S1<<" == "<<S3<<" : "<<(S1 == S3)<<endl; iCString::size_type Index = S1.find("All"); if(Index != iCString::npos) {  cout<<"Index of \"All\" in : "<<Index<<endl; } else {  cout<<"\"All\" not found in \""<<S1<<"\""<< endl; } return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.