#include <string>
1.取当中某个字符
与传统一样 c[11]="0123456789"; c[1]=1; ps:好慢 。。 会不会GG。。。
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 int main() 5 { 6 string s("0123456789"); 7 cout<<s<<endl; 8 cout<<s[1]<<endl;//也是从0位开始 9 return 0; 10 }
2.一些比较函数
① 重载运算符 <,>,<=,>=,==
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 int main() 5 {//得出 重载符 比较的是字典序 不是长度 6 string ss[2]; 7 while(cin>>ss[0]>>ss[1]) 8 { 9 cout<<ss[0]<<" "<<ss[1]<<endl; 10 cout<<" < : "<<(ss[0]<ss[1])<<endl; 11 cout<<" > : "<<(ss[0]>ss[1])<<endl; 12 cout<<" <= : "<<(ss[0]<=ss[1])<<endl; 13 cout<<" >= : "<<(ss[0]>=ss[1])<<endl; 14 cout<<" == : "<<(ss[0]==ss[1])<<endl; 15 } 16 return 0; 17 }
3.一些查找函数
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char *s,int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s,int pos = 0) const;//从pos开始查找字符串s在当前串中的位置 //查找成功时返回所在位置,失败返回string::npos的值
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 int main() 5 { 6 string ss="abcdefghijklmnopqrstuvswxyz"; 7 8 cout<<ss.find('j')<<endl; 9 cout<<ss.find("st")<<endl; 10 cout<<ss.find("sw")<<endl; 11 cout<<ss.find("sw",0,1)<<endl; 12 return 0; 13 //9 18 22 18 14 }
待续:2017-03-14 22:56:21