#include<string>
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<sstream>
using namespace std;
///c++方法,将数值转换为string
string convertTOString(double x){
ostringstream o;
if(o << x)
return o.str();
return "concersion error";///if error
}
///c++方法,将string转换为数值
double convertFromString(const string &s){
istringstream i(s);
double x;
if(i >> x)
return x;
return 0.0;///if error
}
int main(){
///创建字符对象s
string s;
cout << s.length() <<endl;
///对s赋值
s = "Hello world";
cout << s << endl;
///在string对象尾部添加字符
s = s + 'a';
s = s + 'b';
s = s + 'c';
cout << s << endl;
///在string对象尾部添加字符串
///1.采用"+"操作符
s += "abc";
///2.采用append()方法
s.append("123");
cout << s << endl;
///给string对象插入字符
s.clear();
s = "111111";
///定义迭代器
string::iterator it;
it = s.begin();
s.insert(it+1,'p');
cout << s << endl;
///访问string对象的元素
///通过下标访问
s.clear();
s = "123456";
cout << s[3] << endl;
///删除string对象的元素
///定义迭代器变量指向首元素
it = s.begin();
s.erase(it+3);
cout << s << endl;
s.erase(it,it+4);
cout << s << endl;
///返回string对象的长度
printf("长度:\n");
cout << s.size() << endl;
printf("是否为空:\n");
cout << s.empty() << endl;
///替换string对象的字符
s = "123456789";
cout << s << endl;
s.replace(3,3,"good");
cout << s << endl;
///搜索string对象的元素或字串
s = "cat dog cat";
///查找第一个子符'c',返回下标值
cout << s.find('c') << endl;
///查找第一个子串"c",返回下标值
cout << s.find("c") << endl;
///查找第一个子串"cat",返回下标值
cout << s.find("cat") << endl;
///查找第一个子串"dog",返回下标值
cout << s.find("dog") << endl;
///查找第一个子串"dogc",不存在返回4294967295
cout << s.find("dogc") << endl;
///string对象的比较
///s比cat大,返回1;
cout << s.compare("cat") << endl;
///s和"cat dog cat"相等返回0
cout << s.compare("cat dog cat") << endl;
///s比"dog"小返回-1;
cout << s.compare("dog") << endl;
///用reverse反向排序string对象
s = "123456789";
reverse(s.begin(),s.end());
cout << s << endl;
///string对象作为vector元素
vector<string> v;
v.push_back("JACK");
v.push_back("MIKE");
v.push_back("TOM");
cout << v[0] << endl;
cout << v[1] << endl;
cout << v[2] << endl;
cout << v[0][0] << endl;
cout << v[1][0] << endl;
cout << v[2].length() << endl;
///string对象与字符数组相互操作
s.clear();
char ss[100] = "abc123";
///字符数组赋值字符串对象
s = ss;
///用printf输出字符串对象用c_str()方法
printf(s.c_str());
printf("\n");
///string对象与sscanf函数
string s1,s2,s3;
char sa[100],sb[100],sc[100];
///将字符串分离成字串,分隔符为空格
sscanf("abc 123 pc","%s %s %s",sa,sb,sc);
s1 = sa;
s2 = sb;
s3 = sc;
cout << s1 << " " << s2 << " " << s3 << endl;
///将字符串分离成数字,分隔符为空格
///当用到数字时,和scanf一样,它要传指针地址
int a,b,c;
sscanf("1 2 3","%d %d %d",&a,&b,&c);
cout << a << " " << b << " " << c << endl;
///将字符串分离成数字,分隔符为“,”和“$”;
int x,y,z;
sscanf("4,5$6","%d,%d$%d",&x,&y,&z);
cout << x << " " << y << " " << z << endl;
///string对象与数值相互转换
///将数值转换为string的c方法
char g[10];
string h;
sprintf(g,"%d",1975);
h = g;
cout << h << endl;
///将数值转换为string的第二种方法:c++版
string cc = convertTOString(1976);
cout << cc << endl;
///string转换为数值的方法:c++版
string dd = "2006";
int p = convertFromString(dd)+2;
cout << p <<endl;
return 0;
}
string基本字符系列容器
最新推荐文章于 2024-04-23 17:06:35 发布
本文详细介绍了C++中字符串(string)的各种操作方法,包括字符串的创建、修改、搜索、比较等基本操作,以及如何实现字符串与数值之间的转换,并展示了字符串在实际应用中的多种技巧。
1702

被折叠的 条评论
为什么被折叠?



