string类本不是STL的容器,但是它与STL容器有着很多相似的操作,因此,把string放在这里一起进行介绍。
之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下的需要。我们尽可以把它看成是C++的基本数据类型。
string类的初始化:
string a1 = "aaaaaa"; string a2("bbbb"); string a3 = a2;//调用string类的拷贝构造函数 string a4(10, 'a'); // 十个a
string的遍历
#include<iostream> using namespace std; #include<string> void main21() { string a1 = "aaaaaa"; string a2("bbbb"); string a3 = a2;//调用string类的拷贝构造函数 string a4(10, 'a'); // 十个a cout << "a1 = " << a1 << endl; cout << "a2 = " << a2 << endl; cout << "a3 = " << a3 << endl; cout << "a4 = " << a4 << endl; } void main22()//字符串遍历 { string a1 = "abcdefg"; //数组方式: for (int i = 0; i < a1.length(); i++) { cout << a1[i] << " "; } cout << endl; //迭代器方式 for (string::iterator it = a1.begin(); it != a1.end(); it++) { cout << *it << " "; } cout << endl; cout << "at之后" << endl; try { for (int i = 0; i < a1.length() + 3; i++) { cout << a1.at(i) << " ";//at可以抛出异常 } } catch (...) { cout << "发生异常" << endl; } //try //{ // for (int i = 0; i < a1.length() + 3; i++) // { // cout << a1[i] << " ";//数组方式不能捕捉异常,程序会出现错误 // } //} //catch (...) //{ // cout << "发生异常" << endl; //} cout << endl; } int main() { main21(); main22(); system("pause"); return 0; }
字符指针和string的转换
//char*==>string string s1 = "aaabbbb"; //s1==>char* printf("s1:%s ", s1.c_str()); //s1的内容拷贝到 buf 中 char buf1[128] = { 0 }; s1.copy(buf1, 3, 0);//从第0个字符开始拷贝到第3个字符。最后没有加 cout << "buf1 = " << buf1 << endl;
string字符串连接 可以用重载的+和append
string s1 = "aaa"; string s2 = "bbb"; s1 = s1 + s2; cout << "s1: " << s1 << endl; string s3 = "444"; string s4 = "555"; s3.append(s4); cout << "s3: " << s3 << endl;
string的查找和替换
int find(char c,int pos = 0)const;//从pos开始查找字符c在当前字符串的位置 int find(const char*s,int pos = 0)const;//从pos开始查找字符串s在当前字符串的位置 int find(const string &s,int pos = 0)const;//从pos开始查找字符串s在当前字符串的位置,如果找不到就返回-1 int rfind(char *c,int pos = npos)const; int rfind(const string &s,int pos = nopos)const //rfind是反向查找的意思,如果找不到就返回-1
1 string &replace(int pos,int n,const char *s)//删除从pos开始的n个字符,然后在pos处插入串s 2 string &replace(int pos, int n, const string &s);//删除从pos开始的n个字符,然后在pos处插入串s 3 void swap(string &s2) //交换当前字符串与s2的值
案例:
string s1 = "wbm hellow wbm 111 wbm 222 wbm 333"; //查找第一次出现wbm的index int index = s1.find("wbm", 0);//数组下标从零开始 //cout << "index : " << index << endl; //案例1:求wbm出现的次数,每一次出现的数组下标 int offindex = s1.find("wbm", 0); while (offindex != string::npos)//string:npos是个特殊值,说明查找没有匹配 { cout << "offindex : " << offindex << endl; offindex = offindex + 1; offindex = s1.find("wbm", offindex); } //案例2:将小写的wbm换成大写的wbm offindex = s1.find("wbm", 0); while (offindex != string::npos) { cout << "offindex : " << offindex << endl; s1.replace(offindex, 3, "WBM"); offindex = offindex + 1; offindex = s1.find("wbm", offindex); } cout << "s1替换后的结果: " << s1 << endl;
string的区间删除(截断 )和插入
string &insert(int pos,const char *s); string &insert(int pos,const string &s); //前两个函数在pos位置插入字符串 string &erase(int pos = 0,int n = pos);//删除pos开始的n个字符,返回修改后的字符串
案例:
1 string s1 = "hello1 hello2 hello3"; 2 string::iterator it = find(s1.begin(), s1.end(), 'l'); 3 if (it != s1.end()) 4 { 5 s1.erase(it); 6 } 7 cout << "删除l以后的结果" << s1 << endl; 8 9 s1.erase(s1.begin(), s1.end()); 10 cout << "s1全部删除: " << s1 << endl; 11 cout << "s1长度 " << s1.length() << endl; 12 13 string s2 = "BBB"; 14 s2.insert(0, "AAA"); 15 s2.insert(s2.length(), "CCC"); 16 cout << s2 << endl;
大小写转换:
string s1 = "AAAbbb"; //1.函数的入口地址 2.函数对象 3.预定义的函数 transform(s1.begin(), s1.end(), s1.begin(), toupper);//转换为大写 cout << "s1 = " << s1 << endl; string s2 = "AAAbbb"; transform(s2.begin(), s2.end(), s2.begin(), tolower);//后面要将转换入口放到字符串开头 cout << "s2 = " << s2 << endl;
所有代码:
#include<iostream> using namespace std; #include<string> #include"algorithm" void main21() { string a1 = "aaaaaa"; string a2("bbbb"); string a3 = a2;//调用string类的拷贝构造函数 string a4(10, 'a'); // 十个a cout << "a1 = " << a1 << endl; cout << "a2 = " << a2 << endl; cout << "a3 = " << a3 << endl; cout << "a4 = " << a4 << endl; } void main22()//字符串遍历 { string a1 = "abcdefg"; //数组方式: for (int i = 0; i < a1.length(); i++) { cout << a1[i] << " "; } cout << endl; //迭代器方式 for (string::iterator it = a1.begin(); it != a1.end(); it++) { cout << *it << " "; } cout << endl; cout << "at之后" << endl; try { for (int i = 0; i < a1.length() + 3; i++) { cout << a1.at(i) << " ";//at可以抛出异常 } } catch (...) { cout << "发生异常" << endl; } //try //{ // for (int i = 0; i < a1.length() + 3; i++) // { // cout << a1[i] << " ";//数组方式不能捕捉异常,程序会出现错误 // } //} //catch (...) //{ // cout << "发生异常" << endl; //} cout << endl; } //字符指针和string的转换 void main23() { //char*==>string string s1 = "aaabbbb"; //s1==>char* printf("s1:%s ", s1.c_str()); //s1的内容拷贝到 buf 中 char buf1[128] = { 0 }; s1.copy(buf1, 3, 0);//从第0个字符开始拷贝到第3个字符。最后没有加 cout << "buf1 = " << buf1 << endl; } //string字符串连接 void main24() { string s1 = "aaa"; string s2 = "bbb"; s1 = s1 + s2; cout << "s1: " << s1 << endl; string s3 = "444"; string s4 = "555"; s3.append(s4); cout << "s3: " << s3 << endl; } //字符串的查找和替换 void main25() { string s1 = "wbm hellow wbm 111 wbm 222 wbm 333"; //查找第一次出现wbm的index int index = s1.find("wbm", 0);//数组下标从零开始 //cout << "index : " << index << endl; //案例1:求wbm出现的次数,每一次出现的数组下标 int offindex = s1.find("wbm", 0); while (offindex != string::npos)//string:npos是个特殊值,说明查找没有匹配 { cout << "offindex : " << offindex << endl; offindex = offindex + 1; offindex = s1.find("wbm", offindex); } //案例2:将小写的wbm换成大写的wbm offindex = s1.find("wbm", 0); while (offindex != string::npos) { cout << "offindex : " << offindex << endl; s1.replace(offindex, 3, "WBM"); offindex = offindex + 1; offindex = s1.find("wbm", offindex); } cout << "s1替换后的结果: " << s1 << endl; } //string 的区间删除(截断)和插入 void main26() { string s1 = "hello1 hello2 hello3"; string::iterator it = find(s1.begin(), s1.end(), 'l'); if (it != s1.end()) { s1.erase(it); } cout << "删除l以后的结果" << s1 << endl; s1.erase(s1.begin(), s1.end()); cout << "s1全部删除: " << s1 << endl; cout << "s1长度 " << s1.length() << endl; string s2 = "BBB"; s2.insert(0, "AAA"); s2.insert(s2.length(), "CCC"); cout << s2 << endl; } void main27() { string s1 = "AAAbbb"; //1.函数的入口地址 2.函数对象 3.预定义的函数 transform(s1.begin(), s1.end(), s1.begin(), toupper);//转换为大写 cout << "s1 = " << s1 << endl; string s2 = "AAAbbb"; transform(s2.begin(), s2.end(), s2.begin(), tolower);//后面要将转换入口放到字符串开头 cout << "s2 = " << s2 << endl; } int main() { //main21(); //main22(); //main23(); //main24(); //main25(); //main26(); main27(); system("pause"); return 0; }