string 的一些用法——插入(insert)、删除(erase)、替换(replace)和查找(find)
来源:互联网 发布:java调用url接口 编辑:程序博客网 时间:2024/06/05 12:40
昨天刷了牛客网几道关于字符串的题目,感觉不如别人写的简洁、运行效率高。所以,就系统地学习并且整理了一些关于 string 的用法,方便编写代码。在介绍之前,需要了解一下迭代器,因为用得着(这一部分不再赘述,可参考 C++ Primer 第五版 P95)。
先看一个例子,大致了解迭代器。
#include <iostream>#include <string>using namespace std;int main(){string s;getline(cin,s);for (auto it = s.begin(); it != s.end() && !isspace(*it); ++it)*it = toupper(*it);cout << s << endl;return 0;}
该程序是将string对象中的第一个单词,变成大写,比如如果输入的是 hello world,那么输出的就是 HELLO world。begin指向容器的第一个元素或者第一个字符,end 指向容器的尾元素的下一个位置,又称尾后迭代器。获取迭代器指向的元素,也是通过解引用获得,比如上面代码中的 *it,当然了,不能试图解引用一个非法迭代器或者尾后迭代器。如果有必要,要加上一条判断语句:
if (s.begin() != s.end()){}
1、删除字符串中的某个元素(使用string的成员函数 erase)
用法如下:iterator erase( iterator pos );//删除pos指向的字符, 返回指向下一个字符的迭代器
举个例子:(严格来讲,需要判断迭代器有效,即在处理之前,应该先判断 s.begin() != s.end;)
#include <iostream>#include <string>using namespace std;int main(){string s;getline(cin,s);auto it = s.begin();s.erase(it+1);cout << s << endl;return 0;}
说明:erase () 函数的形参是迭代器,类似于指针
2、插入:在字符串某个(某段)位置中的字符(字符串)(使用string的成员函数 insert())
用法如下:1) iterator insert( iterator i,const char &c );//在迭代器i表示的位置前面插入一个字符c
2) basic_string &insert(size_type index, const basic_string &str );//在字符串的位置index插入字符串str
3) basic_string &insert(size_type index, const char *str );//在字符串的位置index插入字符串str
4) basic_string &insert(size_type index1, const basic_string &str, size_type index2, size_type num);//在字符串的位置index1处插入字符串str的子串字串从index2开始,长num
5) basic_string &insert(size_type index, const char *str, size_type num );//在字符串的位置index插入字符串str的前num个字符
6) basic_string &insert(size_type index, size_type num, char c );//在字符串的位置index插入num个字符c
7) void insert( iterator i,size_type num, const char &ch );//在迭代器i表示的位置前面插入num个字符ch
8) void insert( iterator i,iterator start, iterator end );//在迭代器i表示的位置前面插入一段字符,从start开始,以end结束
以第2个和第8个为例。#include <iostream>#include <string>using namespace std;int main(){string s;getline(cin,s);auto it = s.begin();s.insert(2,"hello");//输入为hello ,输出为 hehellollocout << s << endl;return 0;}
#include <iostream>#include <string>using namespace std;int main(){string s;getline(cin,s);auto it = s.begin();s.insert(it+2,it+3,it+6);// 在第三个字符(d)前面,插入从第4个字符(f)到第6个字符(h)cout << s << endl;return 0;}输出结果:
3、替换字符串中的某个元素(使用string的成员函数 replace())
1) basic_string &replace(size_type index, size_type num, const basic_string &str );//删除从index开始的num个字符,并在index处插入str
e.g. stringstr_1 = “abc”;
str_1.replace(2,3,”qqq”);//str= abqqq
2) string &replace(int index,int num,const char *s, int n);//删除index开始的num个字符,然后在index处插入字符串s的前n个字符
3) string &replace(int p0, intn0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s
4) string &replace(int p0, intn0,const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
5) string &replace(int p0, intn0,int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符c
6) string &replace(iteratorfirst0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字符串s
7) string &replace(iteratorfirst0, iterator last0,const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字符
8) string &replace(iteratorfirst0, iterator last0,const string &s);//把[first0,last0)之间的部分替换为串s
9) string &replace(iteratorfirst0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符c
10) string &replace(iteratorfirst0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之间的部分替换成[first,last)之间的字符串
4、查找字符串中的某个元素(使用string的成员函数 find())
1) 从前往后查找(find)—成功返回位置,失败返回string::npos
a) int find(char c, int pos = 0)const;//从pos开始查找字符c在当前字符串的位置
b) int find(const char *s, int pos= 0) const;//从pos开始查找字符串s在当前串中的位置
c) int find(const char *s, intpos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
d) int find(const string &s,int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
2) 从后往前查找(rfind)—成功返回位置,失败返回string::npos
a) int rfind(char c, int pos =npos) const;//从pos开始从后向前查找字符c在当前串中的位置
b) int rfind(const char *s, intpos = npos) const;
c) int rfind(const char *s, intpos, int n = npos) const; //从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,
d) int rfind(const string&s,int pos = npos) const;
3) 从前往后查找第一次出现的位置(find_first_of) (find_last_of) —成功返回位置,失败返回string::npos
a) int find_first_of(char c, intpos = 0) const;//从pos开始查找字符c第一次出现的位置
b) int find_first_of(const char*s, int pos = 0) const;
c) int find_first_of(const char*s, int pos, int n) const;
d) int find_first_of(const string&s,int pos = 0) const;
4) 从前往后查找第一个不在的位置(find_first_not_of) (find_last_not_of)—成功返回位置,失败返回string::npos
a) int find_first_not_of(char c,int pos = 0) const;//查找第一个不是c的字符出现的位置
b) int find_first_not_of(constchar *s, int pos = 0) const;
c) int find_first_not_of(constchar *s, int pos,int n) const;
d) int find_first_not_of(conststring &s,int pos = 0) const;//查找第一个不在s中的字符出现的位置
- string 的一些用法——插入(insert)、删除(erase)、替换(replace)和查找(find)
- string的插入与删除insert、erase
- C++string:查找、替换、插入、删除等
- Find and Replace Dialog Boxes 查找和替换对话框
- string(字符串)类常用成员----增+,append,insert 删erase 改replace 查find 遍历 截取子串 字符串比较
- MySQL的INSERT和REPLACE语句用法
- MySQL的INSERT和REPLACE语句用法
- c++ STL string类的查找、替换和删除操作
- string erase的用法
- string的erase用法
- C++ list中insert、erase和remove函数的用法
- string的查找和替换
- 字符串替换 string (stl) find + replace
- C++日记——list的erase用法(erase迭代删除运行时出错)
- C++关于迭代器删除(erase)插入(insert)失效问题
- Java String replace() 和 replaceAll()的用法
- stl的string的典型操作_初始化_遍历_连接_和字符指针转化_查找替换_删除_插入_常用算法
- 《从零开始学Swift》学习笔记(Day 14)——字符串的插入、删除和替换
- 替换空格
- 【转载】配置linux syslog日志服务器
- 【Hibernate】(4)Hibernate的多表操作、级联操作与延迟加载
- 剑指offer--二叉树的下一个节点
- Java BIO与NIO比较
- string 的一些用法——插入(insert)、删除(erase)、替换(replace)和查找(find)
- ubuntu16 安装pyinstaller 显示command not found
- 活动的四种启动模式(第一行代码总结)
- hdu 6138 Fleet of the Eternal Throne (ac自动机)
- MFC中遇到的关于error C4430: 缺少类型说明符
- 647. Palindromic Substrings
- 二叉排序树
- XML
- servlet生命周期