涉及到的標頭檔和命名空間:
#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;}