#include <iostream>
#include <string>
using namespace std;
//c++string处理常用函数
int main()
{
string str1 = "abcdefg";
string str2("12345");
string str3(5,'c'); // "ccccc"
string str4(str1,2,3); // "cde" 从【2】开始的3个字符
//赋值
cout << str2.assign("asdfghj",2,3) <<endl; // str2 = "asdfghj"从【2】开始的3个字符 return str2;
//拷贝
char *str5;
str5 = (char*)malloc(10);
cout << "copy: "<< str1.copy(str5,3,2) << endl; //取str1 len = 3 从【2】开始 拷贝str5中 str5必须为char*且分配了足够的空间 返回成功拷贝的字符数
cout << str5 << endl;
//尽量避免以下
string str6 = "xxxxxxxxxxxxxxx";
cout << "copy: "<< str1.copy((char*)&str6,3,2) << endl; //重新声明char *变量 和 强转char*没有区别
//cout << str6 << endl; //error 段错误
cout << (char *)&str6 << endl; //打印 "dfg" 之后的"xxxx"不打印 应该是再dfg后加了‘\0’
const char *p2;
p2 = str1.data(); //data() 一定要加const
cout << p2 <<endl;
//取值
cout << str1.at(3) << endl; //取【3】
cout << str1.substr(3) << endl; //取从【3】开始的u全部
cout << str1.substr(3,2) << endl; //取从【3】开始的 len=2
//容量
cout << "容量" << endl;
cout << str1.capacity() << endl; //15 //分配的空间不代表实际长度
cout << str1.size() << endl; //7
cout << str1.length() << endl; //7 和size一样
cout << str1.empty() << endl; //0 //不空为0 空为1 指size,length
cout << str1.max_size() << endl; //当前最大允许的字符串长度 9223372036854775807
str1.reserve(3);
cout << str1 << endl; //为容器预留足够的空间,避免不必要的重复分配。
cout << str1.capacity() << endl; //小于原本capacity不起任何作用
str1.resize(3);
cout << str1 << endl; // 结果:abc 多余的字符被截掉
cout << str1.size() << endl; //结果:3
//比较
string s1 = "012345678"; // \0
string s2 = "0123456789"; // 9
cout << s1.compare(s2) << endl; // s1 < s2 为负 结果:-1
cout << s1.compare(3,2,s2); // s1取【3】开始的2个字符 和 s2比较。
cout << s1.compare(3,2,s2,3,3); // s1取【3】开始的2个字符 s1取【3】开始的3个字符
//查找
cout << "查找\n"; //没找到返回 string::npos 即 18446744073709551615
cout << s1.find("345") << endl; //返回"345"在s1位置, 即'3'的下标 【3】
cout << s1.find("345",2) << endl; //从【2】开始查, 返回【3】
cout << s1.find("345",2,5) << endl; //从【2】开始查,向后查10位到【11】(包括【11】) 返回【3】
//迭代器与逆向迭代器
cout << "iterator:\n";
string::iterator it1 = s1.begin(); //it1 -> '0'
string::iterator it2 = s1.end(); //it2 -> '\0'
while(it1 != it2)
cout << *it1++ <<endl; //cout << *it1; it++;
string::reverse_iterator it3 = s1.rbegin(); //it3 -> '8'
string::reverse_iterator it4 = s1.rend(); //it4 -> '0'前一位
while(it3 != it4)
cout << *it3++ <<endl; //cout << *it3; it++; reverse_iterator ++时,向左走;
//插入
s1 = "012345678";
s1.insert(s1.begin(),'q');
cout << s1 << endl; //q012345678
s1 = "012345678";
s1.insert(s1.begin(),5,'q');
cout << s1 << endl; //qqqqq012345678
s1 = "012345678";
s1.insert(2,5,'q');
cout << s1 << endl; //01qqqqq2345678
s1 = "012345678";
s1.insert(3,"abc");
cout << s1 << endl; //012abc345678
s1 = "012345678";
s1.insert(3,"abcdef",2,3);
cout << s1 << endl; //012cde345678
s1 = "012345678";
s2 = "asd";
s1.insert(s1.begin(),s2.begin(),s2.end());
cout << s1 << endl; //asd012345678
s1 = "012345678";
s2 = "asd";
s1.insert(s1.begin(),s2.rbegin(),s2.rend());
cout << s1 << endl; //dsa012345678
//替换
cout << "替换\n";
s1 = "012345678";
s2 = "asd";
s1.replace(3,2,s2); //s1的[3],len=2 用s2替换 //前两位可以用iterator代替
cout << s1 << endl; //012asd5678
s1 = "012345678";
s1.replace(3,2,(char *)&s2);
cout << s1 << endl; // 012@黚5678
//s2 有两块空间,一块存符号s1, 一块存 字符数组(字符串)
//类似char* 与 char[]; string类似c语言中char * 详情见:https://blog.csdn.net/qq_42948022/article/details/101758348
s1 = "012345678";
char char_tmp[4] = "asd";
s1.replace(3,2,(char *)&char_tmp); //前两位可以用iterator代替
cout << s1 << endl; // 012asd5678
s1 = "012345678";
s1.replace(3,2,s2,1,2); //s1的[3],len=2 用s2的[1],len=2 替换 //前两位可以用iterator代替
s1 = "012345678";
s1.replace(3,2,3,'a'); //s1的[3],len=2 用3个'a' 替换 //前两位可以用iterator代替
//交换
cout << "交换" << endl;
s1 = "012345678";
s2 = "asd";
swap(s1,s2);
cout << s1 << endl;
cout << s2 << endl;
//删除
s1 = "012345678";
s1.erase(3,2); //删除 s1的[3],len=2
cout << s1 << endl;
s1 = "012345678";
s1.erase(3); //删除 s1的[3],len= +00
cout << s1 << endl;
s1 = "012345678";
s1.erase(); //删除所有
cout << s1 << endl;
return 0;
}
c++string类构造及常用函数
最新推荐文章于 2023-06-08 18:27:13 发布