《C++標準程式庫:自修教程與參考手冊》第十一章之字串Strings相關代碼

來源:互聯網
上載者:User

涉及到的標頭檔和命名空間:

#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 + '.' + Suffix;  }  else  {   BaseName = FileName.substr(0,idx);  //基本名   ExtName  = FileName.substr(idx + 1);//副檔名   if(ExtName.empty())//副檔名為空白   {    TmpName  = FileName;    TmpName += Suffix;   }   else if(ExtName == Suffix)   {    TmpName = FileName;    TmpName.replace(idx+1,ExtName.size(),"XXX");//替換副檔名   }   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 逗號 句號 分號 string InLine; while (getline(cin,InLine))//等待輸入 {  string::size_type BegIdx,EndIdx;  BegIdx = InLine.find_first_not_of(Delims);//尋找一個單詞的的第一個字元的索引  while (BegIdx != string::npos)//查到成功  {   EndIdx = InLine.find_first_of(Delims,BegIdx);//單詞的結束位置   if (EndIdx == string::npos)//沒有找到   {    EndIdx = InLine.length();   }   for(int i = EndIdx -1; i >= static_cast<int>(BegIdx);--i)//反向輸出   {    cout<<InLine[i];   }   cout<<' ';   BegIdx = InLine.find_first_not_of(Delims,EndIdx);//尋找下一個單詞的開始位置  }  cout<<endl; } return 0;}
int main(int argc, char* argv[]){ string Str("The zip code Hondelage in Germany is 38108"); cout<<"原始:"<<Str<<endl; transform(Str.begin(),Str.end(),Str.begin(),tolower); cout<<"小寫:"<<Str<<endl; transform(Str.begin(),Str.end(),Str.begin(),toupper); cout<<"大寫:"<<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<<"反轉後:"<<S<<endl; sort(S.begin(),S.end()); cout<<"排序後:"<<S<<endl; S.erase(unique(S.begin(),S.end()),S.end());//刪除連續重複的值 cout<<"去重後:"<<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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.