前言:
C语言只提供了一个char类型用来处理字符,二对于字符串,只能通过字符串数组来处理,显得十分不便。C++STL提供了string基本字符系列容器来处理字符串,可以把string理解为字符串类,他提供了添加,删除,替换,查找和比较等丰富的方法。
显然使用vector<char>这样的向量也可以处理字符串,但是功能比不上string。向量的元素类型可以是string,如vector<char>这样的向量,实际上就类似于C语言中的字符串数组。
使用string容器,需要头文件包含声明“#include<string>”.
1.创建string对象
下面这段代码创建了字符串对对像s,s是一个空字符串,其长度为0:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s;
cout<<s.length()<<endl;
return 0;
}
输出:
2.给string对象赋值
给string对象赋值一半有两种方式。
(1)直接给字符串对象赋值,如:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s;
s="hello,C++STL.";
cout<<s<<endl;
return 0;
}
输出:
(2)更常用的方法是,把字符指针赋给一个字符串对象;
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s;
char ss[5000];
scanf("%s",ss);
s=ss;
cout<<s<<endl;
return 0;
}
输出:
3.从string对象尾部添加字符
在string对象的尾部添加一个字符(char),采用“+”操作符即可:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s;
s=s+'a';
s=s+'b';
s=s+'c';
cout<<s<<endl;
return 0;
}
输出:
4.从string对象的尾部追加字符串
从尾部追加的方式有两种:
(1)直接用“+”操作符:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s;
s=s+"abc";
s=s+"123";
cout<<s<<endl;
return 0;
}
输出:
(2)采用append()方法:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s;
s.append("abc");
s.append("123");
cout<<s<<endl;
return 0;
}
输出:
5.给string对象插入字符
可以使用insert()方法把一个字符串插入到迭代器位置之前:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s;
s.append("123456");
string::iterator it;
it=s.begin();
s.insert(it+1,'P');//插入到第一个字符前面,字符的下标从0开始;
cout<<s<<endl;
return 0;
}
输出:
6.访问string对象的元素
一般使用下标方式随机访问string对象的元素,下标是从0开始计数的。另外,string对象的元素是一个字符(char),这一点一定要清楚。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s;
s.append("abc123456");
cout<<s[0]<<endl;
return 0;
}
输出:
7.删除string对象的元素
(1)清空一个字符串,直接给它赋空字符串即可;
(2)使用erase()方法删除迭代器所指的那一个元素或者一个区间中所有的元素。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s;
s="abc123456";
cout<<s<<endl;
string::iterator it;
it=s.begin();
s.erase(it+3);//删除第3个字符,下标从0开始;
cout<<s<<endl;
s.erase(it,it+4);//删除0--4这个区间的所有字符;
cout<<s<<endl;
return 0;
}
输出:
8.返回string对象的长度
采用length()方法可返回字符串的长度;
采用empty()方法可以返回字符串是否为空;
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s;
s="abc123456";
cout<<s.length()<<endl;//输出字符串的长度;
s="";
cout<<s.empty()<<endl;//判断字符串是否为空,空输出,非空输出0;
return 0;
}
输出:
9.替换string对象的字符
使用replace()方法可以很方便地替换string对象中的字符,replace()方法的重载函数相当多,常用的只有一两个:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s;
s="abc123456";
s.replace(3,3,"good");//从第3个开始,将连续的3个字符替换成“good”;
cout<<s<<endl;
return 0;
}
输出:
10.搜索string对象的元素或子串
采用find()方法可查找字符串中的第一个字符串(char,用单引号界定)或者子串(用双引号界定),如果查到,则返回下标值(从0开始),如果查不到,则返回4294967295。
find()方法有很多重载函数:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s;
s="cat dog cat";
cout<<s.find('c')<<endl;//返回下标值;
cout<<s.find("c")<<endl;
cout<<s.find("cat")<<endl;
cout<<s.find("dog")<<endl;
cout<<s.find("dogc")<<endl;
return 0;
}
输出:
11.string对象的比较
string对象可使用compare()方法与其他字符串比较。如果它比对方大,则返回1;如果比它小,返回-1,;如果它与对方相同,则返回0.
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s="cat dog cat";
cout<<s.compare("a")<<endl;//
cout<<s.compare("cat dog cat")<<endl;
cout<<s.compare("dog")<<endl;
return 0;
}
输出:
12.reverse()反向排序
用reverse()方法可将string对象迭代器所指向的一段区间中的元素(字符)反向排序。reverse()方法需要声明头文件“#include<algorithm>”。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s="123456789";
reverse(s.begin(),s.end());
cout<<s<<endl;
return 0;
}
输出:
13.string和vector
string对象可以作为vector向量的元素,这种用法,类似于字符串数组。
#include<string>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
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;
return 0;
}
输出:
14.数字分离
在ACM竞赛中,常常需要将读入的数字的每一位分离出来,如果采用取余的方法,花费的时间就会很长,这时候,我们可以将读入的数据当成字符串来处理,这样就方便,省时多了。下面这份代码演示了求一个整数各位的和:
#include<string>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s;
s="1234059";
int sum=0;
for(int i=0;i<s.length();i++)
{
sum+=s[i]-'0';
}
cout<<sum<<endl;
return 0;
}
输出:
15.string对象与字符串数组互操作
下面这个程序演示了字符数组与string对象的输入与输出:
#include<string>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s;
char ss[101];
scanf("%s",ss);
s=ss;
printf(s.c_str());
cout<<endl;
printf("%s\n",ss);
cout<<s<<endl;
cout<<ss<<endl;
return 0;
}
输出:
16.string对象与sscanf函数
在C语言中,sscanf函数很管用,它可以把一个字符串按你需要的方式分理处子串,甚至是数字。
#include<string>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s1,s2,s3;
char sa[101],sb[101],sc[101];
sscanf("abc 123 pc","%s %s %s",sa,sb,sc);
s1=sa;
s2=sb;
s3=sc;
cout<<s1<<" "<<s2<<" "<<s3<<endl;
int a,b,c;
sscanf("1 2 3","%d %d %d",&a,&b,&c);//当用到数字时,像scanf一样,它要传指针地址;
cout<<a<<" "<<b<<" "<<c<<endl;
int x,y,z;
sscanf("4,5$6","%d,%d$%d",&x,&y,&z);//将字符串分离成数字,分隔符是","和“$";
cout<<x<<" "<<y<<" "<<z<<endl;
return 0;
}
输出:
17.string对象与数值相互转换
有时候,string对象与数值之间需要相互转换,下面这个例子详细说明了如何完成这个操作:
#include<string>
#include<sstream>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
string convertToString(double x)//C++方法,将数值转换成string;
{
ostringstream o;
if(o<<x)
return o.str();
return "conversion error";
}
double convertFromString(const string &s)//C++方法,将string转换成数值;
{
istringstream i(s);
double x;
if(i>>x)
return x;
return 0.0;
}
int main()
{
string a;
char b[101];
sprintf(b,"%d",1975);
a=b;
cout<<a<<endl;
string cc=convertToString(1976);
cout<<cc<<endl;
string dd="2006";
int p=convertFromString(dd)+2;//数值+2;
cout<<p<<endl;
return 0;
}
输出: