C++——STL标准模板库——容器详解——string

一、基本概念

string本质是一个类,封装了c风格字符串(以'\0'结尾的字符数组),具备自动管理内存功能,提供了多种构造函数和多种删查增改的成员方法。string的本质特点归结以下几点:

1、动态数组:string底层是vector<char>实现的,可以根据字符串长度自动增减所用内存。

2、字符序列:string是一个字符序列,可以被看做字符的连续集合。可以使用索引操作符[]访问、修改或添加字符。

3、标准库类:string是c++标准库的一部分,这也就意味着它是经过广泛测试和优化的,并且与其他标准库组件兼容。

4、提供自动内存管理:与c风格字符串不同,string自动管理内存,减少了内存泄漏和其他问题的可能性,简化了字符串的创建和销毁过程。

5、支持多种操作:和c风格字符串相比,增加了一系列成员函数和运算符功能,例如:+、+=、==、!=、>、<、>=、<=、[]、at()、find()、substr()、erase()、insert()、replace(),等等。

二、构造函数

  1. 默认构造函数,构造一个空字符串:string();
  2. 使用n个字符c构造字符串:string(size_t n,char c);
  3. 使用字符数组构造字符串:string(const char* s);
  4. 使用字符数组前n个字符:string(const char* s,size_t n);
  5. 使用另一个字符串构造:string(const string& str);
  6. 使用初始化列表构造(C++11起):string(initializer_list<char>& list);
  7. 使用两个迭代器之间的内容构造:

        template <class InputIterator> string(InputIterator first, InputIterator last);


string构造函数不止7种,且在c++标准演进和更新过程中,string会出现更多或者减少个别构造函数

三、成员函数

(一)大小和容量

  • size_t string::size()和size_t string::length();返回字符串长度
  • size_t string::max_size();返回字符串能容纳的最大字符数量
  • size_t string::resize(size_t newsize,char _ch='\000');重新设置字符串大小,newsize长于原字符串长度用_ch补全,短于原字符串长度截断
  • size_t string::capacity();返回字符串当前分配储存空间的大小
  • void string::reserve(size_t newcap);预申请字符串储存空间用于提升程序效率
  • bool string::empty();判断字符串是否为空

(二)元素的访问

  • char& string::operator[](size_t index):string封装的[]重载,返回下标index位置的元素引用
  • char& string::at(size_t index):at函数和[]运算符功能一样,返回下标index位置的元素引用
  • char& string::front():返回字符串首字符的引用
  • char& string::back():返回字符串尾字符的引用

(三)字符串比较

  • bool operator==(const string&);判断连个字符串是否相等
  • bool operator!=(const string&);判断两个字符串是否不等
  • bool operator>=(const string&);比较两个字符串大小关系
  • bool operator<=(const string&);比较两个字符串大小关系
  • bool operator>(const string&);比较两个字符串大小关系
  • bool operator<(const string&);比较两个字符串大小关系
  • int string::compare(int pos,int count,const string& str,int spos,int scount);

比较两个字符串大小关系时,底层逻辑是逐一字符比较assic码值

(四)字符串拼接

  • string string::operator+(const string&);返回拼接后的新字符串的值
  • string& string::operator+=(const string&);返回原字符串的引用
  • string& string::append(const string& str,int pos,int count);将str[pos,pos+count)中内容拼接到原字符串末尾,返回原字符串引用

(五)字符串修改 

  • string& string::clear();清空字符串内容,但没有释放内存,也就是size为0,capacity不变
  • string& string::erase(int pos);删除第pos个字符后面的内容
  • string& string::erase(int pos,int count);删除第pos个字符后面的count个字符
  • string& string::insert(int pos,const char* chr,int cnt=0);在pos后面插入字符串chr前cnt个字符
  • string& string::insert(int p,const string& str,int r,int c=MAX);在p后面插入str中r后面c个字符
  • string& string::replace(int p,int cnt,const string&);在p后面用新字符串替换cnt个字符。

(六)查找函数

  • int string::find(const string& str,int pos,int cnt);查找原字符串0-pos范围内,str前cnt个字符子串或者字符第一次出现的位置,返回下标
  • int string::rfind(const string& str,int pos,int cnt);查找原字符串0-pos范围内,str前cnt个字符子串或者字符最后一次出现的位置,返回下标
  • int string::find_first_of(const string& str,int pos,int cnt);查找str前cnt个任意字符在原字符串0-pos范围内第一次出现的位置,返回下标
  • int string::find_last_of(const string& str,int pos,int cnt);查找str前cnt个字符任意一个在原字符串0-pos范围内最后一次出现的位置,返回下标
  • int string::find_first_not_of(const string& str,int pos,int cnt);查找原字符串0-pos范围内第一个和str前cnt个任意字符不重复的字符出现的位置,返回下标
  • int string::find_last_not_of(const string& str,int pos,int cnt);查找原字符串0-pos下标范围内第一个与str前cnt个字符不重复的字符位置,返回下标

以上查找或者搜索函数有多种重载,这里将部分不太好记忆理解的列举

(七)其他成员函数

  • const char* string::c_str();将c++字符串转化为c风格字符串,返回字符数组首地址。
  • int string::copy(char *ptr,int count,int pos=0);将字符串[pos,pos+count)范围内字符赋值到ptr的[0,count)中,返回成功复制字符数量。WARNING:小心越界。
  • string& string::assign(const string& str,int pos,int count);用str[pos,pos+count)中内容赋值给原字符串
  • string string::substr(int pos=0,int count=MAX);取原字符串[pos,pos+count)范围内子串
  • string& string::swap(string& str);交换两个字符串内容

        string::begin()        string::end()        string::rbegin()        string::rend()       

        string::push_back()        string::pop_back()  

        等等和vector中成员函数使用方法一致

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值