C++STL容器——string成员函数大全(超详细)

打印 上一主题 下一主题

主题 597|帖子 597|积分 1791

一、string 成员函数大全

构造
  1. string()//构造空字符串
  2. string(const char* s);//拷贝s所指向的字符串序列
  3. string(const char* s, size_t n);//拷贝s所指向的字符串序列的第n个到结尾的字符
  4. string(size_t n, char c);//将字符c复制n次
  5. string(const string& str);//拷贝构造函数
  6. string(const string& str, size_t pos, size_t len = npos);//拷贝s中从pos位置起的len个字符,若npos>字符串长度,就拷贝到字符串结尾结束
复制代码
析构
  1. ~string();//删除字符串
复制代码
迭代器
  1. /*迭代器*/
  2. iterator begin(); //返回指向字符串第一个字符的迭代器
  3. iterator end(); //返回指向字符串最后一个字符的下一个位置的迭代器
  4. reverse_iterator rbegin(); //返回字符串最后一个字符的反向迭代器
  5. reverse_iterator rend(); //返回指向字符串第一个字符之前的反向迭代器
  6. /*常量迭代器*/
  7. iterator cbegin(); //返回指向字符串第一个字符的迭代器
  8. iterator cend(); //返回指向字符串最后一个字符的下一个位置的迭代器
  9. reverse_iterator rcbegin(); //返回字符串最后一个字符的反向迭代器
  10. reverse_iterator rcend(); //返回指向字符串第一个字符之前的反向迭代器
复制代码
访问
  1. reference at(size_type pos);//同char& operator[],返回pos位置字符的引用,字符串可读可写
  2. char& back();//返回最后字符的引用
  3. char& front();//返回第一个字符的引用
复制代码
长度及容量
  1. size_t size();//返回字符串字符个数
  2. size_t length();//返回字符串字符个数
  3. size_t max_size();//返回string对象中可存放的最大字符串的长度
  4. size_t capacity();//返回string分配的存储容量。
  5. void resize (size_t n);//调整源字符串的长度为n。
  6. void resize (size_t n, char c);//调整字符串长度为n,并用字符c填充不足的部分
  7. void reserve (size_t n = 0);//重新给源字符串分配存储最小为n的容量
  8. void shrink_to_fit()//清理内存,使字符串的容量变得等于字符串的大小
  9. void clear();//将字符串的内容清空,让源字符串成为一个空字符串(长度为0个字符)
  10. bool empty();//判断字符串是否为空
复制代码
修饰

1. append() 在结尾添加字符串
  1. string & append(const string & str)//在结尾添加一个string字符串
  2. string & append(const string & str, size_type subpos, size_type sublen)//追加str中从subpos开始的sublen个字符(子串)
  3. string & append(const charT * s)//C语言字符串
  4. string & append(const charT * s, size_type n)//C语言字符串(长度为n的子串)
  5. string & append(size_type n, charT c)//n个字符c
  6. string & append(InputIterator first, InputIterator last)//使用迭代器append
复制代码
2. push_back 将字符添加到串尾
  1. void push_back (charT c);//将字符C添加到结尾
复制代码
3. assign 赋值
  1. string &assign(const char *s);///char*类型的字符串赋给当前字符串
  2. string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值
  3. string &assign(const string &s);//把字符串s赋给当前字符串
  4. string &assign(int n,char c);//用n个字符c赋值给当前字符串
  5. string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串
  6. string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串
复制代码
4. insert 在串中间插入
  1. string & insert(size_type pos, const string & str)//在pos位置插入字符串str
  2. string & insert(size_type pos, const string & str,size_type subpos, size_type sublen)//从subpos开始的sublen的子串
  3. string & insert(size_type pos, const charT * s)//C语言字符串
  4. string & insert(size_type pos, const charT * s, size_type n)//C语言字符串(长度为n的子串)
  5. string & insert(size_type pos,   size_type n, charT c)//n个字符c
  6. iterator insert(const_iterator p, size_type n, charT c)//使用迭代器索引插入n和字符
  7. iterator insert(const_iterator p, charT c)//单一字符
  8. iterator insert(iterator p, InputIterator first, InputIterator last)//使用迭代器insert
复制代码
5. erase 删除字符串中的特定字符
  1. string & erase(size_type pos=0, size_type len=npos)//从pos处删除len长度的字符
  2. iterator erase(const_iterator p)//删除迭代器所指的单一字符
  3. iterator erase(const_iterator first, const_iterator last)//删除2迭代器中间的字符
复制代码
6. replace 替换字符串的一部分
  1. string & replace(size_type pos,size_type len,const string & str)//从pos位置开始,长度为len的字符替换为str
  2. string & replace(const_iterator i1, const_iterator i2, const string & str)//替换两迭代器之间的字符
  3. string & replace(size_type pos, size_type len, const string & str,size_type subpos, size_type sublen)//使用子串替换
  4. string & replace(size_type pos,     size_type len,     const charT * s)//使用C语言字符串
  5. string & replace(const_iterator i1, const_iterator i2, const charT * s)//迭代器方法
  6. string & replace(size_type pos,     size_type len,     const charT * s, size_type n)//使用子串
  7. string & replace(const_iterator i1, const_iterator i2, const charT * s, size_type n)//迭代器方法
  8. string & replace(size_type pos,     size_type len,     size_type n, charT c)//用n个字符c替换
  9. string & replace(const_iterator i1, const_iterator i2, size_type n, charT c)//迭代器方法
  10. string & replace(const_iterator i1, const_iterator i2,
  11.                        InputIterator first, InputIterator last)//迭代器方法
复制代码
7. swap 交换2字符串
  1. void swap (& str);//交换self和str
复制代码
8. pop_back 删除最后一个字符
  1. void pop_back();//删除串中最后一个字符
复制代码
操作

1. C_str 转为C语言字符串
  1. const charT* c_str() const;//返回C语言字符串常量,指向以空字符终止的数组
复制代码
2. data 转为C语言字符数组
  1. const charT* data() const;//返回C语言字符串常量,结尾没有'\0'
复制代码
3. get_allocator 用于分配内存的内存块
  1. allocator_type get_allocator() const;//它返回与容器关联的分配器对象的副本。yongy
复制代码
4.copy 复制到字符数组
  1. size_type copy (charT* s, size_type len, size_type pos = 0) const;//从string类型对象中至多复制n个字符到字符指针p指向的空间中。不添加'\0'
复制代码
5. find 查找
  1. size_type find(const string & str, size_type pos=0) const;//从母字符串的pos位置查找字串str.存在返回字串第一个字符的位置,不存在返回npos
  2. size_type find(const charT * s, size_type pos=0) const;//C语言字符串作为子串
  3. size_type find(const charT * s, size_type pos, size_type n) const;//C语言字符串的子串(长度为n)作为被查找子串
  4. size_type find(charT c, size_type pos=0) const;//查找单个字符
  5. //倒着找
  6. size_type rfind(const string & str, size_type pos=npos) const;
  7. size_type rfind(const charT * s, size_type pos=npos);
  8. size_type rfind(const charT * s, size_type pos, size_type n);
  9. size_type rfind(charT c, size_type pos=npos) const;
  10. //查找字符串中与目标字符(串中任一个字符)相同的第一个字符
  11. size_type find_first_of(const string & str, size_type pos=0) const;
  12. size_type find_first_of(const charT * s, size_type pos=0) const;
  13. size_type find_first_of(const charT * s, size_type pos, size_type n);
  14. size_type find_first_of(charT c, size_type pos=0) const;
  15. //倒着找 or 找最后一个
  16. size_type find_last_of(const string & str, size_type pos=npos) const
  17. size_type find_last_of(const charT * s, size_type pos=npos) const
  18. size_type find_last_of(const charT * s, size_type pos, size_type n) const
  19. size_type find_last_of(charT c, size_type pos=npos) const
  20. //查找字符串中与目标字符(串中任一个字符)不相同的第一个字符
  21. size_type find_first_not_of(const string & str, size_type pos=0) const;
  22. size_type find_first_not_of(const charT * s, size_type pos=0) const;
  23. size_type find_first_not_of(const charT * s, size_type pos, size_type n) const;
  24. size_type find_first_not_of(charT c, size_type pos=0) const;
  25. //倒着找 or 找最后一个
  26. size_type find_last_not_of(const string & str, size_type pos=npos) const ;
  27. size_type find_last_not_of(const charT * s, size_type pos=npos) const;
  28. size_type find_last_not_of(const charT * s, size_type pos, size_type n) const;
  29. size_type find_last_not_of(charT c, size_type pos=npos) const ;
复制代码
6. substr 子串
  1. string substr (size_type pos = 0, size_type len = npos) const;//返回一个从pos开始,len长度的string类型的子串
复制代码
7.compare 比较

[code]int compare (const string& str) const ;//比较字符串大小,源串大于str返回值>0,相同=0,小于=,,</tdtd比较字符串大小/td/trtrtd>>,
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

缠丝猫

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表