学习string容器总结

  1 # include<iostream>
  2 # include<cstdio>
  3 # include<algorithm>
  4 # include<cstdlib>
  5 # include<string>
  6 # include<vector>
  7 using namespace std;
  8 int main(int argc,const char *argv[])
  9 {
 10     /*------------------------------------------*/
 11     //length
 12     //string s;
 13     //s = "abc123456";
 14     //cout<<s.length()<<endl;
 15     //s = "";
 16     //cout<<s.empty()<<endl;
 17 
 18     /*------------------------------------------*/
 19     //string赋值的两种形式
 20     //<1>
 21     //string s;
 22     //s = "hello,c++ stl";
 23     //cout<<s<<endl;
 24     //<2>
 25     // string s;
 26     //char ss[5000];
 27     //scanf比cin快。。。
 28     //scanf是c语言的函数,不支持string对象
 29     //scanf("%s",&ss);
 30     //s = ss;
 31     //cout<<s<<endl;
 32 
 33 
 34     /*------------------------------------------*/
 35     //从对象尾部添加字符
 36     //string s;
 37     //s = s + 'a';
 38     //s = s + 'b';
 39     // s = s + 'c';
 40     //cout<<s<<endl;
 41     //cout<<s.size()<<endl;
 42 
 43     /*------------------------------------------*/
 44     //从string对象尾部追加字符串
 45     //<1>
 46     //string s;
 47     //s = s+"abc";
 48     //s = s+"123";
 49     //cout<<s<<endl;
 50     //<2>
 51     //string s;
 52     //s.append("abc");
 53     //s.append("123");
 54     //cout<<s<<endl;
 55 
 56     /*------------------------------------------*/
 57     //给string对象插入字符
 58     //string s;
 59     //s = "123456";
 60     //string::iterator it;
 61     //it = s.begin();
 62     //it = s.insert(it+1,'p');
 63     //cout<<s<<endl;
 64 
 65     /*---------------------------------------*/
 66     //访问string对象的元素
 67     //string s;
 68     //s = "abc123456";
 69     //cout<<s[0]<<endl;
 70     //cout<<s[0]-'a'<<endl;
 71 
 72     //string::iterator it;
 73     //for(it = s.begin();it!=s.end();it++)
 74     //{
 75     //    cout<<*it<<" ";
 76     //}
 77 
 78     /*--------------------------------------*/
 79     //删除指定的串
 80     //string s;
 81     //s = "abc123456";
 82     // string::iterator it;
 83     // it = s.begin();
 84     // s.erase(it+3);
 85     // cout<<s<<endl;
 86     // s.erase(it,it+3);
 87     // cout<<s<<endl;
 88     // s = "";
 89     // cout<<s.length()<<endl;
 90 
 91     /*---------------------------------------*/
 92     //替换string对象的字符
 93     //string s;
 94     // s = "abc123456";
 95     //从第3个数开始,将连续的3个字符替换为“good”
 96     //即将“123”替换为“good”
 97     //s.replace(3,3,"good");
 98     //cout<<s<<endl;
 99 
100     /*---------------------------------------*/
101     //搜索string对象的元素或子串
102     //string s;
103     //s = "cat dog cat";
104     //cout<<s.find('c')<<endl;
105     //cout<<s.find("c")<<endl;
106     //cout<<s.find("cat")<<endl;
107     //cout<<s.find("dog")<<endl;
108     //cout<<s.find("dogc")<<endl;
109 
110     /*---------------------------------------*/
111     //string对象的比较
112     //string s;
113     //s = "cat dog cat";
114     //cout<<s.compare("cat")<<endl;
115     //cout<<s.compare("cat dog cat")<<endl;
116     //cout<<s.compare("dog")<<endl;
117 
118     /*--------------------------------------*/
119     //reveser转置
120     //string s;
121     // s = "123456789";
122     //reverse(s.begin(),s.end());
123     //cout<<s<<endl;
124 
125     /*--------------------------------------*/
126     //vector在string的应用
127     //vector<string>v;
128     //v.push_back("Jack");
129     //v.push_back("Michael");
130     //v.push_back("Miao");
131     //cout<<v[0]<<endl;
132     //cout<<v[1]<<endl;
133     //cout<<v[2]<<endl;
134     //cout<<v[0][0]<<endl;
135     //cout<<v[1][0]<<endl;
136     //cout<<v[2].length()<<endl;
137 
138     /*--------------------------------------*/
139     //string s;
140     //s = "1234059";
141     //int i;
142     //int sum = 0;
143     //for(i = 0; i < s.length(); i++)
144     //{
145     //    if(s[i] == '0') sum+=0;
146     //    else if(s[i] == '1') sum+=1;
147     //    else if(s[i] == '2') sum+=2;
148     //    else if(s[i] == '3') sum+=3;
149     //    else if(s[i] == '4') sum+=4;
150     //    else if(s[i] == '5') sum+=5;
151     //    else if(s[i] == '6') sum+=6;
152     //    else if(s[i] == '7') sum+=7;
153     //    else if(s[i] == '8') sum+=8;
154     //    else if(s[i] == '9') sum+=9;
155     // }
156     // cout<<sum<<endl;
157 
158     /*---------------------------------*/
159     //string s;
160     //char ss[100];
161     //scanf("%s",&ss);
162     //s=ss;
163     //printf(s.c_str());
164     //cout<<endl;
165     //printf("%s",ss);
166     //cout<<endl;
167     //cout<<s<<endl;
168     //cout<<ss<<endl;
169 
170     /*----------------------------------*/
171     //string 和scanf函数
172     //string s1,s2,s3;
173     //char sa[100],sb[100],sc[100];
174     //将字符串分离成字串,分隔符为空格
175     //sscanf("abc 123 pc","%s %s %s",&sa,&sb,&sc);
176     //s1 = sa;
177     //s2 = sb;
178     //s3 = sc;
179     //cout<<s1<<" "<<s2<<" "<<s3<<endl;
180     //将字符串分离成数字,分隔符为空格
181     //当用到数字的时候,跟scanf一样,踏要指针地址
182     //int a,b,c;
183     //sscanf("1 2 3","%d %d %d",&a,&b,&c);
184     //cout<<a<<" "<<b<<" "<<c<<endl;
185     //将字符串分离成数字,分隔符为“,”和“$”
186     //当用到数字的时候,跟scanf一样,它要传指针地址
187     //int x,y,z;
188     //sscanf("4,5$6","%d,%d$%d",&x,&y,&z);
189     //cout<<x<<" "<<y<<" "<<z<<endl;
190 
191     /*----------------------------------------------*/
192     //string对象与数值相互转换(写法有问题)
193     //string convertToString(double x)//C++方法,将数值转换为string
194     //{
195     //    ostringstream o;
196     //   if(o<<x)
197     //   return o.str();
198     //   return "conversion error";
199     //}
200     //double convertFromString(const string &s)//C++方法,将string转换为数值
201     //{
202     //    istringstream (s);
203     //    double x;
204     //    if(i >>x)
205     //   return x;
206     //   return 0.0;
207     //}
208     //int main(int argc,const char* argv[])
209     //{
210     //将数值转换为string的第一种方法:C方法
211     //char b[10];
212     // string a;
213     // sprinf(b,"%d",1975);
214     // a = b;
215     // cout<<a<<endl;
216     //将数值转化为string的第二种的方法:C++方法
217     // string cc = convertToString(1976);
218     // cout<<cc<<endl;
219     //将string转化为数值的方法:C++方法
220     // string dd=  "2006";
221     //int p = convertFromString(dd)+2;
222     //cout<<p<<endl;
223     //}
224     return 0;
225 }
View Code
原文地址:https://www.cnblogs.com/sxmcACM/p/3456184.html