一、string 成员函数大全

构造

string()//构造空字符串
string(const char* s);//拷贝s所指向的字符串序列
string(const char* s, size_t n);//拷贝s所指向的字符串序列的第n个到结尾的字符
string(size_t n, char c);//将字符c复制n次
string(const string& str);//拷贝构造函数
string(const string& str, size_t pos, size_t len = npos);//拷贝s中从pos位置起的len个字符,若npos>字符串长度,就拷贝到字符串结尾结束

析构

~string();//删除字符串

迭代器

/*迭代器*/
iterator begin(); //返回指向字符串第一个字符的迭代器
iterator end(); //返回指向字符串最后一个字符的下一个位置的迭代器
reverse_iterator rbegin(); //返回字符串最后一个字符的反向迭代器
reverse_iterator rend(); //返回指向字符串第一个字符之前的反向迭代器
/*常量迭代器*/
iterator cbegin(); //返回指向字符串第一个字符的迭代器
iterator cend(); //返回指向字符串最后一个字符的下一个位置的迭代器
reverse_iterator rcbegin(); //返回字符串最后一个字符的反向迭代器
reverse_iterator rcend(); //返回指向字符串第一个字符之前的反向迭代器

访问

reference at(size_type pos);//同char& operator[],返回pos位置字符的引用,字符串可读可写
char& back();//返回最后字符的引用
char& front();//返回第一个字符的引用

长度及容量

size_t size();//返回字符串字符个数
size_t length();//返回字符串字符个数
size_t max_size();//返回string对象中可存放的最大字符串的长度
size_t capacity();//返回string分配的存储容量。
void resize (size_t n);//调整源字符串的长度为n。
void resize (size_t n, char c);//调整字符串长度为n,并用字符c填充不足的部分
void reserve (size_t n = 0);//重新给源字符串分配存储最小为n的容量
void shrink_to_fit()//清理内存,使字符串的容量变得等于字符串的大小
void clear();//将字符串的内容清空,让源字符串成为一个空字符串(长度为0个字符)
bool empty();//判断字符串是否为空

修饰

1. append() 在结尾添加字符串

string & append(const string & str)//在结尾添加一个string字符串
string & append(const string & str, size_type subpos, size_type sublen)//追加str中从subpos开始的sublen个字符(子串)
string & append(const charT * s)//C语言字符串
string & append(const charT * s, size_type n)//C语言字符串(长度为n的子串)
string & append(size_type n, charT c)//n个字符c
string & append(InputIterator first, InputIterator last)//使用迭代器append

2. push_back 将字符添加到串尾

void push_back (charT c);//将字符C添加到结尾

3. assign 赋值

string &assign(const char *s);///char*类型的字符串赋给当前字符串
string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值
string &assign(const string &s);//把字符串s赋给当前字符串
string &assign(int n,char c);//用n个字符c赋值给当前字符串
string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串
string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串

4. insert 在串中间插入

string & insert(size_type pos, const string & str)//在pos位置插入字符串str
string & insert(size_type pos, const string & str,size_type subpos, size_type sublen)//从subpos开始的sublen的子串
string & insert(size_type pos, const charT * s)//C语言字符串
string & insert(size_type pos, const charT * s, size_type n)//C语言字符串(长度为n的子串)
string & insert(size_type pos,   size_type n, charT c)//n个字符c
iterator insert(const_iterator p, size_type n, charT c)//使用迭代器索引插入n和字符
iterator insert(const_iterator p, charT c)//单一字符
iterator insert(iterator p, InputIterator first, InputIterator last)//使用迭代器insert

5. erase 删除字符串中的特定字符

string & erase(size_type pos=0, size_type len=npos)//从pos处删除len长度的字符
iterator erase(const_iterator p)//删除迭代器所指的单一字符
iterator erase(const_iterator first, const_iterator last)//删除2迭代器中间的字符

6. replace 替换字符串的一部分

string & replace(size_type pos,size_type len,const string & str)//从pos位置开始,长度为len的字符替换为str
string & replace(const_iterator i1, const_iterator i2, const string & str)//替换两迭代器之间的字符
string & replace(size_type pos, size_type len, const string & str,size_type subpos, size_type sublen)//使用子串替换
string & replace(size_type pos,     size_type len,     const charT * s)//使用C语言字符串
string & replace(const_iterator i1, const_iterator i2, const charT * s)//迭代器方法
string & replace(size_type pos,     size_type len,     const charT * s, size_type n)//使用子串
string & replace(const_iterator i1, const_iterator i2, const charT * s, size_type n)//迭代器方法
string & replace(size_type pos,     size_type len,     size_type n, charT c)//用n个字符c替换
string & replace(const_iterator i1, const_iterator i2, size_type n, charT c)//迭代器方法
string & replace(const_iterator i1, const_iterator i2,
                       InputIterator first, InputIterator last)//迭代器方法

7. swap 交换2字符串

void swap (& str);//交换self和str

8. pop_back 删除最后一个字符

void pop_back();//删除串中最后一个字符

操作

1. C_str 转为C语言字符串

const charT* c_str() const;//返回C语言字符串常量,指向以空字符终止的数组

2. data 转为C语言字符数组

const charT* data() const;//返回C语言字符串常量,结尾没有'\0'

3. get_allocator 用于分配内存的内存块

allocator_type get_allocator() const;//它返回与容器关联的分配器对象的副本。yongy

4.copy 复制到字符数组

size_type copy (charT* s, size_type len, size_type pos = 0) const;//从string类型对象中至多复制n个字符到字符指针p指向的空间中。不添加'\0'

5. find 查找

size_type find(const string & str, size_type pos=0) const;//从母字符串的pos位置查找字串str.存在返回字串第一个字符的位置,不存在返回npos
size_type find(const charT * s, size_type pos=0) const;//C语言字符串作为子串
size_type find(const charT * s, size_type pos, size_type n) const;//C语言字符串的子串(长度为n)作为被查找子串
size_type find(charT c, size_type pos=0) const;//查找单个字符
//倒着找
size_type rfind(const string & str, size_type pos=npos) const;
size_type rfind(const charT * s, size_type pos=npos);
size_type rfind(const charT * s, size_type pos, size_type n);
size_type rfind(charT c, size_type pos=npos) const;
//查找字符串中与目标字符(串中任一个字符)相同的第一个字符
size_type find_first_of(const string & str, size_type pos=0) const;
size_type find_first_of(const charT * s, size_type pos=0) const;
size_type find_first_of(const charT * s, size_type pos, size_type n);
size_type find_first_of(charT c, size_type pos=0) const;
//倒着找 or 找最后一个
size_type find_last_of(const string & str, size_type pos=npos) const 
size_type find_last_of(const charT * s, size_type pos=npos) const
size_type find_last_of(const charT * s, size_type pos, size_type n) const
size_type find_last_of(charT c, size_type pos=npos) const
//查找字符串中与目标字符(串中任一个字符)不相同的第一个字符
size_type find_first_not_of(const string & str, size_type pos=0) const;
size_type find_first_not_of(const charT * s, size_type pos=0) const;
size_type find_first_not_of(const charT * s, size_type pos, size_type n) const;
size_type find_first_not_of(charT c, size_type pos=0) const;
//倒着找 or 找最后一个
size_type find_last_not_of(const string & str, size_type pos=npos) const ;
size_type find_last_not_of(const charT * s, size_type pos=npos) const;
size_type find_last_not_of(const charT * s, size_type pos, size_type n) const;
size_type find_last_not_of(charT c, size_type pos=npos) const ;

6. substr 子串

string substr (size_type pos = 0, size_type len = npos) const;//返回一个从pos开始,len长度的string类型的子串

7.compare 比较

int compare (const string& str) const ;//比较字符串大小,源串大于str返回值>0,相同=0,小于<0
int compare (size_type pos, size_type len, const string& str) const;//用自身的子串比较
int compare (size_type pos, size_type len, const string& str,
             size_type subpos, size_type sublen) const;//两字符串均为子串
int compare (const charT* s) const;//C语言字符串
int compare (size_type pos, size_type len, const charT* s) const;//C语言字符串子串
int compare (size_type pos, size_type len, const charT* s, size_type n) const;//双子串

二、符号重载

1. = 赋值

string &operator=(const string &s);//把字符串s赋给当前字符串
string &operator=(const char* s);//char*类型的字符串赋给当前字符串
string &operator=(char c);//单个字符赋给当前字符串

2. [] 访问

char& operator[] (size_t pos);//返回pos处字符的引用 越界导致未定义行为

3. += 追加

string& operator+= (const string& str);//在结尾加如str字符串,等效于append
string& operator+= (const char* s);//C语言字符串
string& operator+= (char c);//单个字符

三、非成员函数重载

函数/操作 作用
+ 合并两个字符串
==,>=,<=,!=,>,< 比较字符串大小
>>,<< 加入输入输出流,用于输入/输出
getline 读取一行,存储在字符串中