ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++进阶-3-容器(string)

2022-05-06 16:31:08  阅读:104  来源: 互联网

标签:容器 进阶 C++ str 字符串 string


C++进阶-3-容器

  1 #include<iostream>
  2 using namespace std;
  3 
  4 // string容器
  5 
  6 // 1.string的构造函数
  7 void test01() {
  8 
  9     string s1;  // 默认构造
 10 
 11     const char* str = "hello world!";
 12     string s2(str);
 13     cout << "s2 = " << s2 << endl;
 14 
 15     string s3(s2);
 16     cout << "s3 = " << s3 << endl;
 17 
 18     string s4(10, 'a');
 19     cout << "s4 = " << s4 << endl;
 20 
 21 }
 22 
 23 // 2.string的赋值操作,operator=  或者 assign
 24 void test02() {
 25 
 26     string str1;
 27     str1 = "hello world!";
 28     cout << "str1 = " << str1 << endl;
 29 
 30     string str2;
 31     str2 = str1;
 32     cout << "str2 = " << str2 << endl;
 33 
 34     string str3;
 35     str3 = 'a';
 36     cout << "str3 = " << str3 << endl;
 37 
 38     string str4;
 39     str4.assign("hello C++");
 40     cout << "str4 = " << str4 << endl;
 41 
 42     string str5;
 43     str5.assign("hello C++", 4);
 44     cout << "str5 = " << str5 << endl;
 45 
 46     string str6;
 47     str6.assign(str5);
 48     cout << "str6 = " << str6 << endl;
 49 
 50     string str7;
 51     str7.assign(10, 'b');
 52     cout << "str7 = " << str7 << endl;
 53 }
 54 
 55 // 3.字符串拼接
 56 void test03() {
 57 
 58     string str1 = "我";
 59     str1 += "最帅";
 60     cout << "str1 = " << str1 << endl;
 61 
 62     string str3 = "哈哈";
 63     str3.append(",我是猪八戒!");
 64     cout << "str3 = " << str3 << endl;
 65 
 66     str3.append("abcdefg", 3);
 67     cout << "str3 = " << str3 << endl;
 68 
 69     string str2;
 70     str2 = "lkjhg";
 71     str3.append(str2, 1, 3);  // 参数2是从哪个位置开始截取,参数参截取字符个数
 72     cout << "str3 = " << str3 << endl;
 73     
 74 }
 75 
 76 // 4.字符串查找和拼接
 77 void test04() {
 78 
 79     // 查找 find
 80     string str1 = "abcdefg";
 81 
 82     int pos = str1.find('df');  // 找到返回位置索引,找不到返回-1
 83     if (pos == -1) {
 84         cout << "未找到字符串!" << endl;
 85     }
 86     else
 87     {
 88         cout << "找到字符串,pos = " << pos << endl;
 89     }
 90 
 91     // 查找 rfind
 92     // 区别:find从左往右查找,rfind是从右往左查找
 93     int rpos = str1.rfind('de');
 94     cout << "找到字符串,rpos = " << rpos << endl;
 95 
 96 
 97     // 替换
 98     string str2 = "abcdefg";
 99     str2.replace(1, 3, "11111");
100     cout << "str2 = " << str2 << endl;
101 
102 }
103 
104 // 5. 字符串比较
105 void test05() {
106 
107     //主要是比较字符串是否相等
108     string str1 = "hello";
109     string str2 = "hello";
110 
111     if (str1.compare(str2) == 0) {
112         cout << "str1 等于 str2" << endl;
113     }
114     else if(str1.compare(str2) == 1) {
115         cout << "str1 大于 str2" << endl;
116     }
117     else if (str1.compare(str2) == -1) {
118         cout << "str1 小于 str2" << endl;
119     }
120 }
121 
122 // 6.字符串存取
123 void test06() {
124 
125     string str = "hello";
126 
127     // 1.通过 [] 访问单个字符
128     for (int i = 0; i < str.size(); i++) {
129         cout << str[i] << " ";
130     }
131     cout << endl;
132 
133     // 2.通过at方式访问单个字符
134     for (int i = 0; i < str.size(); i++) {
135         cout << str.at(i) << " ";
136     }
137     cout << endl;
138 
139     // 修改单个字符
140     str[0] = 'x';
141     cout << "str = " << str << endl;
142 
143     str.at(1) = 'y';
144     cout << "str = " << str << endl;
145 
146 }
147 
148 // 7.字符串插入和删除
149 void test07() {
150 
151     string str = "hello";
152 
153     // 起始下标都是从0开始
154     // 插入
155     str.insert(1, "222");   // h222ello
156     cout << "str = " << str << endl;
157 
158     // 删除
159     str.erase(2, 4);
160     cout << "str = " << str << endl;
161 }
162 
163 // 8.字符串子串
164 void test08() {
165 
166     string str = "abcdef";
167     string subStr = str.substr(1, 3);
168     cout << "subStr = " << subStr << endl;
169 
170     // 实用操作  截取邮件中的用户名
171     string email = "xiaoming@163.com";
172     int pos = email.find("@");
173     string userName = email.substr(0, pos);
174     cout << "用户名是:" << userName << endl;
175 
176 }
177 
178 
179 int main() {
180 
181     // 1.构造函数
182     //test01();
183 
184     // 2.赋值操作
185     //test02();
186 
187     // 3.字符串拼接
188     //test03();
189 
190     // 4.字符串查找和替换
191     //test04();
192 
193     // 5.字符串比较
194     //test05();
195 
196     // 6.字符串存取
197     //test06();
198 
199     // 7.字符串插入和删除
200     //test07();
201 
202     // 8.字符串子串
203     test08();
204 
205 
206     system("pause");
207 
208     return 0;
209 }
210 
211 // 总结
212 // 
213 // string 容器
214 // string是C++风格的字符串,而string本质上是一个类
215 // 
216 // string和char的区别:
217 //    1.char*是一个指针
218 //    2.string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器
219 // 
220 // 特点:
221 //    string类内部封装了很多成员方法,如:find、copy、delete、replace、insert等
222 //    string管理char*所分配的内存,不需要担心复制越界和取值越界等,由类内部进行负责
223 // 
224 // string构造函数
225 //    构造函数原型:
226 //        string();                    // 创建一个空字符串,如:string str;
227 //        string(const char *);        // 使用字符串s初始化
228 //        
229 //        string(const string& str);    // 使用一个string对象初始化另一个对象
230 //        
231 //        string(int n, char c);        // 使用n个字符c初始化
232 // 
233 // string赋值操作
234 // 
235 // 字符串拼接
236 // 
237 // 字符串查找和替换
238 //    查找:查找指定字符串是否存在
239 //    替换:在指定的位置替换字符串
240 // 
241 // 字符串比较
242 //    主要是比较字符串是否相等
243 //    按照ASCII码进行比较:
244 //        =  返回0
245 //        >  返回1
246 //        <  返回-1
247 // 
248 // 字符串存取
249 // 
250 // 字符串插入和删除
251 // 
252 // 字符串子串
253 // 

 

标签:容器,进阶,C++,str,字符串,string
来源: https://www.cnblogs.com/LYH-win/p/16229313.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有