string(s小写)是C++标准库中的类,纯C中没有,使用时需要包含头文件#include
//string的定义及初始化 string s1 = "hello"; //初始化字符串 string s2 ("world"); //另一种初始化 string s3; //初始化字符串,空字符串 string s4(5, 'a'); //s4由连续5个a组成,即s4="aaaaa"; string s5(s1,2,3); //从s1的2位置的字符开始,连续3个字符赋值给s5,即s5="llo"; string s6(s1, 1); //从s1的2位置的字符开始,将后续的所有字符赋值给s6,即s6="ello";
string 类提供的各种操作函数大致分为八类:构造器和析构器、大小和容量、元素存取、字符串比较、字符串修改、字符串接合、I/O 操作以及搜索和查找。
一、构造器和析构器
构造函数:用于产生和复制字符串
析构函数:用于销毁字符串
二、大小和容量
int capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素个数)
例如:
string s ; s.capacity(); //返回结果为15;
int max_size()const; //返回string对象中可存放的最大字符串的长度
例如:
string s1; s1.max_size(); //返回2147483647
int size()const; //返回当前字符串的大小
int length()const; //返回当前字符串的长度
bool empty()const; //当前字符串是否为空
void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分
例如:
#include#include<string> using namespace std; int main() { string s = "Student"; string s1; cout <<"s1的容量为:"< endl; cout<<"s1中可存放的最大字符串的长度为:"< endl; cout<<"s字符串的大小为:"< endl; cout<<"s字符串的长度为:"< endl; cout<<"s字符串是否为空:"< endl; s.resize(10,'a'); cout <<"字符填充后"< endl; return 0; } 输出结果为: s1的容量为:15 s1中可存放的最大字符串的长度为:2147483647 s字符串的大小为:7 s字符串的长度为:7 s字符串是否为空:0 字符填充后Studentaaa
三、元素存取
3.1 元素的取操作
const char &operator[](int n)const;
const char &at(int n)const;
char &operator[](int n);
char &at(int n);
operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。
#include<string> using namespace std; int main() { string s = "Student"; cout<<"s的第一个字符:"<
#include0)<<endl; cout<<"s的第二个字符:"< 1]<<endl; return 0; } 输出结果为: s的第一个字符:S s的第二个字符:t
也可以使用STL特有的迭代器
例如:
string::iterator it; for (it = s1.begin(); it != s1.end(); it++){ cout << *it << endl; } cout << *(s1.begin()); //正确,即访问s1[0] cout << *(s1.end()); //错误,s1.end()指向了空
若想要从后向前遍历string时,可以用到rbegin()和rend()函数。
#include#include<string> using namespace std; int main() { string s1 = "Student"; string::reverse_iterator it; for (it = s1.rbegin(); it != s1.rend(); it++) cout << *it; cout<<endl; cout << *(s1.rbegin())< return 0; } 输出结果为: tnedutS t
3.2 元素的存操作
string的赋值:
string &operator=(const string &s);//把字符串s赋给当前字符串
string &assign(const char *s);//用c类型字符串s赋值
string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值
string &assign(const string &s);//把字符串s赋给当前字符串
string &assign(int n,char c);//用n个字符c赋值给当前字符串
string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串
string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串
#include#include<string> using namespace std; int main() { string s1 = "We"; string s2 = "You"; char* pstr = "are"; cout<<"赋值前s1的值:"< endl; s1 = s2; cout<<"将s2赋值给s1后:"< endl; s1.assign(pstr); cout << "利用assign将c字符串赋给s1后:"< endl; return 0; } 输出结果为: 赋值前s1的值:We 将s2赋值给s1后:You 利用assign将c字符串赋给s1后:are
四、字符串比较
bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等
运算符">","<",">=","<=","!="均被重载用于字符串的比较;
int compare(const string &s) const;//比较当前字符串和s的大小
int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小
int compare(const char *s) const;
int compare(int pos, int n,const char *s) const;
int compare(int pos, int n,const char *s, int pos2) const;
compare函数在>时返回1,<时返回-1,==时返回0
例如:
int main() { string input1 = "My name is lisa"; string input2 = "My name is A"; int result = input1.compare(input2); cout<<"比较结果为:"<endl;
cout<<(input1==input2)<return 0;
}
输出结果:
比较结果为:1
0
1
五、字符串修改
string转char*
const char *data()const;//返回一个非null终止的c字符数组,即转换为c语言式的char* 字符串。
const char *c_str()const;//返回一个以null终止的c字符串
string拷贝:
int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目
string的交换:
void swap(string &s2); //交换当前字符串与s2的值
string类的替换函数:
string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符c
string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字符串s
string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字符
string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之间的部分替换为串s
string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符c
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之间的部分替换成[first,last)之间的字符串
string类的插入函数:
string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n);
string &insert(int p0,const string &s);
string &insert(int p0,const string &s, int pos, int n); //前4个函数在p0位置插入字符串s中pos开始的前n个字符
string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c
iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符
void insert(iterator it, int n, char c);//在it处插入n个字符c
1 插入insert() 2 string s1 = "hello"; 3 s1.insert(1,"ins"); //从s1的1位置开始,插入"ins"字符串,即s1="hinsello"; 4 s1.insert(1, "ins", 2);//从s1的1位置开始,插入"ins"字符串的前2个字符,即s1="hinello"; 5 s1.insert(1, "ins", 1, 2);//从s1的1位置开始,插入"ins"字符串的从1位置开始的2个字符,即s1="hnsello"; 6 7 iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
string类的删除:
iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置
iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置
string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串
六、字符串接合
string的连接:
string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾
string &append(const char *s); //把c类型字符串s连接到当前字符串结尾
string &append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前字符串结尾
string &append(const string &s); //同operator+=()
string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾
string &append(int n,char c); //在当前字符串结尾添加n个字符c
string &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾
#include#include using namespace std; int main() { string s1 = "You"; string s2 = " are"; char* pstr = " my friend"; cout<<"连接前s1的值:"< endl; s1 += s2; cout<<"将s2连接后:"< endl; s1.append(pstr); cout << "将pstr连接后:"< endl; return 0; } 输出结果为: 连接前s1的值:You 将s2连接后:You are 将pstr连接后:You are my friend
使用 “+” 时,要保证两边至少有一个string类型
1:s1 += s2;
2:s1 = s2 + s3;
3:s1 == s2;
4:s1 = "s" + s2; //正确
5:s1 = "s" + "s"; //错误,加号两边至少要有一个string类型的对象
6:s1 = "s" + s2 + "s" + "s"; //正确
“+”的两边要保证至少有一个string类型,所以5正确,6错误。由于在C/C++中,+的返回值还是原类型,所以第7行中,"s"+s2返回一个string类型,因此string+“s”也是正确的。
七、I/O操作
string类的输入输出操作:
string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。
函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符'\n'分开。
例如:
int main() { string input; cout<<"请输入字符串:";//输入字符串"My name is lisa" cin>>input;//方法一:只能接收没有空格的字符串,cin将空格和回车作为结束标志 cout<endl; return 0; } 输出结果: 请输入字符串:My name is lisa My int main() { string input1; cout<<"请输入字符串:"; getline(cin,input1);//方法二:可接收有空格的字符串 cout<endl; return 0; } 输出结果: 请输入字符串:My name is lisa My name is lisa
字符串流处理:
通过定义ostringstream和istringstream变量实现,
例如:
string input("hello,this is a test"); istringstream is(input); string s1,s2,s3,s4; is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test" ostringstream os; os<s4; cout<
八、字符串的搜索和查找
string的子串
string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串
string的查找
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值
int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const; //从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,
//成功返回所在位置,失败时返回string::npos的值
int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const; //从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。
//查找失败返回string::npos
int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const; //从当前串中查找第一个不在串s中的字符出现的位置,
//失败返回string::npos
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const;
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找