草庐IT

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

lynx-peng 2023-04-15 原文

一、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 读取一行,存储在字符串中

有关C++STL容器——string成员函数大全(超详细)的更多相关文章

  1. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  2. ruby-on-rails - 在 ruby​​ 中使用 gsub 函数替换单词 - 2

    我正在尝试用ruby​​中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了

  3. ruby-on-rails - Rails 模型——非持久类成员或属性? - 2

    对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs

  4. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  5. ruby - 字符串文字中的转义状态作为 `String#tr` 的参数 - 2

    对于作为String#tr参数的单引号字符串文字中反斜杠的转义状态,我觉得有些神秘。你能解释一下下面三个例子之间的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用了'd',在双引号中转义时不会改变含义("\d"="d")。'\\'.tr('\\','x')#=>"x"'\\'.tr('\\d','x')#=>"\\"'\\'.tr('\\\d','x')#=>"x" 最佳答案 在tr中转义tr的第一个参数非常类似于正则表达式中的括号字符分组。您可以在表达式的开头使用^来否定匹配(替换任何不匹配的内容)并使用例如a-f来匹配一

  6. ruby - 从 String#split 返回的零长度字符串 - 2

    在Ruby1.9.3(可能还有更早的版本,不确定)中,我试图弄清楚为什么Ruby的String#split方法会给我某些结果。我得到的结果似乎与我的预期相反。这是一个例子:"abcabc".split("b")#=>["a","ca","c"]"abcabc".split("a")#=>["","bc","bc"]"abcabc".split("c")#=>["ab","ab"]在这里,第一个示例返回的正是我所期望的。但在第二个示例中,我很困惑为什么#split返回零长度字符串作为返回数组的第一个值。这是什么原因呢?这是我所期望的:"abcabc".split("a")#=>["bc"

  7. ruby - 在 Ruby 中按名称传递函数 - 2

    如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只

  8. 7个大一C语言必学的程序 / C语言经典代码大全 - 2

    嗨~大家好,这里是可莉!今天给大家带来的是7个C语言的经典基础代码~那一起往下看下去把【程序一】打印100到200之间的素数#includeintmain(){ inti; for(i=100;i 【程序二】输出乘法口诀表#includeintmain(){inti;for(i=1;i 【程序三】判断1000年---2000年之间的闰年#includeintmain(){intyear;for(year=1000;year 【程序四】给定两个整形变量的值,将两个值的内容进行交换。这里提供两种方法来进行交换,第一种为创建临时变量来进行交换,第二种是不创建临时变量而直接进行交换。1.创建临时变量来

  9. C51单片机——实现用独立按键控制LED亮灭(调用函数篇) - 2

    说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时

  10. 在VMware16虚拟机安装Ubuntu详细教程 - 2

    在VMware16.2.4安装Ubuntu一、安装VMware1.打开VMwareWorkstationPro官网,点击即可进入。2.进入后向下滑动找到Workstation16ProforWindows,点击立即下载。3.下载完成,文件大小615MB,如下图:4.鼠标右击,以管理员身份运行。5.点击下一步6.勾选条款,点击下一步7.先勾选,再点击下一步8.去掉勾选,点击下一步9.点击下一步10.点击安装11.点击许可证12.在百度上搜索VM16许可证,复制填入,然后点击输入即可,亲测有效。13.点击完成14.重启系统,点击是15.双击VMwareWorkstationPro图标,进入虚拟机主

随机推荐