1、C/C++ 字符串区别
1.C++的字符串string,本质是:typedef basic_string<char> string;
2.C++中的字符串string与C中的字符串const char *的区别:
1) C中的字符串const char *是指针,C++中的字符串string是类.本质是:typedef basic_string<char> string;
2) C中的字符串必须有结束符'\0',C++的字符串没有结束符'\0',可以通过string类的size访问字符串的所有内容
2、string类的构造函数
const char *src = "hello world";string str1("hello world"); //1.用C字符串构造stringstring str2(str1,6,5); //2.用string的部分构造string: 起始下标 + 长度string str3(src + 6,5); //3.用C字符串的部分构造stringstring str4 = "world";string str5(5,'A'); //4.用指定N个指定字符构造stringstring str6(str1.begin() + 6,str1.end()); //5.用string的部分来构造string 起始迭代器 + 结束迭代器
3、string的属性及相关函数
string str1("hello world"); //1.用C字符串构造stringcout << str1.size() << endl; //数据个数cout << str1.length() << endl; //字符长度cout << str1.capacity() << endl; //容量cout << str1.max_size() << endl; //最大个数;str1.append(" linux"); //追加,会修改容量,会修改sizecout << str1.size() << endl; //数据个数cout << str1.capacity() << endl; //容量str1.reserve(45); //只修改容量,不修改sizecout << str1.size() << endl; //数据个数cout << str1.capacity() << endl; //容量// str1.resize(61); //会修改容量,会修改size,实际字符个数不够时会自动填充空格;str1.resize(11); //截短;cout << str1.size() << endl; //数据个数cout << str1.capacity() << endl; //容量cout << "---" << str1 << "---" << endl;
4、string 遍历
//1.通过at()函数遍历,更安全,如果越界会导致程序结束cout << str1.at(0) << endl;str1.at(0) = 'H';cout << str1 << endl;for(unsigned int i = 0; i < str1.size(); i++)cout << str1.at(i) << "\t";cout << endl;cout << str1.at(str1.size()) << endl; //out_of_range
//2.通过[]遍历,不安全,如果越界不会报错;cout << str1[0] << endl;str1[0] = 'H';cout << str1 << endl;for(unsigned int i = 0; i < str1.size(); i++)cout << str1[i] << "\t";cout << endl;cout << str1[str1.size()] << endl;
//3、一般迭代器string::iterator it = str1.begin(); //一般迭代子:能读能写cout << *it << endl;*it = 'H';*(it + 6) = 'W';cout << str1 << endl;for(it = str1.begin(); it != str1.end(); it++)cout << *it << "\t";cout << endl;cout << *(str1.end() - 1) << endl; //end()最后1个成员的右边一个
//4.常迭代器只能读不能修改string::const_iterator cit = str1.cbegin();cout << *cit << endl;
// *cit = 'H'; //常迭代子只能读不能修改for(; cit != str1.end(); cit++)cout << *cit << "\t";cout << endl;
//5.反向迭代器,能读能写string::reverse_iterator rit = str1.rbegin(); //rbegin() 相当于end() - 1;cout << *rit << endl;*rit = 'D';cout << str1 << endl;for(rit = str1.rbegin(); rit != str1.rend(); rit++) //rend() 是第1个成员的左边一个cout << *rit << "\t";cout << endl;cout << *(str1.rend() - 1) << endl;cout << str1 << endl;
5、string 添加
string str1 = "hello ";string str2 = "world linux abc";const char *src = "world linux abc";string dest;dest = str1 + str2; //1.可用操作符重载+,+=连接字符串str1 += str2;str1.append(str2.begin(),str2.begin() + 5); //2.append()将指定字符串的部分添加源串的末尾str1.append(5,'C'); //3.添加指定N个字符到源串末尾str1.append(src + 6,5); //4.添加C字符串的部分到源串的末尾str1.append(str2,6,5); //5.添加string的部分到源串的末尾str1.insert(str1.end(),'C'); //6.插入指定字符到源串的指定位置str1.insert(str1.size(),5,'C'); //7.插入指定N个字符到指定位置str1.insert(str1.size(),src + 6); //8.插入指定C字符串到指定位置str1.insert(str1.size(),src + 6,5); //9.插入指定C字符串的部分到指定位置str1.insert(str1.size(),str2,6,5); //10.插入指定string的部分到指定位置str1.insert(str1.end(),str2.begin() + 6,str2.end() - 4); //11.插入指定string的部分到指定位置str1.insert(str1.end(),5,'C'); //12.插入指定N个字符到指定位置str1.push_back('C'); //13.将指定字符添加到源串的末尾
6、string 查找
//1.单值,从左向右查找指定字符第1次出现的下标
index = src.find('c'); if(index > -1)cout << "found " << index << endl;elsecout << "not found" << endl;
//2.多值index = 0;while((index = src.find('c',index)) > -1){cout << "found : " << index << endl;index++;}cout << src.size() - 1 << endl;
//3.单值,从左向右从源串查找C字符串子串index = src.find(s + 6,1,3);if(index > -1)cout << "found " << index << endl;elsecout << "not found" << endl;
//4.多值,从左向右从源串查找C字符串子串index = 0;while((index = src.find(s + 6,index,3)) > -1){cout << "found : " << index << endl;index += 3;
//5.多值,从左向右从源串查找C++字符串子串index = 0;while((index = src.find(dest,index)) > -1){cout << "found " << index << endl;index += 3;
//6.从右向左查找指定字符index = src.rfind('c'); if(index > -1)cout << "found " << index << endl;index = src.rfind(s + 6,src.size() - 1,3);if(index > -1)cout << "found " << index << endl;
//7.从左向右查找指定C字符串中任意1个字符第1次出现的位置
index = src.find_first_of(s2); if(index > -1)cout << "found " << index << endl;
//8.从左向右查找不是指定C字符串中任意1个字符第1次出现的位置index = src.find_first_not_of(s2); if(index > -1)cout << "found " << index << endl;
//9.从右向左查找指定C字符串中任意1个字符第1次出现的位置
index = src.find_last_of(s2); if(index > -1)cout << "found " << index << endl;
//10.从右向左查找不是指定C字符串中任意1个字符第1次出现的位置index = src.find_last_not_of(s2); if(index > -1)cout << "found " << index << endl;
7、string 修改
src.at(0) = 'A'; //1.通过at()修改指定位置的字符string::iterator it = src.begin();*it = 'A'; //2.通过迭代器修改指定位置的字符src = "hello C++"; //3.通过=修改整个字符串src.assign(dest.begin(),dest.end()); //4.修改源串的所有内容src.assign(5,'C'); //5.修改源串的所有内容为指定N个指定字符src.assign(s,5); //6.将源串的所有内容修改为指定C字符串的部分src.assign(dest,0,5); //7.将源串的所有内容修改为指定string的部分src.replace(src.begin(),src.begin() + 3,dest.begin(),dest.begin() + 5);//8.将源串的某分修改为目标串的某部分src.replace(src.begin(),src.begin() + 3,s,s + 5); //9.将源串某部分修改为目标C字符串的部分src.replace(src.begin(),src.begin() + 3,5,'C'); //10.将源串某部分修改为指定N个指定字符src.replace(src.begin(),src.begin() + 3,s,5); //11.将源串某部分修改为目标C字符串的部分src.replace(0,3,5,'C'); //12.将源串某部分修改为指定N个指定字符src.replace(0,3,s,5); //13.将源串某部分修改为目标C字符串的部分src.replace(0,3,dest,0,5); //14.将源串的某部分修改为目标串的某部分//15.按值修改,单值index = src.find(dest2);if(index > -1)src.replace(index,3,"AAAAA");//16.按值修改,多值index = 0;while((index = src.find(dest2,index)) > -1){src.replace(index,3,"AAAAA");index += 5;}//17.交换两个string的内容src.swap(dest); cout << dest << endl;
8、string 删除
//1.清除
src.clear(); //2.是否为空 if(src.empty()) cout << "src is empty" << endl;//3.删除最后1个字符src.pop_back(); //4.删除指定范围src.erase(src.begin(),src.begin() + 3); //5.删除指定位置的字符src.erase(src.end() - 1); //6.删除指定范围src.erase(0,3); //7.按值删除,单值index = src.find("abc");if(index > -1)src.erase(index,3);//8.按值删除,多值index = 0;while((index = src.find("abc",index)) > -1){src.erase(index,3);index++;}
9、string 比较
//1.C中不能使用==,!=,>,<,>=,<=比较字符串,因为比较两个数组的地址,要strcmp(),strncmp(),strcasecmp(),strncasecmp();
if(buf1 == buf2) cout << "buf1 == buf2" << endl;elsecout << "buf1 != buf2" << endl;//2.C++中可以使用==,!=操作符重载比较两个字符串内容是否相同if(str1 == str2) cout << "str1 == str2" << endl;elsecout << "str1 != str2" << endl;//3.C++中可以使用>,>=,<,<=操作符重载比较两个字符串内容的大小if(str1 > str2) cout << "str1 > str2" << endl;elsecout << "str1 <= str2" << endl;//4.区分大小写比较两个字符串的部分int ret = str1.compare(0,5,buf1,5); //5.区分大小写比较两个字符串的部分int ret = str1.compare(0,5,str2,0,5);
10、string的截取和转换
int start = src.find('<');if(start > -1){int end = src.find('>',start);if(end > -1){int len = end - start - 1;string mail = src.substr(start + 1,len); //1.截取字符串cout << mail << endl;}}
//C字符串与C++字符串转换://1) C字符串转C++字符串:string str1 = "hello";string str2("hello");//2) 将C++字符串转成C字符串const char *s1 = str1.c_str();const char *s2 = str1.data();cout << s1 << " " << s2 << endl;
以上就是关于C++ string类的介绍,感谢观看!