C++中string常用函数用法总结
string(s小写)是C++标准库中的类,纯C中没有,使用时需要包含头文件#include<string>,注意不是<string.h>,下面记录一下string中比较常用的用法。
string的定义及初始化
string s1 = "hello"; //初始化字符串
string s2 ("world"); //另一种初始化
string s3; //初始化字符串,空字符串
string s4(, 'a'); //s4由连续5个a组成,即s4="aaaaa";
string s5(s1,,); //从s1的2位置的字符开始,连续3个字符赋值给s5,即s5="llo";
string s6(s1, ); //从s1的2位置的字符开始,将后续的所有字符赋值给s6,即s6="ello";
string的读入
当使用C++的cin读入字符串时,程序遇到空白字符就停止读取了。比如程序输入是:
" hello world"
那么当我们使用如下代码时,s1得到的只是"hello"。
string s1;
cin>>s1;
如果我们想读取一整行输入,包括空格及空格后面的字符,我们可以使用getline。
string str;
getline(cin, str);
cout << str << endl;
重载的运算符
此处列举一下被重载的运算符,基本意思一目了然。其中注意“+”操作
s1 = s2;
s1 += s2;
s1 = s2 + s3;
s1 == s2;
s1 = "s" + s2; //正确
s1 = "s" + "s"; //错误,加号两边至少要有一个string类型的对象
s1 = "s" + s2 + "s" + "s"; //正确
“+”的两边要保证至少有一个string类型,所以5正确,6错误。由于在C/C++中,+的返回值还是原类型,所以第7行中,"s"+s2返回一个string类型,因此string+“s”也是正确的,以此类推。
遍历string(迭代器)
遍历string中的元素时,我们可以使用类似C中的数组形式访问,如s1[1],也可以使用STL特有的迭代器访问:
string::iterator it;
for (it = s1.begin(); it != s1.end(); it++){
cout << *it << endl;
} cout << *(s1.begin()); //正确,即访问s1[0]
cout << *(s1.end()); //错误,s1.end()指向了空
若想要从后向前遍历string时,可以用到rbegin()和rend()函数。
const_iterator begin()const;
iterator begin(); //返回string的起始位置
const_iterator end()const;
iterator end(); //返回string的最后一个字符后面的位置
const_iterator rbegin()const;
iterator rbegin(); //返回string的最后一个字符的位置
const_iterator rend()const;
iterator rend(); //返回string第一个字符位置的前面
插入insert()
string s1 = "hello";
s1.insert(,"ins"); //从s1的1位置开始,插入"ins"字符串,即s1="hinsello";
s1.insert(, "ins", );//从s1的1位置开始,插入"ins"字符串的前2个字符,即s1="hinello";
s1.insert(, "ins", , );//从s1的1位置开始,插入"ins"字符串的从1位置开始的2个字符,即s1="hnsello";
iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
删除erase()
iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置
iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置
string &erase(int pos = , int n = npos);//删除pos开始的n个字符,返回修改后的字符串
查找 find()
cout << s.find("aa", ) << endl; //返回的是子串位置。第二个参数是查找的起始位置,如果找不到,就返回string::npos
if (s.find("aa1", ) == string::npos)
{
cout << "找不到该子串!" << endl;
}
C++中string常用函数用法总结的更多相关文章
- C++中的string常用函数用法
标准c++中string类函数介绍 注意不是CString 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而 ...
- c++中的string常用函数用法总结!
标准c++中string类函数介绍 注意不是CString 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作 ...
- [转]c++中的string常用函数用法总结
标准c++中string类函数介绍 注意不是CString之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为 ...
- numpy函数库中一些常用函数的记录
##numpy函数库中一些常用函数的记录 最近才开始接触Python,python中为我们提供了大量的库,不太熟悉,因此在<机器学习实战>的学习中,对遇到的一些函数的用法进行记录. (1) ...
- Mysql中的常用函数:
Mysql中的常用函数: 1.字符串函数: (1).合并字符串 concat():// concat('M','y',"SQL",'5.5');== MySQL5.5//当传入的参 ...
- socket编程中客户端常用函数
1 常用函数 1.1 connect() int connect(int sockfd, const struct sockaddr *servaddr, socklen_taddrlen); 客 ...
- delphi中Application.MessageBox函数用法详解
delphi中Application.MessageBox函数用法详解 Application.MessageBox是TApplication的成员函数,声明如下:functionTApplicati ...
- C++ string 常用函数
C++ String常用函数 一,类型别名 size_type 无符号整型 iterator 迭代器类型 const_iterator 只读迭代器 reverse_iterator 逆序迭代器 con ...
- java中string.trim()函数的使用
java中string.trim()函数的的作用是去掉字符串开头和结尾的空格,防止不必要的空格导致的错误. public static void main(String arg[]){ String ...
随机推荐
- Conetos 下安装docker 和镜像加速
首先升级yum yum update 安装yum-utils,它提供yum-config-manager可以用来配置repo yum install -y yum-utils 使用以下命令设置稳定版 ...
- 模板if 的使用
from flask import Flask,render_template app = Flask(__name__) app.debug = True @app.route('/') def h ...
- Yarn-本地获取任务日志
Yarn-本地获取任务日志 yarn logs -applicationId application_1517538889175_2550 > logs.txt
- for循环语句的用法
1.for(int i : index){}用法[又称是foreach用法]: 比如: public class Test { public static void main(String[] arg ...
- Axios 的基本使用
Axios 是一个基于 promise 的HTTP 库, 可以用在浏览器和 node.js 中. 1. 从浏览器创建 XMLHttpRequests 2. 从node.js 创建 http 请求 3. ...
- LayUI 多选框动态加载、启用、禁用、赋值、取值等js实现
例如多选框如下: <div class="layui-form"> <select xm-select="city"> </sel ...
- vim编辑Dockerfile时语法高亮
参考Dockerfile构建容器---语法高亮 三个文件扔进相关的目录即可 1. /usr/share/vim/vimfiles/doc/dockerfile.txt *dockerfile.txt* ...
- Fire Net HDU - 1045 (二分图匹配)
题意: 给出一张图,图中'X'表示wall,'.'表示空地,可以放置blockhouse同一条直线上只能有一个blockhouse,除非有wall 隔开,问在给出的图中最多能放置多少个blockhou ...
- html元素标签时间格式化
<fmt:formatDate value="${user.loginTime}" pattern="yyyy-MM-dd HH:mm:ss"/>
- VMware虚拟机中CentOS/redhat设置固定IP
你的笔记本中的VMware中redhat或centOS系统,如果想在上面建站,而又如果你需要在家里和公司都能访问该站(至少希望你自己的笔记本能访问),那么就需要将虚拟机IP设置为固定IP了.以下介绍两 ...