C++基础之string类
string也是属于顺序容器,但是string类很重要且经常使用,因此在这里单独记录。
string的操作总结
string(const char *s,int n); //用c字符串s初始化,s应至少含有n个字符
string(s2,pos2); //s2从下标pos2开始的字符的拷贝,pos2>s2.size(),构造函数的行为未定义;
string(s2,pos2,len2); //s2从下标pos2开始的字符的拷贝,pos2>s2.size(),构造函数的行为未定义;不管len2是多少构造函数之多拷贝s2.size()-pos2个字符
构造函数能够接受一个string和const char*参数,而当我们用const char*创建string时,指针指向的数组必须以空字符结尾,拷贝操作遇到空字符时停止;如果还传递给构造函数一个计数值,则不必以空字符结尾。
string类的字符操作:
const char &operator[](int n)const;
const char &at(int n)const;
char &operator[](int n);
char &at(int n);
operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。
const char *data()const;//返回一个非null终止的c字符数组
const char *c_str()const;//返回一个以null终止的c字符串
size_type copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目
string的特性描述:
size_type capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素个数)
size_type max_size()const; //返回string对象中可存放的最大字符串的长度
size_type size()const; //返回当前字符串的大小
size_type length()const; //返回当前字符串的长度
bool empty()const; //当前字符串是否为空
void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分
注意:size()等函数和下面出现的find返回的是size_type,而不是int类型,常有人把他们划等号,但是他们是不一样的,size_type是string的伙伴类型,是unsigned int类型,虽然,大部分情况用int代替不会出错,但其实有隐患,所以不要把size_type 赋给int。
使用int变量的另一个问题是,有些机器上int变量的表示范围太小,甚至无法存储实际并不长的string对象。如在有16位int型的机器上,int类型变量最大只能表示32767个字符的string对象。而能容纳一个文件内容的string对象轻易就会超过这个数字。因此,为了避免溢出,保存一个string对象size的最安全的方法就是使用标准库类型。
string类的输入输出操作:
string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。
函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符'\n'分开。
string的赋值:
string &operator=(const string &s);//把字符串s赋给当前字符串
string &assign(const char *s);//用c类型字符串s赋值,C风格的字符串
string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值,C风格的字符串
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迭代器之间的部分赋给字符串
注意:下面replace()、insert()、find()等函数都包含C风格的字符串的处理方法。
string的连接:
string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾
string &append(const char *s); //把c类型字符串s连接到当前字符串结尾
string &append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前字符串结尾
string &append(const string &s); //同operator+=()
string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾
string &append(int n,char c); //在当前字符串结尾添加n个字符c
string &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾
string的比较:
bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等
运算符">","<",">=","<=","!="均被重载用于字符串的比较;
size_type compare(const string &s) const;//比较当前字符串和s的大小
size_type compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
size_type compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中
//pos2开始的n2个字符组成的字符串的大小
int compare(const char *s) const;
int compare(int pos, int n,const char *s) const;
int compare(int pos, int n,const char *s, int pos2) const;
compare函数在>时返回1,<时返回-1,==时返回0
string的子串:
string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串
pos和n都有默认值,pos默认为0,n默认为s.size()-pos;如果开始位置超过了string的大小,则substr函数抛出out_of_range的异常。
string的交换:
void swap(string &s2); //交换当前字符串与s2的值
string类的查找函数:
size_type find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
size_type find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
size_type find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
size_type find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值
size_type rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
size_type rfind(const char *s, int pos = npos) const;
size_type rfind(const char *s, int pos, int n = npos) const;
size_type rfind(const string &s,int pos = npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值
size_type find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
size_type find_first_of(const char *s, int pos = 0) const;
size_type find_first_of(const char *s, int pos, int n) const;
size_type find_first_of(const string &s,int pos = 0) const;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos
size_type find_first_not_of(char c, int pos = 0) const;
size_type find_first_not_of(const char *s, int pos = 0) const;
size_type find_first_not_of(const char *s, int pos,int n) const;
size_type find_first_not_of(const string &s,int pos = 0) const;
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos
size_type find_last_of(char c, int pos = npos) const;
size_type find_last_of(const char *s, int pos = npos) const;
size_type find_last_of(const char *s, int pos, int n = npos) const;
size_type find_last_of(const string &s,int pos = npos) const;
size_type find_last_not_of(char c, int pos = npos) const;
size_type find_last_not_of(const char *s, int pos = npos) const;
size_type find_last_not_of(const char *s, int pos, int n) const;
size_type find_last_not_of(const string &s,int pos = npos) const;
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找
搜索(以及其他string)是大小写敏感的
注意:string::npos = -1;(无符号的)
但是 string::size_type (由字符串配置器 allocator 定义) 描述的是 size,故需为无符号整数型别。因为缺省配置器以型别 size_t 作为 size_type,于是 -1 被转换为无符号整数型别,npos 也就成了该型别的最大无符号值。不过实际数值还是取决于型别 size_type 的实际定义。不幸的是这些最大值都不相同。事实上,(unsigned long)-1 和 (unsigned short)-1 不同(前提是两者型别大小不同)。
string类的替换函数:
string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s,C风格的字符串
string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符,C风格的字符串
string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符c
string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字符串s
string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字符
string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之间的部分替换为串s
string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符c
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之间的部分替换成[first,last)之间的字符串
string类的插入函数:
string &insert(int p0, const char *s);//C风格的字符串
string &insert(int p0, const char *s, int n);//C风格的字符串
string &insert(int p0,const string &s);
string &insert(int p0,const string &s, int pos, int n);
//前4个函数在p0位置插入字符串s中pos开始的前n个字符
string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c
iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符
void insert(iterator it, int n, char c);//在it处插入n个字符c
string类的删除函数
iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置
iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置
string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串
string类的迭代器处理:
string类提供了向前和向后遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类似于指针操作,迭代器不检查范围。
用string::iterator或string::const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:
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第一个字符位置的前面
rbegin和rend用于从后向前的迭代访问,通过设置迭代器string::reverse_iterator,string::const_reverse_iterator实现
string的数制转换函数
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
使用C++11引入的C++库函数将string转换为数值类型,相应的库函数申明于头文件<string>中。
名称 | 原型 | 说明 |
---|---|---|
stoi | int stoi (const string& str, size_t* idx = 0, int base = 10); int stoi (const wstring& str, size_t* idx = 0, int base = 10); |
Convert string to integer (function template ) |
stol | long stol (const string& str, size_t* idx = 0, int base = 10); long stol (const wstring& str, size_t* idx = 0, int base = 10); |
Convert string to long int (function template) |
stoul | unsigned long stoul (const string& str, size_t* idx = 0, int base = 10); unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10); |
Convert string to unsigned integer (function template) |
stoll | long long stoll (const string& str, size_t* idx = 0, int base = 10); long long stoll (const wstring& str, size_t* idx = 0, int base = 10); |
Convert string to long long (function template) |
stoull | unsigned long long stoull (const string& str, size_t* idx = 0, int base = 10); unsigned long long stoull (const wstring& str, size_t* idx = 0, int base = 10); |
Convert string to unsigned long long (function template) |
stof | float stof (const string& str, size_t* idx = 0); float stof (const wstring& str, size_t* idx = 0); |
Convert string to float (function template ) |
stod | double stod (const string& str, size_t* idx = 0); double stod (const wstring& str, size_t* idx = 0); |
Convert string to double (function template ) |
stold | long double stold (const string& str, size_t* idx = 0); long double stold (const wstring& str, size_t* idx = 0); |
Convert string to long double (function template) |
形参说明:
str:重载了string和wstring版本,表示被转换的字符串。
idx:表示一个size_t*的指针类型,默认为空值。不为空时,转换成功时获取第一个非数值字符的下标。一般情况下,因为它是直接char型指针把最后非数值字符的地址值和起始地址值相减,所以也表示成功转换的字符数量,如”10”转成功为数值10时,*idx的值为2。
base:表示转换基准,默认是10进制。
注意:如果string不能转换成为一个数值,这些函数会抛出invalid_argument异常,如果转换的数值不能用任何类型来表示,则抛出out_of_range异常。
C++基础之string类的更多相关文章
- Java基础笔记-String类
String 类(被final修饰) 字符串是一种特殊的对象,一旦字符串被初始化就不可以被改变了.(内容不变) 例如: String s = “abc”; String s1 = new Stri ...
- Java基础之String类
String类 字符串是不可变的,对其做的任何改变,会生成一个对象,不会改变有原有对象. ==和equals() String s1 = "good"; String s2 = & ...
- C++语言基础(16)-string类
使用 string 类需要包含头文件<string>,下面的例子介绍了几种定义 string 变量(对象)的方法: #include <iostream> #include & ...
- Java入门 - 语言基础 - 14.String类
原文地址:http://www.work100.net/training/java-string.html 更多教程:光束云 - 免费课程 String类 序号 文内章节 视频 1 概述 2 创建字符 ...
- Java基础教程——String类
String类 Java程序中的所有字符串字面值(如 "abc" )都是String的实例 字符串是常量(因为 String 对象是不可变的,所以可以共享) 字符串的本质是字符数组 ...
- Java 基础之 String 类
String String 被声明为 final,因此不能被继承.(Integer 等包装类也不能被继承) 在 java8 中,String 内部使用 char 数组 来存储数据 public fin ...
- Java基础笔记-String类2
StringBuffer 特点: 是字符串缓冲区. 是一个容器,其长度可变,可以操作添加多个数据类型. 最后通过toString方法变成字符串. 被final锁修饰,因此不能被继承. 存储: 方法1: ...
- JAVA_SE基础——63.String类的常用方法
获取方法int length() 获取字符串的长度char charAt(int index) 获取特定位置的字符 (角标越界)int indexOf(String str) 查找子串第一次出现的索 ...
- JAVA_SE基础——62.String类的构造方法
下面我先列出初学者目前用到的构造方法 String 的构造方法: String() 创建一个空内容 的字符串对象. String(byte[] bytes) 使用一个字节数组构建一个字 ...
随机推荐
- springboot + jedisCluster
如果使用的是redis2.x,在项目中使用客户端分片(Shard)机制. 如果使用的是redis3.x中的集群,在项目中使用jedisCluster. 1.项目结构 2.pom.xml 1 <? ...
- python 33 多进程(一)
目录 多进程 1. 进程创建的两种方式 multiprocessing 2. 进程pid (process id) 3. 验证进程之间的空间隔离 4. 进程对象join方法 5. 进程对象其他属性 6 ...
- antd模糊搜索和远程数据的结合
//用到此方法的情景 先根据input框中的姓名模糊搜索出客户名称,当选中客户名称之后,获取ID ,根据客户的ID,去搜索数据列表. 防抖方法 let timeout; let currentVal ...
- Mac OS 下包管理器 homebrew的安装
homebrew :熟悉mac os的小伙伴们一定都知道这个包管理工具,它非常方便且好用,安装它只需要打开终端并将以下代码粘贴到终端中运行即可: /usr/bin/ruby -e "$(cu ...
- 第三篇 视觉里程计(VO)的初始化过程以及openvslam中的相关实现详解
视觉里程计(Visual Odometry, VO),通过使用相机提供的连续帧图像信息(以及局部地图,先不考虑)来估计相邻帧的相机运动,将这些相对运行转换为以第一帧为参考的位姿信息,就得到了相机载体( ...
- lightoj 1049 - One Way Roads(dfs)
Time Limit: 0.5 second(s) Memory Limit: 32 MB Nowadays the one-way traffic is introduced all over th ...
- R语言基础入门
请先安装好R和RStudio 如果不干别的,控制台就是一个内置计算器 2 * 3 #=> 6 sqrt(36) #=> 6, square root log10(100) #=> 2 ...
- 超实用!K8s 开发者必须知道的 6 个开源工具
文章来源:云原生实验室,点击查看原文. 导读:Kubernetes 作为云原生时代的"操作系统",熟悉和使用它是每名用户(User)的必备技能.如果你正在 Kubernetes 上 ...
- 使用Kubectl部署应用
目录 使用Kubectl部署应用 Kubectl部署流程 部署一个简单的Demo网站 一旦运行了Kubernetes集群,就可以在其上部署容器化应用程序.因此在开始之前,我们需要先确保集群已经准 ...
- 【JavaWeb】Servlet3.0中注解驱动开发
一.概述 二.@WebServlet注解 三.共享库/运行时插件 2.1 注册Servlet 2.2 注册监听器 2.3 注册过滤器 一.概述 Servlet3.0中引入了注解开发 二.@WebSer ...