11.string容器

 1 #include <iostream>
 2 //string的本质也是容器
 3 #include <string>
 4 #include <cstdlib>
 5 using namespace std;
 6 
 7 void main()
 8 {
 9     string str1 = "1234";
10     string str2 = "345";
11     string str3 = str1 + str2;
12     
13     //对比两个字符串,实现strcmp
14     cout << str1.compare(str2) << endl;
15 
16     ////尾插
17     //str3.push_back('X');
18     ////中间位置插入
19     //str3.insert(str3.begin() + 3, 'X');
20     ////清除某一部分
21     //str3.erase(str3.begin, str3.begin() + 5);
22     //把0到2换成hello
23     // str3.replace(0, 2, "hello");
24     //从第五个位置向后移动0个字符进行插入
25     /*str3.replace(5, 0, "abc");*/
26     //获取内部指针
27     /*const char *p = str3.c_str();
28     cout << p << endl;
29 */
30     /*for (auto i : str3)
31     {
32         cout << i << endl;
33     }
34 
35     for (auto cb = str3.cbegin(), ce = str3.cend(); cb != ce; cb++)
36     {
37         cout << *cb << endl;
38     }*/
39 
40     string s("test asdfggfds");
41     cout << s.find("test") << endl;
42     //从指定位置开始找(找到返回1,没找到返回0)
43     cout << (int)s.find("test",12) << endl;
44     //反向查找
45     cout << s.rfind("test") << endl;
46     //查找第一个属于asd的字符
47     cout << s.find_first_of("asd") << endl;
48     //s.find_first_not_of 第一个不属于的
49 
50     //清空
51     str3.empty();
52     //获取大小
53     str3.size();
54     //字符串长度
55     //str3.length();
56     cin.get();
57 }
原文地址:https://www.cnblogs.com/xiaochi/p/8627346.html