```cpp
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;
//c++方法 将数值转换为string 头文件 #include <sstream>
string convertToString(double x){
ostringstream o;
if(o<<x){
return o.str();
}
return "conversion error!";//if error
}
//c++方法 将string转换为数值 头文件 #include <sstream>
double convertFromString(string &s){
istringstream i(s);
double x;
if(i>>x){
return x;
}
return 0.0;//if error
}
int main(){
string s;
char ss[100];
//scanf的输入速度比cin快得多
//但是scanf是c语言的函数,不支持string对象
scanf("%s",&ss);
//把整个字符数组赋值给string对象
s=ss;
cout << s <<endl; //整体输出scanf对象
s=s+'a';//尾部添加单个字符 用+
cout << s << endl;
s=s+"asd";//尾部追加字符串 +
cout << s << endl;
s.append("qwe");//尾部追加字符串 append()方法
cout << s << endl;
//插入字符
string::iterator it;//定义迭代器
it=s.begin();//迭代器位置为字符串首
s.insert(it+1,'#');//把字符插入第一个字符前 从0开始 即放在位置1
cout << s << endl;
//访问string对象元素
cout << s[0] << endl;
cout << s[0]-'a' << endl;//两个相同的字符相减为0
//删除
s.erase(it+3);
cout << s << endl; //删除第3个元素,从0开始
s.erase(it,it+4); //删除0-4区间的所有元素 (包括s[0]和s[4])
cout << s << endl;
//替换string对象的字符
s.replace(3,3,"good"); //从第3个开始,连续3个字符,替换为good
cout << s << endl;
//搜索string对象的元素或子串
cout << s.find('s') << endl;//查找第一个字符s并返回下标值
cout << s.find("qw") << endl; //查找第一个字符串s并返回下标值,找不到返回乱码
cout << s.length() << endl; //输出字符串的长度
s=""; //清空字符串
cout << s.empty() << endl; //判断字符串是否为空
//string对象的比较
string s1,s2;
s1="jhk";s2="idofoo";
cout << s1.compare(s2) << endl; //s1>s2返回1,s1=s2返回0,s1<s2返回-1
//reverse反向排序 即反向输出
//reverse() 头文件#include <algorithm>
s1="35712168";
reverse(s1.begin(),s1.end());
cout << s1 << endl;
//string对作为vector元素 类似于字符串数组
//头文件 #include <vector>
vector<string> v;
v.push_back("Amy");
v.push_back("Tom");
cout << v[0] << ' ' << v[1] << endl;
cout << v[0][0] << endl;
cout << v[1].length() << endl;
//string类型的数字化处理
//字符串->数字
string a="45681";//求一个整数各位的和
int i,sum=0;
for(i=0;i<a.length();i++){
if(a[i]=='0'){
sum+=0;
}
else if(a[i]=='1'){
sum+=1;
}
else if(a[i]=='2'){
sum+=2;
}
else if(a[i]=='3'){
sum+=3;
}
else if(a[i]=='4'){
sum+=4;
}
else if(a[i]=='5'){
sum+=5;
}else if(a[i]=='6'){
sum+=6;
}else if(a[i]=='7'){
sum+=7;
}else if(a[i]=='8'){
sum+=8;
}else if(a[i]=='9'){
sum+=9;
}
}
cout << sum << endl;
s="asdsd";
printf(s.c_str());//printf输出string对象
cout << endl;
//string对象与sscanf函数
//sscanf函数可以把一个字符串按你需要的方式分离出子串,甚至是数字
string str1,str2,str3;
char sa[100],sb[100],sc[100];
//将字符串分离成子串,分隔符为空格
sscanf("asd 123 vff 89","%s %s %s",sa,sb,sc);
str1=sa;
str2=sb;
str3=sc;
cout << str1 << ' ' << str2 << ' ' << str3 << endl;
//将字符串 分离成数字,分隔符为空格
//当用到数字的时候,和scanf一样,需要传指针地址
int a1,a2,a3;
sscanf("7 4 1","%d %d %d",&a1,&a2,&a3);
cout << a1 << ' ' << a2 << ' ' << a3 << endl;
//将字符串分离成数字,分隔符为","和"$"
int x,y,z;
sscanf("54,9$3","%d,%d$%d",&x,&y,&z);
cout << x << ' ' << y << ' ' << z <<endl;
//string对象与数值相互转换
//将数值转化成string c方法
char b[10];
string bb;
sprintf(b,"%d",4567); //只能转换给char型字符数组
bb=b;
cout << bb << endl;
//将数值转化成string c++方法
string cc=convertToString(1998); //自定义函数
cout << cc << endl;
//将string转换为数值,c++方法
string dd="20001108";
int p=convertFromString(dd); //自定义函数
cout << p << endl;
return 0;
}