string基本字符序列容器(竞赛时常用的使用方法总结)

C语言只提供了一个char类型用来处理字符,而对于字符串,只能通过字符串数组来处理,而C++STL提供了string基本字符序列容器来处理字符串,可以将其理解为字符串类,它提供了添加,删除,替换、查找和比较等丰富、简洁的方法。

下面是在编写代码是的具体应用。

  1 //关于C++ STL string基本字符系列容器的学习,看别人的代码一百遍,不如自己动手写一遍。
  2 #include <string>
  3 #include <vector> 
  4 #include <iostream>
  5 #include <algorithm>
  6 using namespace std;
  7 
  8 int main()
  9 {
 10     /*创建string对象 string 加 对象名*/
 11     string s;
 12     cout<<s.length()<<endl;
 13     /*运行结果
 14     0
 15     */ 
 16     
 17     /*给string对象赋值一般有两种方式*/
 18     s="hello,C++STL.";//直接赋值
 19     //更长用的方法是把一个字符指针赋给一个字符串对象
 20     char ss[]="abcd";//scanf("%s",ss);  //注意使用scanf输入,此处直接赋值 
 21     s="";//清空字符串
 22     if(s.empty()) cout<<"该字符串为空\n"; 
 23     s=ss;//把整个字符数组赋给string对象 
 24     cout<<"赋值后为:"<<s<<endl;
 25     /*运行结果
 26     该字符串为空
 27     赋值后为:abcd
 28     */
 29     
 30     /*string对象和字符数组互操作*/
 31     printf(s.c_str());cout<<endl;//用printf输出字符串对象,要采用c_str()方法
 32     printf("%s\n",ss);
 33     cout<<s<<endl;//输出字符串对象 
 34     cout<<ss<<endl;//直接使用cout输出字符串数组 
 35     /*运行结果
 36     abcd
 37     abcd
 38     abcd
 39     abcd
 40     */
 41     
 42     s="hello,C++STL.";//重新直接赋值 
 43     
 44     /*在string尾部添加字符或者字符串*/
 45     //要想在string对象尾部添加字符
 46     s += 'a';
 47     cout<<s<<endl;
 48     //同样要想在string对象尾部添加字符串
 49     s += "bc";
 50     cout<<s<<endl;
 51     //也可以使用append()方法
 52     s.append("edg");
 53     cout<<s<<endl;
 54     /*运行结果
 55     hello,C++STL.a
 56     hello,C++STL.abc
 57     hello,C++STL.abcedg
 58     */
 59     
 60     /*给string对象添加字符,可以使用insert()方法把一个字符插入到迭代器位置之前*/
 61     //定义迭代器
 62     string::iterator it1;
 63     it1=s.begin();
 64     //把字符插入到第2个字符之前,注意字符位置从0开始计数 
 65     s.insert(it1+2,'h'); 
 66     cout<<s<<endl;
 67     /*运行结果
 68     hehllo,C++STL.abcedg
 69     */
 70     
 71     /*访问string对象元素时一般使用下标方式随机访问string对象的元素*/
 72     int i;
 73     for(i=0;i< s.length();i++){//其中length()方法计算字符串的长度 
 74         cout<<s[i]<<' ';
 75     } 
 76     cout<<endl;
 77     /*运行结果
 78     h e h l l o , C + + S T L . a b c e d g
 79     */
 80     
 81     /*删除string对象的元素*/
 82     //清空一个字符串对象直接给他赋一个空字符串即可,即s="";
 83     //要向删除迭代器所指的那个元素或者一个区间中的所有元素时,使用erase()方法
 84     string::iterator it2=s.begin();//定义迭代器变量,指向字符串对象首元素
 85     s.erase(it2+2);//删除第2个元素,元素位置从0开始计数 
 86     cout<<s<<endl;
 87     
 88     s.erase(it2+13,it2+19);//删除第13到第18,区间删除时有区间多加一个单位 
 89     cout<<s<<endl;
 90     /*运行结果
 91     hello,C++STL.abcedg
 92     hello,C++STL.
 93     */
 94     
 95     /*要想替换string对象的字符,则使用replace()方法*/
 96     //从第0个开始,将连续的5个字符替换为"good",即"hello"替换为"good" 
 97     s.replace(0,5,"good");
 98     cout<<s<<endl;
 99     /*运行结果
100     good,C++STL.
101     */
102     
103     /*要想搜索string对象的元素或子串,则采用find()方法,找到返回下标值,找不到的话,在DEV-C++5.9.2中返回18446744073709551615*/
104     //查找字符'C'
105     cout<<s.find('C')<<endl; 
106     //查找字符串"C++"
107     cout<<s.find("C++")<<endl;
108     //查找字符串"hello"
109     cout<<s.find("hello")<<endl; 
110     /*运行结果
111     5
112     5
113     18446744073709551615
114     */
115     
116     /*string对象的比较*/ 
117     cout<<s.compare("z")<<endl;//s比"good"字符串小,返回-1
118     cout<<s.compare("good,C++STL.")<<endl;//s与"good"字符串相同,返回0
119     cout<<s.compare("a")<<endl;//s比"good"字符串大,返回1
120     /*运行结果
121     -1
122     0
123     1
124     */
125     
126     /*要想将string对象中一段区间中的元素反向排序,则采用reverse()算法,注意加上头文件algorithm*/
127     cout<<"s反向前:\n";
128     cout<<s<<endl;
129     reverse(s.begin(),s.end()); 
130     cout<<"s反向后:\n";
131     cout<<s<<endl;
132     /*运行结果
133     s反向前:
134     good,C++STL.
135     s反向后:
136     .LTS++C,doog
137     */
138     
139     /*处理二维字符串数组时可将string基本字符序列容器作为vector的元素,从而看作vector向量容器来处理,只不过是元素是string对象*/
140     vector<string> v;
141     v.push_back("Jack");
142     v.push_back("Mike");
143     v.push_back("Tom");
144     
145     cout<<v[0]<<endl;
146     cout<<v[1]<<endl;
147     cout<<v[2]<<endl;
148     cout<<v[0][0]<<endl;
149     cout<<v[1][0]<<endl;
150     cout<<v[2][0]<<endl;
151     cout<<v[2].length()<<endl;
152     /*运行结果
153     Jack
154     Mike
155     Tom
156     J
157     M
158     T
159     3
160     */
161     return 0;
162 } 

 

posted @ 2018-03-04 17:31  Reqaw  阅读(429)  评论(0编辑  收藏  举报