《C++ string类》

转载链接: https://blog.csdn.net/mars_xiaolei/article/details/80623116

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类的构造函数和析构函数:


 
 
  1. string s; //生成一个空字符串s
  2. string s(str) //拷贝构造函数 生成str的复制品
  3. string s (str,stridx) //将字符串str内“始于位置stridx”的部分当作字符串的初值
  4. string s (str,stridx,strlen) //将字符串str内“始于stridx且长度顶多strlen”的部分作为字符串的初值
  5. string s (cstr) //将C字符串作为s的初值
  6. string s (chars,chars_len) //将C字符串前chars_len个字符作为字符串s的初值。
  7. string s (num,c) //生成一个字符串,包含num个c字符
  8. string s (beg,end) //以区间beg;end(不包含end)内的字符作为字符串s的初值
  9. s.~ string () //销毁所有字符,释放内存

最常用的是:

string s(str) //字符串的初始值为str
 
 

直接将字符串作为构造函数的参数。

注意:不能使用字符或者整数去初始化字符串。例如:

string str('x');
 
 

string字符串的输入与输出

示例代码


 
 
  1. /************************************************************************
  2. <*@创建者:OYXL
  3. <*@创建时间:2018/6/8
  4. <*@程序功能:用于string的输入与输出
  5. <*@注意:cin输入的字符串中不能有空格,用getline()函数可以解决
  6. ************************************************************************/
  7. #include "iostream"
  8. #include "string"
  9. using namespace std;
  10. int main()
  11. {
  12. char s1[ 20];
  13. string s2;
  14. cout<< "input a string: ";
  15. cin>>s1;
  16. cout<< "input a string again: ";
  17. cin>>s2;
  18. cout<< "s1 is :"<<s1<< endl;
  19. cout<< "s2 is :"<<s2<< endl;
  20. system( "pause");
  21. return 0;
  22. }

显示结果

C++ cin不支持输入空格,用getline()函数可以解决,下面是博客链接:

https://blog.csdn.net/EXLsunshine/article/details/28440629

 

string的基本操作


赋值和拼接

string类的赋值和拼接都是对字符串的赋值操作。赋值是将原有的字符串舍弃,用新的字符串代替。拼接则是不舍弃原有的字符串,仅将新的字符串连接在原有的字符串的末尾。

1、赋值

=和assign() 函数都可以用来给字符串进行赋值

assign() 函数的函数声明:


 
 
  1. //用c类型字符串s赋值
  2. string &assign(const char *s);
  3. //用c类型字符串s开始的n个字符赋值
  4. string &assign(const char *s,int n);
  5. //把型字符串s赋给当前字符串
  6. string &assign(const string &s);
  7. //用n个字符c赋值给当前字符串
  8. string &assign(int n,char c);
  9. //把字符串s中从start开始的n个字符赋给当前字符串
  10. string &assign(const string &s,int start,int n);
  11. //把first和last迭代器之间的部分赋给字符串
  12. string &assign(const_iterator first,const_iterator last);

示例代码:


 
 
  1. # include <iostream>
  2. # include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string str1 = "hello world";
  7. string str2 ("see you again");
  8. string str3,str4;
  9. str3.assign(str2, 3, 6);
  10. str4.assign(str2, 3, string::npos);
  11. cout<< "str4 is :"<<str4<< endl;
  12. str4.assign( "love");
  13. cout<< "str4 is :"<<str4<< endl;
  14. str4.assign( "nice", 5); //大于等于4都可以
  15. cout<< "str4 is :"<<str4<< endl;
  16. str4.assign( 5, 'x');
  17. cout<< "str1 is :"<<str1<< endl;
  18. cout<< "str2 is :"<<str2<< endl;
  19. cout<< "str3 is :"<<str3<< endl;
  20. cout<< "str4 is :"<<str4<< endl;
  21. system( "pause");
  22. return 0;
  23. }

显示结果

2、拼接

+=,append(),push_back() 都可以用来拼接字符串

示例代码


 
 
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string s,s1( " world");
  7. s+= "hello";
  8. cout<<s<< endl;
  9. s+=s1;
  10. cout<<s<< endl;
  11. system( "pause");
  12. return 0;
  13. }

显示结果

append()函数声明


 
 
  1. //把c类型字符串s连接到当前字符串结尾
  2. string &append(const char *s);
  3. //把c类型字符串s的前n个字符连接到当前字符串结尾
  4. string &append(const char *s,int n);
  5. //将string对象s的字符串连接到当前字符串的末尾
  6. string &append(const string &s);
  7. //在当前字符串结尾添加n个字符c
  8. string &append(int n,char c);
  9. //把迭代器first和last之间的部分连接到当前字符串的结尾
  10. string &append(const_iterator first,const_iterator last);
  11. //把字符串s中从pos开始的n个字符连接到当前字符串的结尾
  12. string &append(const string &s,int pos,int n);

比较字符串

==,!=,<,<=,>,>=,compare() 函数用于进行比较字符串操作。比较方法是依次从字符串的初始位置两两比较对应位置字符的大小,直到有不同的字符或字符串结束为止。

示例代码


 
 
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string s1("hello"),s2("world");
  7. cout<< "s1<s2 is ";
  8. cout<<((s1<s2)? "true": "false")<< endl;
  9. cout<< "s1>s2 is ";
  10. cout<<((s1>s2)? "true": "false")<< endl;
  11. cout<< "s1<=s2 is "<< endl;
  12. cout<<((s1<=s2)? "true": "false")<< endl;
  13. cout<< "s1>=s2 is "<< endl;
  14. cout<<((s1>=s2)? "true": "false")<< endl;
  15. cout<< "s1!=s2 is "<< endl;
  16. cout<<((s1!=s2)? "true": "false")<< endl;
  17. system( "pause");
  18. return 0;
  19. }

因为第一个字符就是不同的,所以就比较第一个字符大小。

显示结果

compare() 函数声明


 
 
  1. //比较当前string对象和s的大小
  2. int compare(const string &s) const;
  3. int compare(const char *s) const;
  4. //比较当前字符串从pos开始的n个字符组成的字符串与s的大小
  5. int compare(int pos,int n,const string &s) const;
  6. int compare(int pos,int n,const char *s) const;
  7. //比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符的字符串的大小
  8. int compare(int pos,int n,const char *s,int pos2) const;
  9. int compare(int pos,int n,const char *s,int pos2,int n2) const;

子串

substr()函数用于获取字符串对象的子字符串。该函数返回string对象的某个子串。其中pos为子串的初始位置,如果不设置,则默认为从0开始,npos为子串的长度。

示例代码


 
 
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string s("hello world!");
  7. string s1,s2;
  8. s1=s.substr( 6, 5);
  9. s2=s.substr( 0, 5);
  10. cout<< "s1 : "<<s1<< endl;
  11. cout<< "s2 : "<<s2<< endl;
  12. system( "pause");
  13. return 0;
  14. }

显示结果

交换字符串

swap() 函数用于交换字符串

示例代码


 
 
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string str1 = "see you again";
  7. string str2 ("nice to meet you");
  8. str2.swap(str1);
  9. cout<<str1<< endl;
  10. cout<<str2<< endl;
  11. system( "pause");
  12. return 0;
  13. }

显示结果

字符插入

 insert()函数用于插入一个或者多个字符,需要注意的是string对象中字符的初始位置从0开始。

示例代码


 
 
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string s1("hello world!");
  7. cout<< "s1 : "<<s1<< endl;
  8. s1.insert( 6, "my ");
  9. cout<< "s1 : "<<s1<< endl;
  10. string s2("program ");
  11. s1.insert( 9,s2);
  12. cout<< "s1 : "<<s1<< endl;
  13. system( "pause");
  14. return 0;
  15. }

显示结果

替换字符

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++入门很简单》

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值