C++标准库中string类以类型的形式对字符串进行封装,使得它除了像一个存储字符的容器外,更加包含了字符序列的处理操作。
string类所有函数
string类的所有成员函数 | |
函数名称 | 实现功能 |
构造函数 | 产生或者复制字符串 |
析构函数 | 销毁字符串 |
assign,= | 赋值 |
Swap | 交换两个字符串的内容 |
append(),push_back(),+= | 添加字符 |
insert() | 插入字符 |
erase() | 删除字符 |
clear() | 移除全部字符 |
resize() | 改变字符数量 |
replace() | 替换字符 |
+ | 串联字符串 |
compare(),==,!,<,<=,>,>= | 比较字符串内容 |
size(),length | 返回字符数量 |
max_size() | 返回字符的最大可能个数 |
empty() | 判断字符串是否为空 |
capacity() | 返回重新分配之前的字符容量 |
reserve() | 保留内存以存储一定数量的字符 |
[],at() | 存取单一字符 |
>>,geline() | 从stream中读取某值 |
<< | 将值写入stream |
copy() | 将内容复制为一个C-string |
c_str() | 将内容以C-string形式返回 |
data() | 将内容以字符数组形式返回 |
substr() | 返回子字符串 |
find() | 搜寻某子字符串或字符 |
begin(),end() | 提供正向迭代器支持 |
rbrgin(),rend() | 提供逆向迭代器支持 |
get_allocator() | 返回配置器 |
string类包含头文件
#include <string>
声明一个string类对象
string Str;
既然是一个类,就有构造函数和析构函数。上面的声明没有传入参数,所以直接使用了string的默认构造函数,目的是初始化为一个空字符串。string类的构造函数和析构函数:
string s; //生成一个空字符串s
string s(str) //拷贝构造函数 生成str的复制品
string s(str,stridx) //将字符串str内“始于位置stridx”的部分当作字符串的初值
string s(str,stridx,strlen) //将字符串str内“始于stridx且长度顶多strlen”的部分作为字符串的初值
string s(cstr) //将C字符串作为s的初值
string s(chars,chars_len) //将C字符串前chars_len个字符作为字符串s的初值。
string s(num,c) //生成一个字符串,包含num个c字符
string s(beg,end) //以区间beg;end(不包含end)内的字符作为字符串s的初值
s.~string() //销毁所有字符,释放内存
最常用的是:
string s(str) //字符串的初始值为str
直接将字符串作为构造函数的参数。
注意:不能使用字符或者整数去初始化字符串。例如:
string str('x');
string字符串的输入与输出
示例代码
/************************************************************************
<*@创建者:OYXL
<*@创建时间:2018/6/8
<*@程序功能:用于string的输入与输出
<*@注意:cin输入的字符串中不能有空格,用getline()函数可以解决
************************************************************************/
#include "iostream"
#include "string"
using namespace std;
int main()
{
char s1[20];
string s2;
cout<<"input a string: ";
cin>>s1;
cout<<"input a string again: ";
cin>>s2;
cout<<"s1 is :"<<s1<<endl;
cout<<"s2 is :"<<s2<<endl;
system("pause");
return 0;
}
显示结果
C++ cin不支持输入空格,用getline()函数可以解决,下面是博客链接:
string的基本操作
赋值和拼接
string类的赋值和拼接都是对字符串的赋值操作。赋值是将原有的字符串舍弃,用新的字符串代替。拼接则是不舍弃原有的字符串,仅将新的字符串连接在原有的字符串的末尾。
1、赋值
=和assign() 函数都可以用来给字符串进行赋值
assign() 函数的函数声明:
//用c类型字符串s赋值
string &assign(const char *s);
//用c类型字符串s开始的n个字符赋值
string &assign(const char *s,int n);
//把型字符串s赋给当前字符串
string &assign(const string &s);
//用n个字符c赋值给当前字符串
string &assign(int n,char c);
//把字符串s中从start开始的n个字符赋给当前字符串
string &assign(const string &s,int start,int n);
//把first和last迭代器之间的部分赋给字符串
string &assign(const_iterator first,const_iterator last);
示例代码:
# include <iostream>
# include <string>
using namespace std;
int main()
{
string str1 = "hello world";
string str2 ("see you again");
string str3,str4;
str3.assign(str2,3,6);
str4.assign(str2,3,string::npos);
cout<<"str4 is :"<<str4<<endl;
str4.assign("love");
cout<<"str4 is :"<<str4<<endl;
str4.assign("nice",5);//大于等于4都可以
cout<<"str4 is :"<<str4<<endl;
str4.assign(5,'x');
cout<<"str1 is :"<<str1<<endl;
cout<<"str2 is :"<<str2<<endl;
cout<<"str3 is :"<<str3<<endl;
cout<<"str4 is :"<<str4<<endl;
system("pause");
return 0;
}
显示结果
2、拼接
+=,append(),push_back() 都可以用来拼接字符串
示例代码
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s,s1(" world");
s+="hello";
cout<<s<<endl;
s+=s1;
cout<<s<<endl;
system("pause");
return 0;
}
显示结果
append()函数声明
//把c类型字符串s连接到当前字符串结尾
string &append(const char *s);
//把c类型字符串s的前n个字符连接到当前字符串结尾
string &append(const char *s,int n);
//将string对象s的字符串连接到当前字符串的末尾
string &append(const string &s);
//在当前字符串结尾添加n个字符c
string &append(int n,char c);
//把迭代器first和last之间的部分连接到当前字符串的结尾
string &append(const_iterator first,const_iterator last);
//把字符串s中从pos开始的n个字符连接到当前字符串的结尾
string &append(const string &s,int pos,int n);
比较字符串
==,!=,<,<=,>,>=,compare() 函数用于进行比较字符串操作。比较方法是依次从字符串的初始位置两两比较对应位置字符的大小,直到有不同的字符或字符串结束为止。
示例代码
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("hello"),s2("world");
cout<<"s1<s2 is ";
cout<<((s1<s2)?"true":"false")<<endl;
cout<<"s1>s2 is ";
cout<<((s1>s2)?"true":"false")<<endl;
cout<<"s1<=s2 is "<<endl;
cout<<((s1<=s2)?"true":"false")<<endl;
cout<<"s1>=s2 is "<<endl;
cout<<((s1>=s2)?"true":"false")<<endl;
cout<<"s1!=s2 is "<<endl;
cout<<((s1!=s2)?"true":"false")<<endl;
system("pause");
return 0;
}
因为第一个字符就是不同的,所以就比较第一个字符大小。
显示结果
compare() 函数声明
//比较当前string对象和s的大小
int compare(const string &s) const;
int compare(const char *s) const;
//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int compare(int pos,int n,const string &s) const;
int compare(int pos,int n,const char *s) const;
//比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符的字符串的大小
int compare(int pos,int n,const char *s,int pos2) const;
int compare(int pos,int n,const char *s,int pos2,int n2) const;
子串
substr()函数用于获取字符串对象的子字符串。该函数返回string对象的某个子串。其中pos为子串的初始位置,如果不设置,则默认为从0开始,npos为子串的长度。
示例代码
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("hello world!");
string s1,s2;
s1=s.substr(6,5);
s2=s.substr(0,5);
cout<<"s1 : "<<s1<<endl;
cout<<"s2 : "<<s2<<endl;
system("pause");
return 0;
}
显示结果
交换字符串
swap() 函数用于交换字符串
示例代码
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "see you again";
string str2 ("nice to meet you");
str2.swap(str1);
cout<<str1<<endl;
cout<<str2<<endl;
system("pause");
return 0;
}
显示结果
字符插入
insert()函数用于插入一个或者多个字符,需要注意的是string对象中字符的初始位置从0开始。
示例代码
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("hello world!");
cout<<"s1 : "<<s1<<endl;
s1.insert(6,"my ");
cout<<"s1 : "<<s1<<endl;
string s2("program ");
s1.insert(9,s2);
cout<<"s1 : "<<s1<<endl;
system("pause");
return 0;
}
显示结果
替换字符
replace() 函数功能是用一个字符串将当前字符串中的某个子串做替换。如果替换字符串与子串的长度不等,则还需要对字符数组进行移动。当字符串中的全部字符被替换成了另一个字符串时,替换字符就变成了赋值。
查找字符串
1、find()函数
find()函数是按字符串从左往右的顺序进行查找。默认查找的初始位置为字符串首字符。一旦查找到当前字符串有匹配的字符或者字符串是,就返回该字符或字符串的首字符下标。
2、rfind()函数
rfind()函数是按字符串逆序查找,从左往右开始查找。
3、find_first_of()函数
find_first_of()函数是查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。
返回字符数量
size(),length()这两个函数会返回string类型对象中的字符个数,且它们的执行效果相同。
max_size()函数返回string类型对象最多包含的字符数。一旦程序使用长度超过max_size()的string操作,编译器会抛出length_error异常。
capacity()函数返回在重新分配内存之前,string类型对象所能包含的最大字符数。
reserve()函数可以为string类型对象重新分配内存。重新分配的大小由其参数决定。reserve()的默认参数为0。
判断字符串是否为空
empty()
删除字符
erase()函数和clear()函数
参考:
https://blog.csdn.net/fenxinzi557/article/details/51457829
《C++入门很简单》