|
一、string基本概念: 1、本质 string是c++风格的字符串,而string本质上是一个类。 2、string和char *的区别 -char *是一个char类型的指针类型 -string是一个类,类内部封装了char*,是一个char型的容器。string类内部封装了很多成员函数,比如:查找find,拷贝copy,删除delete,替换replace,插入insert;string管理char所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责 二、string构造函数: 构造函数原型:
代码应用举例: #include <iostream>#include <string> using namespace std; void test() { string s1; const char *str = "txp"; string s2(str); cout << "s2= "<<s2<<endl; string s3(str); cout <<"s3= "<<s3<<endl; string s4(10,'a'); cout <<"s4= "<<s4<<endl; } int main() { test(); } 输出结果: root@txp-virtual-machine:/home/txp/test2# ./a.outs2= txp s3= txp s4= aaaaaaaaaa 三、string赋值操作: 实现string字符串赋值的函数类型:
代码应用: #include <iostream>#include <string> using namespace std; void test() { string str1; str1 = "txp"; cout<<"str1= "<<str1<<endl; string str2; str2=str1; cout<<"str2 = "<<str2<<endl; string str3; str3='a'; cout<<"str3= "<<str3<<endl; string str4; str4.assign("txp like cpp"); cout<<"str4= "<<str4<<endl; string str5; str5.assign("txp like cpp",5); cout<<"str5= "<<str5<<endl; string str6; str6.assign(str5); cout<<"str6= "<<str6<<endl; string str7; str7.assign(10,'w'); cout<<"str7= "<<str7<<endl; } int main() { test(); } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.outstr1= txp str2 = txp str3= a str4= txp like cpp str5= txp l str6= txp l str7= wwwwwwwwww 四、string字符串拼接: 实现字符串末尾拼接字符串函数原型:
代码应用: #include <iostream>#include <string> using namespace std; void test() { string str1 = "txp"; str1 += "xiaoping"; cout <<"str1= "<<str1<<endl; str1+=":"; cout<<"str1= "<<str1<<endl; string str2 = "cpp"; str1+=str2; cout<<"str1= "<<str1<<endl; string str3 = "I"; str3.append("love"); cout<<"str3= "<<str3<<endl; str3.append("game abcde",4); cout<<"str3= "<<str3<<endl; str3.append(str2); cout<<"str3= "<<str3<<endl; str3.append(str2,0,3); cout<<"str3= "<<str3<<endl; } int main() { test(); } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.outstr1= txpxiaoping str1= txpxiaoping: str1= txpxiaoping:cpp str3= Ilove str3= Ilovegame str3= Ilovegamecpp str3= Ilovegamecppcpp 五、string查找和替换: 实现字符串的查找和替换函数原型:
代码应用: #include <iostream>#include <string> using namespace std; void test() { string str1 = "abcd"; int pos = str1.find("bc"); if(pos) { cout<< " find the word"<<endl; } else { cout<<" not find the word"<<endl; } cout<<"pos= "<<pos<<endl; //rfind和find的区别:rfind从右往左查找,find从左往右查找 pos = str1.rfind("cd"); cout<<"pos= "<<pos<<endl; } void test1() { string str2 = "abcd"; str2.replace(1,3,"1111"); cout<<"str2= "<<str2<<endl; } int main() { test(); test1(); } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.outfind the word pos= 1 pos= 2 str2= a1111 六、string字符串比较: 比较方式:字符串比较是按字符的ASCII码进行对比的: = : 返回 0 > < : 返回 -1 函数原型:
代码应用: #include <iostream>#include <string> using namespace std; void test() { string str1 = "txp"; string str2 = "linux"; if(str1.compare(str2)==0) { cout<<"str1 = str2 "<<endl; } else if(str1.compare(str2)>0) { cout<<"str1 > str2 "<<endl; } else { cout<<" str1 < str2 "<<endl; } } int main() { test(); } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.outstr1 > str2 七、string字符存取: 实现字符存取函数原型:
代码应用: #include <iostream>#include <string> using namespace std; void test() { string str = "linux"; //通过[]访问单个字符 for(int i = 0;i< str.size();i++) { cout<<str<<" "; } cout<<endl; //通过at方式访问单个字符 for(int i =0; i<str.size();i++) { cout<<str.at(i)<<" "; } cout<<endl; //修改单个字符的方式 str[0] = 'x'; cout<<"str = "<<str<<endl; str.at(1) = 'x'; cout<<"str = "<<str<<endl; } int main() { test(); } 输出结果: root@txp-virtual-machine:/home/txp/test2# ./a.outl i n u x l i n u x str = xinux str = xxnux 八、string插入和删除: 实现string插入和删除的函数原型:
代码应用: #include <iostream>#include <string> using namespace std; void test() { string str = "linux"; str.insert(1,"111"); cout<<"str = "<<str<<endl; str.erase(1,3); cout<<"str = "<<str<<endl; } int main() { test(); } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.outstr = l111inux str = linux 九:string子串 实现从string字符串中获得子串的函数原型:
代码应用: #include <iostream>#include <string> using namespace std; void test() { string str = "linux"; string substr = str.substr(1,3); cout<<"substr = "<<substr<<endl; int pos = str.find('u'); string ur = str.substr(0,pos); cout<<ur<<endl; } int main() { test(); } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.outsubstr = inu lin |
微信公众号
手机版