C++ STL之string

string介绍

string是标准库容器中最经常使用的两个之一,定义在<string>头文件中,使用时加上using namespace std;

string底层的实现

string的底层是一个如下的模板类,我们经常直接使用的string是模板通过char实例化出来的类

template<typename _CharT, typename _Traits, typename _Alloc>  class basic_string;
typedef basic_string<char>    string; 

底层的basic_string类有很多typedef定义,如下所示,可以见到我们常用的比如size_type、difference_type、iterator、const_iterator、value_type

typedef _Traits															traits_type;
typedef typename _Traits::char_type										value_type;
typedef _Char_alloc_type												allocator_type;
typedef typename _Alloc_traits::size_type								size_type;
typedef typename _Alloc_traits::difference_type							difference_type;
typedef typename _Alloc_traits::reference								reference;
typedef typename _Alloc_traits::const_reference							const_reference;
typedef typename _Alloc_traits::pointer									pointer;
typedef typename _Alloc_traits::const_pointer							const_pointer;
typedef __gnu_cxx::__normal_iterator<pointer, basic_string>  			iterator;
typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>		const_iterator;
typedef std::reverse_iterator<const_iterator>							const_reverse_iterator;
typedef std::reverse_iterator<iterator>									reverse_iterator;

///  Value returned by various member functions when they fail.
static const size_type	npos = static_cast<size_type>(-1);

string支持的操作

string支持的操作真的非常的多,个人认为记下每一个函数以及它的所有重载版本是不明智的,提供了这么多操作,私以为了解每个函数的功能,在需要这个功能的时候坚信string已经提供了当前便于使用的重载版本,再根据IDE的自动提示去使用就好,熟能生巧。

string构造函数

string有很多构造函数,构造函数接受的参数是模板实例化的类型,函数原型多如下所示

string(const basic_string<char, char_traits<_CharT>, allocator<_CharT>> &__str);

为了简便,下面将basic_string<char, char_traits<_CharT>, allocator<_CharT>>记为string,其具体构造函数如下

//拷贝构造函数
string(const string &__str);

//用从__str的下标__pos开始直到结束来构造,__pos必须合理,否则报错
string(const string &__str, unsigned long long int __pos);

//用从__str的下标__pos开始的__n个字符来构造,__pos必须合理,否则报错
string(const string &__str, unsigned long long int __pos, unsigned long long int __n);

//使用字符指针来构造,到'\0'截止
string(const char *__s);

//使用字符指针的直到第__n个索引之前的值来构造,若__n超出__s的范围,补空字符
string(const char *__s, unsigned long long int __n);

//使用__n个字符__c来构造
string(unsigned long long int __n, char __c);

//移动构造函数,接管__str的内容
string(string &&__str);

//用字符的列表来构造
string(initializer_list<char> __l);

//使用迭代器范围内的元素构造,不包括__end,范围超出会乱码
string(_InputIterator __beg, _InputIterator __end);

string的基本操作函数

  1. iterator begin()
  2. iterator end()
  3. size_type size()
  4. bool empty()
  5. void clear()
  6. void push_back(char __c)
  7. void pop_back()
  8. reference front()
  9. reference back()
  10. size_type length()
  11. reference at(size_type __n)
  12. size_type capacity()
  13. void shrink_to_fit()
  14. const char *c_str()返回一个指向正规C字符串的指针常量, 内容与本string串相同
  15. size_type max_size()
  16. iterator cbegin()
  17. iterator rbegin()
  18. void swap(string &__s)将当前string更换为__s
  19. static const size_type npos = static_cast<type_size>(-1)一个常量,表示string的结束位置

string的insert操作

string重载了9个insert操作,insert操作的基本形式都是在指定位置之后插入元素,返回插入结束后的插入头位置。

iterator insert(__const_iterator __p, char __c);
iterator insert(__const_iterator __p, size_type __n, char __c);
iterator insert(const_iterator __p, _InputIterator __beg, _InputIterator __end);
void insert(iterator __p, initializer_list<char> __l);
string &insert(size_type __pos, const char *__s);
string &insert(size_type __pos, size_type __n, char __c);
string &insert(size_type __pos, const char *__s, size_type __n);
string &insert(size_type __pos, const string &__str);
string &insert(size_type __pos1, const string &__str, size_type __pos2, size_type __n);

string的erase操作

string &erase(size_type __pos = 0, size_type __n = npos);
iterator erase(const_iterator __pos);
iterator erase(const_iterator __first, const_iterator __last);

string的assign操作

string &assign(const char *__s);
string &assign(size_type __n, char __c);
string &assign(string &&__str);
string &assign(const string &__str);
string &assign(initializer_list<char> __l);
string &assign(const char *__s, size_type __n);
string &assign(_InputIterator __first, _InputIterator __last);
string &assing(string &__str, size_type __pos, size_type __n);

string的append操作

string &append(const char *__s);
string &append(size_type __n, char __c);
string &append(initializer_list<char> __l);
string &append(const char *__s, size_type __n);
string &append(const string &__str);
string &append(_InputIterator __first, _InputIterator __last);
string &append(const string &__str, size_type __pos, size_type __n);

string的replace操作

string &replace(size_type __pos, size_type __n1, const char *__s);
string &replace(size_type __pos, size_type __n1, size_type __n2, char __c);
string &replace(const_iterator __i1, const_iterator _i2, const char *__s);
string &replace(size_type __pos, size_type __n, const string &__str);
string &replace(size_type __pos, size_type __n1, const char *__s, size_type __n2);
string &replace(const_iterator __i1, const_iterator __i2, char *__k1, char *__k2);
string &replace(const_iterator __i1, const_iterator __i2, initializer_list<char> __l);
string &replace(const_iterator __i1, const_iterator __i2, size_type __n, char __c);
string &replace(const_iterator __i1, const_iterator __i2, iterator __k1, iterator __k2);
string &replace(const_iterator __i1, const_iterator __i2, const char *__s, size_type __n);
string &replace(const_iterator __i1, const_iterator __i2, const string &__str);
string &replace(const_iterator __i1, const_iterator __i2, const char *__k1, const char *__k2);
string &replace(const_iterator __i1, const_iterator __i2, _InputIterator __k1, InputIterator __p2);
string &replace(const_iterator __i1, const_iterator __i2, const_iterator __k1, const_iterator __k2);
string &replace(size_type __pos1, size_type __n1, const string &__str, size_type __pos2, size_type n2);

string的compare操作

int compare(const char *__s);
int compare(const string &__str);
int compare(size_type __pos, size_type __n1, const char *__s);
int compare(size_type __pos, size_type __n, const string &__str);
int compare(size_type __pos, size_type __n1, const char *__s, size_type __n2);
int compare(size_type __pos1, size_type __n1, const string &__str, size_type __pos2, size_type __n2);

string的find操作

size_type find(const char *__s, unsigned long long int __pos, unsigned long long int __n);
size_type find(const string &__str, unsigned long long int __pos = 0);
size_type find(const char *__s, unsigned long long int __pos = 0);
size_type find(char _c, unsigned long long int __pos = 0);

string的rfind操作

size_type rfind(const char *__s, unsigned long long int __pos, unsigned long long int __n);
size_type rfind(const string &__str, unsigned long long int __pos = npos);
size_type rfind(const char *__s, unsigned long long int __pos = npos);
size_type rfind(char _c, unsigned long long int __pos = 0);

string的find_first_of和find_first_not_of操作

size_type find_first_of(const string &__str, unsigned long long int __pos = 0);
size_type find_first_of(const char *__s, unsigned long long int __pos, unsigned long long int __n);
size_type find_first_of(const char *__s, unsigned long long int __pos = 0);
size_type find_first_of(char __c, unsigned long long int __pos = 0);

size_type find_first_not_of(const string &__str, unsigned long long int __pos = 0);
size_type find_first_not_of(const char *__s, unsigned long long int __pos, unsigned long long int __n);
size_type find_first_not_of(const char *__s, unsigned long long int __pos = 0);
size_type find_first_not_of(char __c, unsigned long long int __pos = 0);

string的find_last_of和find_last_not_of操作

size_type find_last_of(const string &__str, unsigned long long int __pos = npos);
size_type find_last_of(const char *__s, unsigned long long int __pos, unsigned long long int __n);
size_type find_last_of(const char *__s, unsigned long long int __pos = npos);
size_type find_last_of(char __c, unsigned long long int __pos = npos);

size_type find_last_not_of(const string &__str, unsigned long long int __pos = npos);
size_type find_last_not_of(const char *__s, unsigned long long int __pos, unsigned long long int __n);
size_type find_last_not_of(const char *__s, unsigned long long int __pos = npos);
size_type find_last_not_of(char __c, unsigned long long int __pos = npos);

string其他的重要操作

void resize(unsigned long long int __n, char __c);
void resize(unsigned long long int __n);
void reserve(size_type __res_arg = 0);
string substr(unsigned long long int __pos = 0, unsigned long long int __n = npos);
const char *data();
size_type copy(char *__s, unsigned long long int __n, unsigned long long int __pos = 0);
void swap(string &__s);

string小结

string支持的操作以及重载的种类真的太多了,上述如果有问题,希望大家帮忙指出,基本上包含了所有的操作的所有形式,找到其中的规律很好的使用吧。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值