字符串结构

 1 // string 
 2 
 3 #include<string>
 4 #include<iostream>
 5 #include<cstdlib>
 6 
 7 using namespace std;
 8 
 9 void main()
10 {
11     string str1 = "ABC";
12     string str2 = "XYZ";
13     string str3 = str1+str2;// string 是一个类重载了+运算符
14     
15     string mystr("calc");
16     system(mystr.c_str());// 返回类的内部指针
17     
18     string mynewstr;
19     mynewstr += mystr;// 重载了+=
20     cout << mynewstr << endl;// 
21 
22     cin.get();
23 }
24 
25 //------------------------------------------------------------------------
26 
27 
28 void main()
29 {
30     string str1 = "tasklist";
31 
32     str1.insert(str1.begin()+3,'A');// 插入字符
33     
34     //str1.insert(str1.begin()+3,"123");
35     
36     str1.replace(3,0,"123");// 插入字符串
37     
38     str1.replace(1,3,"ABC");// 替换字符串
39 
40     str1.erase(str1.begin());// 删除第一个字符
41     
42     str1.erase(str1.begin()+4);// 删除指定字符
43 
44     str1.erase(1,3);// 删除一个字符串,第一个整数是下表,从下表开始删除3个字符
45 
46     cout << str1 << endl;
47 
48 
49     
50 
51     // 字符串的迭代器 都是迭代字符
52     for(auto ib = str1.begin(),ie = str1.end();ib!=ie;ib++)
53     {
54         cout << *ib << endl;    
55     }
56     
57     for(auto rb = str1.rbegin(),re = str1.rend();rb!=re;rb++)
58     {
59         cout << *rb << endl;    
60     }
61 
62 
63     cin.get();
64 }
65 
66 //------------------------------------------------------------------------
67 
68 void main()
69 {
70 
71     string str1 = "ipconconfig";
72 
73     int pos = str1.find('p');// 查找一个字符 1
74     
75     int pos = str1.find("con");// 查找返回第一个字符的位子  2
76     
77     int pos = str1.rfind("con");// 反向查找
78 
79     int pos = str1.find("co",4);// 从下表4 开始查找,找不到返回-1
80     
81     
82 
83     cout << pos << endl;
84 
85     string strA = "123";
86     string strB = "123";
87     cout << (strA == strB) << endl;// 相等 1 不等 0
88 
89     cin.get();
90 }

 

posted on 2015-06-14 15:04  Dragon-wuxl  阅读(142)  评论(0)    收藏  举报

导航