C++中string类的用法

16 篇文章 0 订阅


标准C++中提供的string类得功能也是非常强大的,一般都能满足我们开发项目时使用。而且我们可以讲string当作容器使用 , 而string也支持大部分的容器操作.当我们如遇到大数加减的问题(单开一篇贴子) , string类能够成为你的得力工具. 现将具体用法的一部分罗列如下,只起一个抛砖引玉的作用吧,直接进入正题吧!
要想使用标准C++中string类,必须要包含

#include< string > // 注意是< string >,不是<string.h>,带.h的是C语言中的头文件

using std::string;

using std::wstring;

使用string/wstring了,它们两分别对应着char和wchar_t。

或者

using namespace std;
一般情况下直接使用using namespace std;就好了.

构造函数(constructors)

  string();
  //空字符串
  string( size_type length, char ch );
  //以length为长度的ch的拷贝(即length个ch)
  string( const char *str );
  //以str为初值 (长度任意)
  string( const char *str, size_type length );
  //同上 , 长度不限 , 但要注意不能越界 , 以免发生不可逆转的错误
  string( string &str, size_type index, size_type length );
  //以index为索引开始的子串,长度为length
  string( input_iterator start, input_iterator end );
  //以从start到end的元素为初值.

例如:

	string cc;
	string a(5,'c');
	string b("hello world");
	string b2("wasd",3);
	string c(b);
	string d(b,3,4);
	string e(b.begin(),b.end());
	cout<<cc<<endl;
	cout<<a<<endl;
	cout<<b<<endl;
	cout<<b2<<endl;
	cout<<c<<endl;
	cout<<d<<endl;
	cout<<e<<endl;

在这里插入图片描述

支持的操作符

==  >  <  >=  <=  !=  +  +=  [] 

注意:没有赋值运算符的重载 = ; 但是有中括号运算符的重载[ ];

追加文本(append())

basic_string &append(const basic_string &str);
//在字符串的末尾添加str  
basic_string &append(const char *str);
//在字符串末尾添加str所指向的c风格字符串   
basic_string &append(const basic_string &str,size_type index,size_type len);  
//在字符串的末尾添加str的子串,子串以index索引开始,长度为len  
basic_string &append(const char *str,size_type num);
//在字符串的末尾添加str中的num个字符  
basic_string &append(size_type num,char ch);
//在字符串的末尾添加num个字符ch    
basic_string &append(input_iterator start,input_iterator end);  
//在字符串的末尾添加以迭代器start和end表示的字符序列    
push_back('k');
//把一个字符连接到当前字符串的结尾  

例如:

	string f("hhhhh");
	string e("hello world");
	e.append(f);
	cout<<e<<endl;
	e.append(10,'!');
	cout<<e<<endl;

运行结果:
在这里插入图片描述

赋值(assign())

basic_string &assign(const basic_string &str);
//用str为字符串赋值   
basic_string &assign(const char *str);
//用字符串赋值  
basic_string &assign(const char *str,size_type num);
//用str的开始num个字符为字符串赋值  
basic_string &assign(const basic_string &str,size_type index,size_type len);  
//用str的子串为字符串赋值,子串以index索引开始,长度为len   
basic_string &assign(size_type num,char ch);
//用num个字符ch为字符串赋值  
string &assign(const_iterator begin,const_itertor end);  
//把first和last迭代器之间的部分赋给字符串  

例如:

	string a ;
	string b = "welcome";
	a.assign(b,3,3);
	cout<<a<<endl;

运行结果:
在这里插入图片描述

比较(compare())

int compare(const basic_string &str);
//比较自己和str  

int compare(size_type index,size_type length,const basic_string &str);  
//比较自己的子串和str,子串以index索引开始,长度为length    

int compare(size_type index,size_type length,const basic_string &str,size_type   
            index2,size_type length2);  
//比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己  

int compare(const char *str);
//比较自己和str(字符串)  

int compare(int pos, int n,const char *s)  
//比较自己的子串,从pos开始,n个字符,和s进行比较    

int compare(size_type index,size_type length,const char *str,size_type length2);  
//比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串  
//以index开始,长度为length  

copare()的返回值

返回值比较结果
< 0this < str
0this == str
> 0this > str

插入(insert())

iterator insert(iterator i,const char &ch);
//在迭代器i表示的位置前面插入一个字符ch

basic_string &insert(size_type index,const basic_string &str);
//在字符串的位置index插入字符串str

basic_string &insert(size_type index,const char *str);
//在字符串的位置index插入字符串str

basic_string &insert(size_type index1,const basic_string &str,size_type index2,size_type num);
//在字符串的位置index插入字符串str的子串(从index2开始,长num个字符)

basic_string &insert(size_type index,const char *str,size_type num);
//在字符串的位置index插入字符串str的num个字符

basic_string &insert(size_type index,size_type num,char ch );
//在字符串的位置index插入num个字符ch的拷贝

void insert(iterator i,size_type num,const char &ch);
//在迭代器i表示的位置前面插入num个字符ch的拷贝

void insert(iterator i,iterator begin,iterator end );
//在迭代器i表示的位置前面插入一段字符,从start开始,以end结束

例如:

	string a = "qwert" ;
	string b = "welcome";
	b.insert(0,a);
	cout<<b<<endl;
	b.insert(0,"hahaha");
	cout<<b<<endl;

运行结果:
在这里插入图片描述

删除(earse())

iterator erase(iterator first, iterator last);
//删除[first,last)之间的所有字符,返回删除后迭代器的位置
 
iterator erase(iterator it);
//删除it指向的字符,返回删除后迭代器的位置
 
string &erase(int pos = 0, int n = npos);
//删除pos开始的n个字符,返回修改后的字符串

例如:

	string a = "qwert" ;
	string b = "welcome";
	b.insert(0,a);

	auto it = b.begin();
	auto it2 = b.end();
	//此时的b 是合并了a 和 (原)b的 , 即"qwertwelcome" 
	cout<<b<<endl;
	b.erase(it+3,it2-3);
	cout<<b<<endl;

运行结果:
在这里插入图片描述

替换(replace())

basic_string &replace(size_type index,size_type num,const basic_string &str);
//用str中的num个字符替换本字符串中的字符,从index开始
 
replace(size_type index1,size_type num1,const basic_string &str,size_type index2,size_type num2);
//用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
 
basic_string &replace(size_type index,size_type num,const char *str);
//用str中的num个字符(从index开始)替换本字符串中的字符
 
basic_string &replace(size_type index,size_type num1,const char *str,size_type num2);
//用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
 
basic_string &replace(size_type index,size_type num1,size_type num2,char ch);
//用num2个ch字符替换本字符串中的字符,从index开始,num1个字符
 
basic_string &replace(iterator start,iterator end,const basic_string &str);
//用str中的字符替换本字符串中的字符,迭代器start和end指示范围
 
basic_string &replace(iterator start,iterator end,const char *str);
//用str替换本字符串中的内容,迭代器start和end指示范围
 
basic_string &replace(iterator start,iterator end,const char *str,size_type num );
//用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围
 
basic_string &replace(iterator start,iterator end,size_type num,char ch );
//用num个ch字符替换本字符串中的内容,迭代器start和end指示范围

例如:

	string a = "aaa" ;
	string b = "welcome";
	//用a所有的字符 , 替换b中从 b[2] 开始的 1 个字符
	b.replace(2,1,a);
	cout<<b<<endl;

运行结果:
在这里插入图片描述

查找(find())

  size_type find( const basic_string &str, size_type index );
  //返回str(对象)在字符串中第一次出现的位置(从index开始查找)。如果没找到则返回string::npos
  
  size_type find( const char *str, size_type index );
  //返回str(字符串)在字符串中第一次出现的位置(从index开始查找)。如果没找到则返回string::npos
  
  size_type find( const char *str, size_type index, size_type length );
  //返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos

  size_type find( char ch, size_type index );
  //返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos

函数 find_first_of:查找在字符串中第一个与str中的某个字符匹配的字符
 
    size_type find_first_of( const basic_string &str, size_type index = 0);
 
    size_type find_first_of( const char *str, size_type index = 0 );
 
    size_type find_first_of( const char *str, size_type index, size_type num );
 
    size_type find_first_of( char ch, size_type index = 0 );
 
函数 find_first_not_of:在字符串中查找第一个与str中的字符都不匹配的字符
 
    size_type find_first_not_of( const basic_string &str, size_type index = 0 );
 
    size_type find_first_not_of( const char *str, size_type index = 0 );
 
    size_type find_first_not_of( const char *str, size_type index, size_type num );
 
    size_type find_first_not_of( char ch, size_type index = 0 );
 
函数 find_last_of:在字符串中查找最后一个与str中的某个字符匹配的字符
 
  size_type find_last_of( const basic_string &str, size_type index = npos );
 
  size_type find_last_of( const char *str, size_type index = npos );
 
  size_type find_last_of( const char *str, size_type index, size_type num );
 
  size_type find_last_of( char ch, size_type index = npos );
 
函数 find_last_not_of:在字符串中查找最后一个与str中的字符都不匹配的字符
 
    size_type find_last_not_of( const basic_string &str, size_type index = npos );
 
    size_type find_last_not_of( const char *str, size_type index = npos);
 
    size_type find_last_not_of( const char *str, size_type index, size_type num );
 
    size_type find_last_not_of( char ch, size_type index = npos );
 
rfind函数
 
  size_type rfind( const basic_string &str, size_type index );
  //返回最后一个与str中的某个字符匹配的字符,从index开始查找
 
  size_type rfind( const char *str, size_type index );
  //返回最后一个与str中的某个字符匹配的字符,从index开始查找
 
  size_type rfind( const char *str, size_type index, size_type num );
  //返回最后一个与str中的某个字符匹配的字符,从index开始查找,最多查找num个字符
 
  size_type rfind( char ch, size_type index );
  //返回最后一个与ch匹配的字符,从index开始查找

例如:

	string a = "Alpha Beta Gamma Delta" ;
	unsigned int loc = a.find( "Omega", 0 );
	if( loc != string::npos )
	{
		cout << "Found Omega at " << loc << endl;
	} 
    else
	{
		cout << "Didn't find Omega" << endl;
	}

	string b = "Gamma";
	if( a.find( b, 0 ) != string::npos )
	{
		cout << "Found Gamma at " << endl;
	} 
    else
	{
		cout << "Didn't find Gamma" << endl;
	}

运行结果:
在这里插入图片描述

交换(swap())

void swap( basic_string &str );

例如 :

	string a = "Alpha Beta Gamma Delta" ;
	string b = "abcdefghijklmn";
	cout<< "a:"<< a << endl;
	cout<< "b:"<< b << endl;
	b.swap(a);	
	cout<< "a:"<< a << endl;
	cout<< "b:"<< b << endl;

运行结果 :
在这里插入图片描述

其他函数

push_back函数
     void push_back(value_type_Ch);
     //在字符串之后插入一个字符
pop_back函数
     void pop_back();
     //弹出字符串的最后一个字符 , 有效减少它的长度
at函数
     reference at( size_type index );
     //at()函数返回一个引用,指向在index位置的字符. 如果index
     //不在字符串范围内, at() 将报告"out of range"错误,并抛出out_of_range异常
 
begin函数
    iterator begin();//begin()函数返回一个迭代器,指向字符串的第一个元素
 
end函数
    iterator end();//返回一个迭代器,指向字符串的末尾(最后一个字符的下一个位置)
 
c_str函数
    const char *c_str();//返回一个指向正规C字符串的指针, 内容与本字符串相同
 
capacity函数
    size_type capacity();//返回在重新申请更多的空间前字符串可以
    //容纳的字符数. 这个数字至少与 size()一样大
 
copy函数
    size_type copy( char *str, size_type num, size_type index );
    //拷贝自己的num个字符到str中(从索引index开始)。返回值是拷贝的字符数
 
data函数
    const char *data();//返回指向自己的第一个字符的指针
 
empty函数
    bool empty();//如果字符串为空则empty()返回真(true),否则返回假(false)
 
get_allocator函数
    allocator_type get_allocator();//返回本字符串的配置器
 
length函数
    size_type length();//返回字符串的长度. 这个数字应该和size()返回的数字相同
 
max_size
    size_type max_size();//返回字符串能保存的最大字符数
 
rbegin函数
    rbegin();//返回一个逆向迭代器,指向最后一个字符
 
rend函数
    rend();//返回一个逆向迭代器,指向第一个元素的前一个位置
 
reserve函数
    reserve( size_type num );//保留一定容量以容纳字符串(设置capacity值)
 
resize函数
  void resize( size_type num );//改变本字符串的大小到num, 新空间的内容不确定
 
  void resize( size_type num, char ch );//也可以指定用ch填充
 
size函数
    size();//返回字符串中字符的数量
 
substr函数
     basic_string substr( size_type index, size_type num = npos );
     //返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,
     //将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串
  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值