string的最基本操作就是创建字符串
void test(){ //创建一个空的string对象 string str; //拷贝一个和已经存在的对象内容一模一样的新的对象 string copy(str); //生成一个abc的字符串 string str2("abc"); //在str2中取下标1至2的字符串生成一个新的字符串 string substr(str2, 1, 2); //"bc" //在字符串"123456789"中取前5个 string str3("123456789", 5);//"12345" //生成一个有5个a的字符串 string str4(5, 'a');//aaaaa //单参构造的隐式类型转换, 相当于调用string str2("abcde"); string str5 = "abcde"; }
访问string的方式,(范围for,for)也可以修改内容
void test(){ string str = "123456"; //范围for: 实际上是通过迭代器实现的 //支持迭代器访问的自定义类型都可以支持范围for for (auto& ch : str){ cout << ch << " "; ch = 'a'; } cout << endl << str << endl; }
void test(){ string str = "123456"; for (int i = 0; i < str.size(); i++){ cout << str[i] << " "; str[i] = 'a'; } cout << endl << str << endl; }
resize:修改有效字符的个数, 当调整之后的size大于容量时, 容量会自动发生变化
void test(){ string str; cout << str.size() << endl; //0 //resize: 修改有效字符的个数,不足的补'\0',多的去掉 str.resize(10); cout << str.size() << endl; //0000000000 string str2 = "123"; cout << str2.size() << endl; //123 str2.resize(10); cout << str2.size() << endl; //1230000000 str2.resize(2); cout << str2.size() << endl; //12 //不足的位置填充a str2.resize(5, 'a'); //12aaa str2.resize(2, 'b'); //12,减小不会填充 }
reserve: 修改容量大小,增加容量,不影响size
void test(){ string str = "123"; //有效字符的个数 int a = str.size(); //3 //空间的容量 int b = str.capacity(); //15 cout << a << ' ' << b << endl; str.resize(2); int a1 = str.size(); //2 int b1 = str.capacity(); //15 cout << a1 << ' ' << b1 << endl; str.resize(20); int a2 = str.size(); //20 //自动增加容量 int b2 = str.capacity(); //31 cout << a2 << ' ' << b2 << endl; //reserve:修改容量,只能增加容量 //不会影响size的大小 str.reserve(60); int a3 = str.size(); //20 int b3 = str.capacity(); //63 cout << a3 << ' ' << b3 << endl; //设置减小容量,但程序运行中无法减小容量,只能增容 str.reserve(10); int a4 = str.size(); //20 int b4 = str.capacity(); //63 cout << a4 << ' ' << b4 << endl; }
clear : 清空有效字符, 但不影响容量.
void test(){ string str = "123"; //有效字符的个数 int a = str.size(); //3 //空间的容量 int b = str.capacity(); //15 cout << a << ' ' << b << endl; str.clear(); int a1 = str.size(); //0 int b1 = str.capacity(); //15 cout << a1 << ' ' << b1 << endl; }
字符串尾插
void test(){ string str; string str1 = "123"; //+=操作,相当于尾插 str += str1; //123 str += "abc"; //123abc str += '4'; //123abc4 str.operator+=('5'); //123abc45 //append操作,追加,相当于尾插 str.append(str1); //123abc45123 // 从str中下标为1的开始加一个字符 str.append(str1, 1, 1); //123abc451232 str.append("abc"); //123abc451232abc // 从123456中加前5个 str.append("123456",5); //123abc451232abc12345 //增加5个相同字符 str.append(5,'m'); //123abc451232abc12345mmmmm //使用迭代器 str.append(str1.begin(), str1.end()); //123abc451232abc12345mmmmm123 cout << str << endl; }
赋值assign
void test(){ string str; string str1 = "123"; str.assign(str1); //123 str.assign(str1,1,1); //2 str.assign("abc"); //abc str.assign("abcd",2); //ab str.assign(5,'a'); //aaaaa str.assign(++str1.begin(),str1.end()); //23 }
插入insert
void test(){ string str1 = "123"; string str2 = "abc"; //在下标为0的位置插入str2 str1.insert(0, str2); //abc123 //在下标为4的位置插入str2中下标为1之后的两个字符 str1.insert(4, str2,1,2); //abc1bc23 //尾插abc str1.insert(str1.size(),"abc"); //abc1bc23abc //在下标为5的位置插入123456中的前4个字符 str1.insert(5, "123456",4); //abc1b1234c23abc str1.insert(str1.begin(),2,'0'); //00abc1b1234c23abc str1.insert(str1.end(), str2.begin(), str2.end());//00abc1b1234c23abcabc cout << str1 << endl; }