序书接上回我们已经讲到了string类对象的访问及遍历操作让我们继续来学习string类的其他知识点C官网传送门点我目录序C官网传送门一、string类对象的修改操作1. 增加数据的相关接口1.1 operator1.1.1 函数原型1.1.2 函数作用1.1.3 相关代码1.2 append1.2.1 函数原型1.2.2 函数作用1.3 insert1.3.1 函数原型1.3.2 函数作用1.3.3 相关代码1.4 push_back1.4.1函数原型1.4.2 函数作用2.修改数据的相关接口2.1 assign2.1.1函数原型2.1.2函数作用2.1.3 相关代码2.2 replace2.2.1 函数原型2.2.2 函数作用3.删除数据相关接口3.1 pop_back3.1.1 函数原型3.1.2 函数作用3.2 erase3.2.1 函数原型3.2.2 函数作用3.2.3 相关代码二、string类对象的查找操作范例find1. 函数原型2. 函数具体作用3.相关代码三、string的其他接口1. substr1.1函数原型1.2 函数作用2. shrink_to_fit2.1 函数原型2.2 函数作用3.shrink_to_fit3.1函数原型3.2 函数作用3. string转为各种类型4.各种类型转为string5.各种类型转为w_string一、string类对象的修改操作1. 增加数据的相关接口1.1 operator1.1.1 函数原型1.1.2 函数作用在字符串后面加上字符串相关接口作用string operator (const string str);在字符串后添加一个字符串string operator (const char* s);在字符串后添加C风格的字符串string operator (char c);在字符串后添加一个字符1.1.3 相关代码string s1(abc); string s2(def); s1 ghi; cout s1 endl; s2 x; cout s2 endl; s1 s2; cout s1 endl;结果abcghi defx abcghidefx1.2 append1.2.1 函数原型1.2.2 函数作用这个函数实用性很低日常很少用到相关函数作用string append (const string str);复制一份string对象追加到末尾string append (const string str, size_t subpos, size_t sublen);将从subpos开始长度为sublen的字符串追加到末尾string append (const char* s);将以’\0’结尾的C风格字符串追加到末尾string append (const char* s, size_t n);将s的前n个字符追加到末尾string append (size_t n, char c);将n个c追加到末尾template class InputIterator string append (InputIterator first, InputIterator last);将迭代器范围为[first,last)的字符串追加到末尾1.3 insert1.3.1 函数原型1.3.2 函数作用在指定位置插入字符串相关函数作用string insert (size_t pos, const string str);在pos位置之前插入string对象的拷贝string insert (size_t pos, const string str, size_t subpos, size_t sublen);将string对象从subpos位置开始长度为sublen的部分插入到pos位置之前string insert (size_t pos, const char* s);将C风格的字符串s插入到pos位置之前string insert (size_t pos, const char* s, size_t n);将C风格字符串s的前n个字符插入到pos位置之前string insert (size_t pos, size_t n, char c);将n个字符c插入到pos位置之前void insert (iterator p, size_t n, char c);将n个字符c插入到迭代器p位置之前iterator insert (iterator p, char c);将c插入到迭代器p的位置之前并返回指向新字符的迭代器为了防止迭代器失效template class InputIterator void insert (iterator p, InputIterator first, InputIterator last);将string对象[first,last)位置的字符串插入到迭代器p的位置之前1.3.3 相关代码cout 非迭代器版本插入 endl; string s1(abc); string s2(defgh); cout s1 s2 endl; s2.insert(0, s1); cout s2 endl; s2.insert(s2.size(),ijk); cout s2 endl; cout 迭代器版本插入 endl; auto it1 s1.begin(); s1.insert(it1 1, 10, x); cout s1 endl;运行结果非迭代器版本插入 abc defgh abcdefgh abcdefghijk 迭代器版本插入 axxxxxxxxxxbc1.4 push_back1.4.1函数原型1.4.2 函数作用在末尾加上一个字符此函数有上位替代operator用途不大2.修改数据的相关接口2.1 assign2.1.1函数原型2.1.2函数作用对整个字符串进行修改相关函数作用string assign (const string str);用str替换当前字符串string assign (const string str, size_t subpos, size_t sublen);用str从subpos开始长度为sublen的字符串替换当前字符串string assign (const char* s);用C风格的字符串s替换当前字符串string assign (const char* s, size_t n);用s前n个字符组成的字符串替换当前字符串string assign (size_t n, char c);用n个c替换当前字符串template class InputIterator string assign (InputIterator first, InputIterator last);用迭代器范围[first,last)的字符串替换当前字符串2.1.3 相关代码string s1(abcdefg); string s2(qwerty); cout s1: s1 endl s2: s2 endl; s1.assign(s2); cout s1 endl; s1.assign(plmokn); cout s1 endl; s1.assign(zxcvbn, 4); cout s1 endl;运行结果s1: abcdefg s2: qwerty qwerty plmokn zxcv2.2 replace2.2.1 函数原型2.2.2 函数作用与assign不同replace是对字符串的部分区域进行修改作用可参考assign具体作用见函数原型3.删除数据相关接口3.1 pop_back3.1.1 函数原型3.1.2 函数作用删除字符串最后一个元素3.2 erase3.2.1 函数原型3.2.2 函数作用删除指定区域的字符相关函数作用string erase (size_t pos 0, size_t len npos);从pos位置开始删除len个数据若超出范围则pos以后全部删除iterator erase (iterator p);删除迭代器p所指字符iterator erase (iterator first, iterator last);删除迭代器范围[first,last)的字符3.2.3 相关代码string s1(abcdefg); cout s1 endl; s1.erase(0, 2); cout s1 endl; auto it s1.begin(); s1.erase(it,it2); cout s1 endl;运行结果abcdefg cdefg efg二、string类对象的查找操作string类内有几种常用的与查找相关的函数分别是find、rfind、find_first_of、find_first_not_of、find_last_of、find_last_not_of相关函数作用find查找第一次出现字符串的位置找到了返回索引未找到返回nposrfind查找最后一次出现字符串的位置相当于find反着找find_first_of查找第一次出现参数中任意一个字符的位置find_first_not_of查找第一次出现一个字符这个字符不是参数中任意一个字符find_last_of查找最后一次出现参数中任意一个字符的位置相当于find_first_of反着找find_last_not_of查找最后一次出现一个字符这个字符不是参数中任意一个字符相当于find_first_not_of反着找范例find1. 函数原型2. 函数具体作用相关函数作用size_t find (const string str, size_t pos 0) const;从pos开始往后找str找到返回索引没找到返回npossize_t find (const char* s, size_t pos 0) const;从pos开始往后找C风格的s找到返回索引没找到返回npossize_t find (const char* s, size_t pos, size_t n) const;从pos开始找由s前n个字符组成的子串size_t find (char c, size_t pos 0) const;从pos开始查找字符c3.相关代码string s1(0123456789); string s2(123456); size_t index s1.find(s2); cout index endl; index s1.find(56789); cout index endl; index s1.find(x); cout index endl;运行结果1 5 18446744073709551615其余函数功能与find大体相似因此不过多赘述三、string的其他接口1. substr1.1函数原型1.2 函数作用返回对象从pos位置开始长度为len的子串2. shrink_to_fit2.1 函数原型2.2 函数作用请求字符串的capacity缩减到与size相适配这仅仅是个请求并非强制执行且该过程可以被自由优化该函数不会影响字符串的长度和内容3.compare3.1函数原型3.2 函数作用比较当前字符串和str若有pos和len或subpos和sublen那么pos和len分别指当前字符串的起始位置和长度subpos和sublen分别指str的起始位置和长度。如果当前字符串大于str则返回正值若小于则返回负值4.getline4.1 函数原型4.2 函数作用从输入流中读取一行数据给string与不同它碰到空格不会停止直到下一行它才会停止读取5. string转为各种类型string提供了string转为各种类型的函数具体见下表相关函数作用stodstring-doublestofstring-floatstoistring-intstolstring-longstoldstring-long doublestollstring-long longstoulstring-unsigned longstoullstring-unsigned long long6.各种类型转为string将其他类型转为string要用到to_string函数该函数原型及介绍如下7.各种类型转为w_string这是各种类型转为宽字符形式用to_wstring本章完
【C++】string类【二】
序书接上回我们已经讲到了string类对象的访问及遍历操作让我们继续来学习string类的其他知识点C官网传送门点我目录序C官网传送门一、string类对象的修改操作1. 增加数据的相关接口1.1 operator1.1.1 函数原型1.1.2 函数作用1.1.3 相关代码1.2 append1.2.1 函数原型1.2.2 函数作用1.3 insert1.3.1 函数原型1.3.2 函数作用1.3.3 相关代码1.4 push_back1.4.1函数原型1.4.2 函数作用2.修改数据的相关接口2.1 assign2.1.1函数原型2.1.2函数作用2.1.3 相关代码2.2 replace2.2.1 函数原型2.2.2 函数作用3.删除数据相关接口3.1 pop_back3.1.1 函数原型3.1.2 函数作用3.2 erase3.2.1 函数原型3.2.2 函数作用3.2.3 相关代码二、string类对象的查找操作范例find1. 函数原型2. 函数具体作用3.相关代码三、string的其他接口1. substr1.1函数原型1.2 函数作用2. shrink_to_fit2.1 函数原型2.2 函数作用3.shrink_to_fit3.1函数原型3.2 函数作用3. string转为各种类型4.各种类型转为string5.各种类型转为w_string一、string类对象的修改操作1. 增加数据的相关接口1.1 operator1.1.1 函数原型1.1.2 函数作用在字符串后面加上字符串相关接口作用string operator (const string str);在字符串后添加一个字符串string operator (const char* s);在字符串后添加C风格的字符串string operator (char c);在字符串后添加一个字符1.1.3 相关代码string s1(abc); string s2(def); s1 ghi; cout s1 endl; s2 x; cout s2 endl; s1 s2; cout s1 endl;结果abcghi defx abcghidefx1.2 append1.2.1 函数原型1.2.2 函数作用这个函数实用性很低日常很少用到相关函数作用string append (const string str);复制一份string对象追加到末尾string append (const string str, size_t subpos, size_t sublen);将从subpos开始长度为sublen的字符串追加到末尾string append (const char* s);将以’\0’结尾的C风格字符串追加到末尾string append (const char* s, size_t n);将s的前n个字符追加到末尾string append (size_t n, char c);将n个c追加到末尾template class InputIterator string append (InputIterator first, InputIterator last);将迭代器范围为[first,last)的字符串追加到末尾1.3 insert1.3.1 函数原型1.3.2 函数作用在指定位置插入字符串相关函数作用string insert (size_t pos, const string str);在pos位置之前插入string对象的拷贝string insert (size_t pos, const string str, size_t subpos, size_t sublen);将string对象从subpos位置开始长度为sublen的部分插入到pos位置之前string insert (size_t pos, const char* s);将C风格的字符串s插入到pos位置之前string insert (size_t pos, const char* s, size_t n);将C风格字符串s的前n个字符插入到pos位置之前string insert (size_t pos, size_t n, char c);将n个字符c插入到pos位置之前void insert (iterator p, size_t n, char c);将n个字符c插入到迭代器p位置之前iterator insert (iterator p, char c);将c插入到迭代器p的位置之前并返回指向新字符的迭代器为了防止迭代器失效template class InputIterator void insert (iterator p, InputIterator first, InputIterator last);将string对象[first,last)位置的字符串插入到迭代器p的位置之前1.3.3 相关代码cout 非迭代器版本插入 endl; string s1(abc); string s2(defgh); cout s1 s2 endl; s2.insert(0, s1); cout s2 endl; s2.insert(s2.size(),ijk); cout s2 endl; cout 迭代器版本插入 endl; auto it1 s1.begin(); s1.insert(it1 1, 10, x); cout s1 endl;运行结果非迭代器版本插入 abc defgh abcdefgh abcdefghijk 迭代器版本插入 axxxxxxxxxxbc1.4 push_back1.4.1函数原型1.4.2 函数作用在末尾加上一个字符此函数有上位替代operator用途不大2.修改数据的相关接口2.1 assign2.1.1函数原型2.1.2函数作用对整个字符串进行修改相关函数作用string assign (const string str);用str替换当前字符串string assign (const string str, size_t subpos, size_t sublen);用str从subpos开始长度为sublen的字符串替换当前字符串string assign (const char* s);用C风格的字符串s替换当前字符串string assign (const char* s, size_t n);用s前n个字符组成的字符串替换当前字符串string assign (size_t n, char c);用n个c替换当前字符串template class InputIterator string assign (InputIterator first, InputIterator last);用迭代器范围[first,last)的字符串替换当前字符串2.1.3 相关代码string s1(abcdefg); string s2(qwerty); cout s1: s1 endl s2: s2 endl; s1.assign(s2); cout s1 endl; s1.assign(plmokn); cout s1 endl; s1.assign(zxcvbn, 4); cout s1 endl;运行结果s1: abcdefg s2: qwerty qwerty plmokn zxcv2.2 replace2.2.1 函数原型2.2.2 函数作用与assign不同replace是对字符串的部分区域进行修改作用可参考assign具体作用见函数原型3.删除数据相关接口3.1 pop_back3.1.1 函数原型3.1.2 函数作用删除字符串最后一个元素3.2 erase3.2.1 函数原型3.2.2 函数作用删除指定区域的字符相关函数作用string erase (size_t pos 0, size_t len npos);从pos位置开始删除len个数据若超出范围则pos以后全部删除iterator erase (iterator p);删除迭代器p所指字符iterator erase (iterator first, iterator last);删除迭代器范围[first,last)的字符3.2.3 相关代码string s1(abcdefg); cout s1 endl; s1.erase(0, 2); cout s1 endl; auto it s1.begin(); s1.erase(it,it2); cout s1 endl;运行结果abcdefg cdefg efg二、string类对象的查找操作string类内有几种常用的与查找相关的函数分别是find、rfind、find_first_of、find_first_not_of、find_last_of、find_last_not_of相关函数作用find查找第一次出现字符串的位置找到了返回索引未找到返回nposrfind查找最后一次出现字符串的位置相当于find反着找find_first_of查找第一次出现参数中任意一个字符的位置find_first_not_of查找第一次出现一个字符这个字符不是参数中任意一个字符find_last_of查找最后一次出现参数中任意一个字符的位置相当于find_first_of反着找find_last_not_of查找最后一次出现一个字符这个字符不是参数中任意一个字符相当于find_first_not_of反着找范例find1. 函数原型2. 函数具体作用相关函数作用size_t find (const string str, size_t pos 0) const;从pos开始往后找str找到返回索引没找到返回npossize_t find (const char* s, size_t pos 0) const;从pos开始往后找C风格的s找到返回索引没找到返回npossize_t find (const char* s, size_t pos, size_t n) const;从pos开始找由s前n个字符组成的子串size_t find (char c, size_t pos 0) const;从pos开始查找字符c3.相关代码string s1(0123456789); string s2(123456); size_t index s1.find(s2); cout index endl; index s1.find(56789); cout index endl; index s1.find(x); cout index endl;运行结果1 5 18446744073709551615其余函数功能与find大体相似因此不过多赘述三、string的其他接口1. substr1.1函数原型1.2 函数作用返回对象从pos位置开始长度为len的子串2. shrink_to_fit2.1 函数原型2.2 函数作用请求字符串的capacity缩减到与size相适配这仅仅是个请求并非强制执行且该过程可以被自由优化该函数不会影响字符串的长度和内容3.compare3.1函数原型3.2 函数作用比较当前字符串和str若有pos和len或subpos和sublen那么pos和len分别指当前字符串的起始位置和长度subpos和sublen分别指str的起始位置和长度。如果当前字符串大于str则返回正值若小于则返回负值4.getline4.1 函数原型4.2 函数作用从输入流中读取一行数据给string与不同它碰到空格不会停止直到下一行它才会停止读取5. string转为各种类型string提供了string转为各种类型的函数具体见下表相关函数作用stodstring-doublestofstring-floatstoistring-intstolstring-longstoldstring-long doublestollstring-long longstoulstring-unsigned longstoullstring-unsigned long long6.各种类型转为string将其他类型转为string要用到to_string函数该函数原型及介绍如下7.各种类型转为w_string这是各种类型转为宽字符形式用to_wstring本章完