STL之string

C++ string详解

  之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。我们可以用 = 进行赋值操作,== 进行比较,+ 做串联(是不是很简单?)。我们尽可以把它看成是C++的基本数据类型。

1.声明一个C++字符串

a)    string s;  //生成一个空字符串s

b)    string s(str) //拷贝构造函数 生成str的复制品

c)    string s(str,stridx) //将字符串str内“始于位置stridx”的部分当作字符串的初值

d)    string s(str,stridx,strlen) //将字符串str内“始于stridx且长度顶多strlen”的部分作为字符串的初值

e)    string s(cstr) //将C字符串作为s的初值

f)    string s(chars,chars_len) //将C字符串chars前chars_len个字符作为字符串s的初值。

g)    string s(num,c) //生成一个字符串,包含num个c字符

h)    string s(beg,end) //以区间beg;end(不包含end)内的字符作为字符串s的初值

i)    s.~string() //销毁所有字符,释放内存

 

2.字符串操作函数

a) =,assign()   //赋以新值

b) swap()   //交换两个字符串的内容

c) +=,append(),push_back() //在尾部添加字符

d) insert() //插入字符

e) erase() //删除字符

f) clear() //删除全部字符

g) replace() //替换字符

h) + //串联字符串

i) ==,!=,<,<=,>,>=,compare()  //比较字符串

j) size(),length()  //返回字符数量

k) max_size() //返回字符的可能最大个数

l) empty()  //判断字符串是否为空

m) capacity() //返回重新分配之前的字符容量

n) reserve() //保留一定量内存以容纳一定数量的字符

o) [ ], at() //存取单一字符

p) >>,getline() //从stream读取某值

q) <<  //将某值写入stream

r) copy() //将某值赋值为一个C_string

s) c_str() //将内容以C_string返回

t) data() //将内容以字符数组形式返回

u) substr() //返回某个子字符串

v) find() //查找函数

w)begin() end() //提供类似STL的迭代器支持

x) rbegin() rend() //逆向迭代器

y) get_allocator() //返回配置器

*2.1 C++字符串和C字符串的转换

2.2 大小和容量函数

一个C++字符串存在三种大小:

现有的字符数,函数是size()和length(),他们等效。Empty()用来检查字符串是否为空。

2.3元素存取

   我们可以使用下标操作符[]和函数at()对元素包含的字符进行访问。

Str[3];    //ok

Str.at(3);  //ok

2.4比较函数

   C++字符串支持常见的比较操作符(>,>=,<,<=,==,!=),甚至支持string与C-string的比较(如 str<”hello”)。

  1 1)
  2 #include <string>
  3 #include <iostream>
  4 using namespace std;
  5 
  6 int main()
  7 {
  8     string s("hehe");
  9     cout<<s<<endl;
 10     system(“pause”);
 11 return 0;
 12 }
 13 2)
 14 #include <string>
 15 #include <iostream>
 16 using namespace std;
 17 
 18 int main()
 19 {
 20     char chs[] = "hehe";
 21     string s(chs);
 22     cout<<s<<endl;
 23     system(“pause”);
 24 return 0;
 25 }
 26 3)
 27 #include <string>
 28 #include <iostream>
 29 using namespace std;
 30 
 31 int main()
 32 {
 33     char chs[] = "hehe";
 34     string s(chs,1,3);    //指定从chs的索引1开始,最后复制3个字节
 35     cout<<s<<endl;
 36     system(“pause”);
 37 return 0;
 38 }
 39 4)
 40 #include <string>
 41 #include <iostream>
 42 using namespace std;
 43 
 44 int main()
 45 {
 46     string s1("hehe");
 47     string s2(s1);    
 48     cout<<s2<<endl;
 49     system(“pause”);
 50 return 0;
 51 }
 52 5)
 53 #include <string>
 54 #include <iostream>
 55 using namespace std;
 56 
 57 int main()
 58 {
 59     string s1("hehe",2,3);
 60     string s2(s1);    
 61     cout<<s2<<endl;
 62     system(“pause”);
 63 return 0;
 64 }
 65 6)
 66 #include <string>
 67 #include <iostream>
 68 using namespace std;
 69 
 70 int main()
 71 {
 72     char chs[] = "hehe";
 73     string s(chs,3);    //将chs前3个字符作为初值构造
 74     cout<<s<<endl;
 75     system(“pause”);
 76 return 0;
 77 }
 78 7)
 79 #include <string>
 80 #include <iostream>
 81 using namespace std;
 82 
 83 int main()
 84 {
 85     string s(10,'k');    //分配10个字符,初值都是'k'
 86     cout<<s<<endl;
 87     system(“pause”);
 88 return 0;
 89 }
 90 //以上是string类实例的构造手段,都很简单.
 91 
 92 9)
 93 //赋新值
 94 #include <string>
 95 #include <iostream>
 96 using namespace std;
 97 
 98 int main()
 99 {
100     string s(10,'k');    //分配10个字符,初值都是'k'
101     cout<<s<<endl;
102     s = "hehehehe";
103     cout<<s<<endl;
104     s.assign("kdje");
105     cout<<s<<endl;
106     s.assign("fkdhfkdfd",5);    //重新分配指定字符串的前5的元素内容
107     cout<<s<<endl;        
108     system(“pause”);
109 return 0;
110 }
111 10)
112 //swap方法交换
113 #include <string>
114 #include <iostream>
115 using namespace std;
116 
117 int main()
118 {
119     string s1 = "hehe";
120     string s2 = "gagaga";
121     cout<<"s1 : "<<s1<<endl;
122     cout<<"s2 : "<<s2<<endl;
123     s1.swap(s2);
124     cout<<"s1 : "<<s1<<endl;
125     cout<<"s2 : "<<s2<<endl;
126     system(“pause”);
127 return 0;
128 }
129 11)
130 //+=,append(),push_back()在尾部添加字符
131 #include <string>
132 #include <iostream>
133 using namespace std;
134 
135 int main()
136 {
137     string s = "hehe";
138     s += "gaga";
139     cout<<s<<endl;
140     s.append("嘿嘿");    //append()方法可以添加字符串
141     cout<<s<<endl;
142     s.push_back('k');    //push_back()方法只能添加一个字符...
143     cout<<s<<endl;
144     system(“pause”);
145 return 0;
146 }
147 12)
148 //insert() 插入字符.其实,insert运用好,与其他的插入操作是一样的.
149 #include <string>
150 #include <iostream>
151 using namespace std;
152 
153 int main()
154 {
155     string s = "hehe";
156     s.insert(0,"头部");            //在头部插入
157     s.insert(s.size(),"尾部");    //在尾部插入
158     s.insert(s.size()/2,"中间");//在中间插入
159     cout<<s<<endl;
160     system(“pause”);
161 return 0;
162 }
163 13)
164 #include <string>
165 #include <iostream>
166 using namespace std;
167 
168 int main()
169 {
170     string s = "abcdefg";
171     s.erase(0,1);    //从索引0到索引1,即删除掉了'a'
172     cout<<s<<endl;
173     //其实,还可以使用replace方法来执行删除操作
174     s.replace(2,3,"");//即将指定范围内的字符替换成"",即变相删除了
175     cout<<s<<endl;
176     system(“pause”);
177 return 0;
178 }
179 
180 14)
181 //clear() 删除全部字符
182 #include <string>
183 #include <iostream>
184 using namespace std;
185 
186 int main()
187 {
188     string s = "abcdefg";
189     cout<<s.length()<<endl;
190     s.clear();
191     cout<<s.length()<<endl;
192     //使用earse方法变相全删除
193     s = "dkjfd";
194     cout<<s.length()<<endl;
195     s.erase(0,s.length());
196     cout<<s.length()<<endl;
197     system(“pause”);
198 return 0;
199 }
200 15)
201 //replace() 替换字符
202 #include <string>
203 #include <iostream>
204 using namespace std;
205 
206 int main()
207 {
208     string s = "abcdefg";
209     s.replace(2,3,"!!!!!");//从索引2开始3个字节的字符全替换成"!!!!!"
210     cout<<s<<endl;
211     system(“pause”);
212 return 0;
213 }
214 16)
215 //==,!=,<,<=,>,>=,compare()  比较字符串
216 #include <string>
217 #include <iostream>
218 using namespace std;
219 
220 int main()
221 {
222     string s1 = "abcdefg";
223     string s2 = "abcdefg";    
224     if (s1==s2)cout<<"s1 == s2"<<endl;
225     else cout<<"s1 != s2"<<endl;
226     
227     if (s1!=s2)cout<<"s1 != s2"<<endl;
228     else cout<<"s1 == s2"<<endl;
229     
230     if (s1>s2)cout<<"s1 > s2"<<endl;
231     else cout<<"s1 <= s2"<<endl;
232     
233     if (s1<=s2)cout<<"s1 <= s2"<<endl;
234     else cout<<"s1 > s2"<<endl;
235 
236     system(“pause”);
237 return 0;
238 }
239 17)
240 //size(),length()  返回字符数量
241 #include <string>
242 #include <iostream>
243 using namespace std;
244 
245 int main()
246 {
247     string s = "abcdefg";
248     cout<<s.size()<<endl;
249     cout<<s.length()<<endl;
250 
251     system(“pause”);
252 return 0;
253 }
254 18)
255 //max_size() 返回字符的可能最大个数
256 #include <string>
257 #include <iostream>
258 using namespace std;
259 
260 int main()
261 {
262     string s = "abcdefg";
263     cout<<s.max_size()<<endl;
264 
265     system(“pause”);
266 return 0;
267 }
268 19)
269 //empty()  判断字符串是否为空
270 #include <string>
271 #include <iostream>
272 using namespace std;
273 
274 int main()
275 {
276     string s ;
277     if (s.empty())
278         cout<<"s 为空."<<endl;
279     else
280         cout<<"s 不为空."<<endl;
281 
282     s = s + "abcdefg";
283     if (s.empty())
284         cout<<"s 为空."<<endl;
285     else
286         cout<<"s 不为空."<<endl;
287 
288     system(“pause”);
289 return 0;
290 }
291 20)
292 // [ ], at() 存取单一字符
293 #include <string>
294 #include <iostream>
295 using namespace std;
296 
297 int main()
298 {
299     string s = "abcdefg1111";
300     
301     cout<<"use []:"<<endl;
302     for(int i=0; i<s.length(); i++)
303     {
304         cout<<s[i]<<endl;
305     }
306     cout<<endl;
307 
308     cout<<"use at():"<<endl;
309     for(int i=0; i<s.length(); i++)
310     {
311         cout<<s.at(i)<<endl;
312     }
313     cout<<endl;
314     
315     system(“pause”);
316 return 0;
317 }
318 21)
319 #include <string>
320 #include <iostream>
321 using namespace std;
322 
323 int main()
324 {
325     string s = "abcdefg1111";
326     
327     const char * chs1 = s.c_str();
328     const char * chs2 = s.data();
329 
330     cout<<"use at():"<<endl;
331     int i;
332     for(i=0; i<s.length(); i++)
333     {
334         cout<<"c_str() : "<<chs1[i]<<endl;
335         cout<<"data() : "<<chs2[i]<<endl;
336     }
337     cout<<"c_str() : "<<chs1<<endl;
338     cout<<"data() : "<<chs2<<endl;
339     cout<<endl;
340     
341     system(“pause”);
342 return 0;
343 }
344 22)
345 // substr() 返回某个子字符串
346 #include <string>
347 #include <iostream>
348 using namespace std;
349 
350 int main()
351 {
352     string s = "abcdefg1111";
353     
354     string str = s.substr(5,3);//从索引5开始3个字节
355     cout<<str<<endl;
356     
357     system(“pause”);
358 return 0;
359 }
360 23)
361 // find 查找函数
362 #include <string>
363 #include <iostream>
364 using namespace std;
365 
366 int main()
367 {
368     string s = "abcdefg1111";
369     string pattern = "fg";
370     string::size_type pos;
371     pos = s.find(pattern,0);        //从索引0开始,查找符合字符串"fg"的头索引
372     cout<<pos<<endl;
373     string str = s.substr(pos,pattern.size());
374     cout<<str<<endl;
375     system(“pause”);
376 return 0;
377 }
378 24)
379 // begin() end() 提供类似STL的迭代器支持
380 #include <string>
381 #include <iostream>
382 using namespace std;
383 
384 int main()
385 {
386     string s = "abcdefg1111";
387     for(string::iterator iter = s.begin(); iter!=s.end(); iter++)
388     {
389         cout<<*iter<<endl;
390     }
391     cout<<endl;
392 
393     system(“pause”);
394 return 0;
395 }
cstring代码实例
星河滚烫,你是人间理想。
原文地址:https://www.cnblogs.com/becase/p/11787179.html