C++的 string 使用

一、介绍

用字符数组存放字符串容易发生数组越界的错误,而且往往难以察觉。因此,C++ 标准模板库设计了 string 数据类型,专门用于字符串处理。

string 类型的变量就是用来存放字符串的,也叫“string对象”。string 并不是 C++ 的基本数据类型,它是 C++ 标准模板库中的一个“类”。

在用 C++ 编程时,要优先考虑用 string 对象来处理字符串,因为其用法比字符数组更简单,而且不容易出错。

首先,为了在程序中使用string类型,必须包含头文件 <string>。如下:

#include <string>

注意这里不是string.h,string.h是C字符串头文件。string类是一个模板类,位于名字空间std中,通常为方便使用还需要增加:

using namespace std;

二、声明(初始化)字符串

声明一个字符串有很多种方式,具体如下:

string s;//调用默认构造函数,s为一个空字符串

string s(str);//等价于string s = str;调用拷贝构造函数,s是str的备份

string s(str,strindex);//将字符串str内始于strindex位置的部分当作s的初始值
    eg.string str = "123456789";
    string  s(str,3);//s的初值为str由位置3开始的字符串,即456789

string s(str,stridx,strlen); // 将字符串str由stridx位置起始且长度为strlen的部分最为s的初值,如果strlen大于最大长度,则只截取字符串最大长度
    eg.string s(str,3,10);//s=456789,由位置3开始,截取长度为10的部分,由于str剩余部分长度小于10,则截取str剩余最大长度

string s(cstr);//将C风格字符串作为s的初值
    eg.string s("hello");//s的初值为hello

string s(cstr,length);//将C风格字符串的length长度部分作为s的初值
    eg.string s("hello",2);//s="he"

string s(num,c);//生成一个字符串,包含num个c字符
    eg.string s(10,'c');//s的初值为“cccccccccc”

to_string(num); // 将一个整数型转变为字符串型

三、字符串操作函数

C++字符串的操作函数很多,这里把常用的罗列出来

=、assign()//用于赋予新值,assign函数用于将一个字符串的部分内容赋值给另一个string对象
eg.string s1 = "hello";
   string s2;
   s2.assign(s1,0,3);//s2的值为“hel”

swap() //交换两个字符串的内容
eg.string s1 = "hello";
   string s2 = "world";
   swap(s1,s2);//swap函数将s1和s2的内容交换,现在s1="world",s2="hello"

+=、append()、push_back()//在字符串尾部追加内容,"+="可追加string对象,字符以及C风格字符串,append函数则可以追加string对象和C风格字符串,push_back函数则只能追加字符
eg.string s1 = "hello";
   string s2 = " world";
   s1 += s2;//正确,s1的值为”hello world“
   s1 +="world";// 正确,s1的值为"hello world"
   s1 +='c'; //正确,s1的值为"helloc"

   s1.append(s2);//正确,s1的值为"hello world"
   s1.append(" world");//正确,s1的值为"hello world"
   s1.append('c');//错误,append函数不支持追加字符

   s1.push_back(s2);//错误
   s1.push_back("world");//错误
   s1.push_back('c');//正确


insert()//用于插入字符串
eg.string s1 = "hello";
   s1.insert(0,"world ");//s1的值为world hello

erase()//用于删除字符的
eg.string str("This is an example phrase.");
   string::iterator it;//迭代器

   str.erase(10,8);//str的值为"This is an phrase.",删除了从位置10开始的8个字符

   it = str.begin()+9;//迭代器位置为9
   str.erase(it);//删除了从it迭代器位置处的一个字符,str="This is a phrase."

   str.erase(str.begin()+5,str.end()-7);//删除两个参数之间的所有字符,str="This phrase."


clear()函数和~string()//都是用来删除全部字符的
eg.str.clear();//删除str的全部字符,此时str为一个空串
   str.~string();//销毁所有字符,释放内存

replace()函数,用于替换字符
eg.1.string line = "this@ is@ a test string!";
     line = line.replace(line.find("@"),1,"");//将line中从find的@位置开始替换一个长度的字符为"" 结果为this is@ a test string!

==、!=、<、<=、>、>=、compare()  //比较字符串,根据字典序比较
eg.string s1 = "haha";
    string s2 = "haha";
    if(s1.compare(s2) == 0){
         cout << "相等" << endl;      
}

size()函数和length()函数,返回字符串的字符数
eg.string str = "haha";
   str.size() 等于 str.length(),值均为4

empty()//判断字符串是否为空

下标法str[index]或者str.at(index)获取字符串内指定位置的字符

data()函数,将内容以字符数组的形式返回

四、几个重要的算法

使用时,需要包含头文件:#include<algorithm>

(1) 使用sort排序

string s;
sort(s.begin(),s.end());  // 这种情况是升序排序

(2) 使用reverse进行元素倒置

string s;
reverse(s.begin(),s.end());  

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值