说明:代码均省略其输出验证步骤
[1] 赋值语句
#include<iostream> #include<map> #include<string> #include<utility> #include<vector> #include<string> using namespace std; int main() { //string与c语言的\0表示结尾不一样 string a1(10,'a'); cout<<a1<<endl; string a2(10,'\0'); cout<<a2.size()<<endl;//size()和length()一样长 string a3="222"; //不能string a3='2'; string a6("nihao",20);//表示长度是20,后面接上\0 string a7({'x','y','z'}); cout<<a7<<endl; return 0; }
[2]简单介绍,string与printf的协作,与字符数组的区别
#include<iostream> #include<map> #include<string> #include<utility> #include<vector> #include<string> using namespace std; int main() { //string与printf的协作,.c_str()自动帮忙添上\0 string a1="nihao"; printf("Guten Morgen! %s\n",a1.c_str()); char v[100]; a1.copy(v,100); char v1[100]; a1.copy(v1,100,2);//从下标为2的字符开始拷贝 cout<<v<<endl;//会出现乱码 //不会有乱码了 v[a1.size()]='\0'; cout<<v<<endl; return 0; }
[3]赋值语句
#include<iostream> #include<map> #include<string> #include<utility> #include<vector> #include<string> #include<cstring> using namespace std; int main() { string a1; string a2("nihaodajiahao"); string a3(a2,2);//从a2的第二个字符向后面取 string a4(a2,2,4); string a5(a4); string a6("everybodydancewiththedeath",2); //去前两个字符:ev,是c语言的字符串,要有所区分 string a7(20,'x');//20个x char v[]="nihao"; int n=strlen(v); string a9(v,v+n); string a10(a2.begin(),a2.begin()+2); string a11{'x','x','c'}; //a11.empty()也一样用 //string的大小按照字典序大小比较 return 0; }
[4]对字符串中字符的访问
#include<iostream> #include<map> #include<string> #include<utility> #include<vector> #include<string> #include<cstring> using namespace std; int main() { //字符串的大小比较 string a1="hello"; string a2="Nihao"; //比较满足: cout<<(a1<"zzz")<<endl; cout<<("zzz"<a1)<<endl; cout<<(a1<a2)<<endl; //不能这么比: cout<<("hfjha"<"jfakj")<<endl; //另一种比较方法: cout<<a1.compare(a2)<<endl;//大于0则a1>a2 cout<<a1.compare(1,3,a2)<<endl;//从1位向后取三个字符 cout<<a1.compare(1,3,a2,1,2)<<endl; cout<<a1.compare("hello")<<endl;//也可以直接用原生字符串 return 0; }
[5]字符串的访问
#include<iostream> #include<map> #include<string> #include<utility> #include<vector> #include<string> #include<cstring> using namespace std; int main() { //字符串的访问 string a1="nihaodajiahao"; cout<<a1[4]<<endl;//或者cout<<a1.at(3)<<endl; //全部转换成大写 for(int i=0;i<a1.size();i++) { a1[i]=toupper(a1[i]); } //访问第一个和最后一个 cout<<a1.front()<<endl; cout<<a1.back()<<endl; //使用引用或者指针 char &p=a1[0];//引用 p='X'; char &q=a1.back();//引用 q='L'; //还有a1.copy()操作 return 0; }
[6].assign() .swap()
#include<iostream> #include<map> #include<string> #include<utility> #include<vector> #include<string> #include<cstring> using namespace std; int main() { //字符串的修改 string a1="nihaodajiahao"; a1="nihao"; //或者: a1.assign("miaomiaomiao"); string a2; a2.assign(a1,3,4);//从3开始向后取4个 cout<<a2<<endl; a2.assign("aaa",100); cout<<a2.size()<<endl; //交换 a1.swap(a2);//成员函数 swap(a1,a2);//算法 return 0; }
[7].insert()
#include<iostream> #include<string> using namespace std; int main() { //插入 .insert() string a1="abcdefg"; string b1="xxxcc"; a1.insert(2,b1); a1="abcdefg"; a1.insert(3,b1,1,3); a1="abcdefg"; a1.insert(3,"xxx"); a1="abcdefg"; a1.insert(3,"xxx",2); //可以用迭代子a1.insert(p1,'x'); //可以用迭代子a1.insert(p1,p2,p3);p2、p3属于b //可以用迭代子a1.insert(p1,{'x','y'));p2、p3属于b return 0; }
[8]清除 .erase(), .clear() .resize(), .pop_back()
#include<iostream> #include<string> using namespace std; int main() { string a1="12345678"; a1.clear();//清空 //等价a1.erase(); a1="12345678"; a1.erase(2,3); cout<<a1<<endl; //a1.erase(p1); //a1.erase(p2,p3); //a1.pop_back();C++11 //a1.resize(); return 0; }
[9]替换
#include<iostream> #include<string> using namespace std; int main() { //替换 string a1="12345678"; string b1="XXXXXX"; a1.replace(2,3,b1);//2-3位替换为b1 a1="12345678"; a1.replace(2,3,b1,2,1); //以上的数字均可以替换为迭代子 a1="12345678"; a1.replace(2,3,10,'x');//10个x return 0; }
[10]查找
#include<iostream> #include<string> using namespace std; int main() { //查找 .find() string a1="123456786"; int i1=a1.find('6'); cout<<i1<<endl; int i2=a1.find('6',i1+1);//从i+1之后向后查找 cout<<i2<<endl; //显示所有的h string s="hajfhnchdfjiefh"; int i=0; while(i<s.size()) { int t=s.find('h',i); cout<<t<<" "; i=t+1; } //逆向查找 cout<<s.rfind('h'); return 0; }
[11]集合的查找.find_first_of() .find_last_of()
#include<iostream> #include<string> using namespace std; int main() { //find_first_of 在集合中查找 string a1="abcdefghijklmn";//a1看成是字符串 string b1="xzghn";//b1当成集合看 int i1=a1.find_first_of(b1); cout<<a1[i1]<<endl; i1=a1.find_first_not_of(b1); cout<<i1<<endl; return 0; }
[12]拼接
#include<iostream> #include<string> #include<cstring> using namespace std; int main() { string a1 = "abcdefghijklmn"; string b1 = a1.substr(3); cout << b1 << endl; string b2 = a1.substr(3, 4); cout << b2 << endl; string a2 = "nihao"; string a3 = "dajiahao"; string b3 = a2 + a3; cout << b3 << endl; string b4 = 'X' + a2; cout << b4 << endl; string b5 = a2 + 'X'; cout << b5 << endl; return 0; }
[13]输入cin和getline
直接cin:[不能有空格]
#include<iostream> #include<string> #include<cstring> using namespace std; int main() { string a1; cin >> a1; cout << a1 << endl; //输入时不能有空格 system("pause"); return 0; }
getline:[可以有空格]
#include<iostream> #include<string> #include<cstring> using namespace std; int main() { string a1; getline(cin, a1); cout << a1 << endl;//输入时不能有空格 system("pause"); return 0; }
[14]数字与字符串之间的转换
数字转字符串
#include<iostream> #include<string> #include<cstring> using namespace std; //数字转字符串 直接用 to_string int main() { string a1 = to_string(118);//"118" cout << a1 << endl; a1 = to_string(123.4567); cout << a1 << endl;//"123.456700" a1.pop_back(); a1.pop_back(); cout << a1 << endl; system("pause"); return 0; }
字符串转数字(含进制的转化)
#include<iostream> #include<string> #include<cstring> using namespace std; int main() { //stoi s:string i:int int a1 = stoi("77"); cout << a1<< endl; a1 = stoi(" 77"); cout << a1 << endl; a1 = stoi(" 77xcvb"); cout << a1 << endl; std::size_t t; a1 = stoi(" 77xcvb", &t); cout << t << endl; a1 = stoi("77xcvb", &t);//转化不动时候的下标 cout << t << endl; a1 = stoi("1239", nullptr, 8);//8进制的123转化为十进制为83(没有9) cout << a1 << endl; a1 = stoi("1239", &t, 8); cout << t << endl;//显示地址3 cout << a1 << endl;//83 return 0; }
#include<iostream> #include<string> #include<cstring> #include<limits> using namespace std; //数字转字符串 直接用 to_string int main() { // stoi string to int // stol string to long // stoul string to unsigned long // stoll string to long long // stoull string to unsigned long long // stof string to float // stod string to double // stold string to long double std::size_t idx; cout << stoi(" 42 is the truth",&idx)<<endl; cout << " idx of first unprocessed char: " << idx << endl; cout << stoi(" 42", nullptr, 16) << endl; cout << stol("789", &idx, 8) << endl; cout << " idx of first unprocessed char: " << idx << endl; long long ll = numeric_limits<long long>::max(); string sss = to_string(ll); cout << ll << endl; return 0; }