string类的常用函数

string类的构造函数:
string(const char *s);    //用c字符串s初始化
string(int n,char c);     //用n个字符c初始化
此外,string类还支持默认构造函数和复制构造函数,如string s1;string s2="hello";都是正确的写法。当构造的string太长而无法表达时会抛出length_error异常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字符串
int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目string的特性描述:
int capacity()const;    //返回当前容量(即string中不必增加内存即可存放的元素个数)
int max_size()const;    //返回string对象中可存放的最大字符串的长度
int size()const;        //返回当前字符串的大小
int length()const;       //返回当前字符串的长度
bool empty()const;        //当前字符串是否为空
void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分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赋值
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迭代器之间的部分赋给字符串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;//比较两个字符串是否相等
运算符">","<",">=","<=","!="均被重载用于字符串的比较;
int compare(const string &s) const;//比较当前字符串和s的大小
int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int 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个字符组成的字符串string的交换:
void swap(string &s2);    //交换当前字符串与s2的值string类的查找函数:int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::nposint find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const;
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::nposint find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const;int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int 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 &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
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);
string &insert(int p0, const char *s, int n);
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个字符cstring类的删除函数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实现字符串流处理:通过定义ostringstream和istringstream变量实现,<sstream>头文件中
例如:string input("hello,this is a test");istringstream is(input);string s1,s2,s3,s4;is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"ostringstream os;os<<s1<<s2<<s3<<s4;cout<<os.str();

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/461456.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Memcached 内存管理(一)

2019独角兽企业重金招聘Python工程师标准>>> Memcached是一个高效的分布式内存cache&#xff0c;了解memcached的内存管理机制&#xff0c;便于我们理解memcached&#xff0c;让我们可以针对我们数据特点进行调优&#xff0c;让其更好的为我所用。这里简单谈一下我对…

相同字符串的string对象不等

今天遇到一个问题&#xff0c;用复制构造函数构造出来的String和同一个字符串的string对象居然不相等&#xff0c;即A和B是相同字符串的string,调用Cstring(A)之后&#xff0c;C和B不相等。 后来改成C string(A.c_str())后&#xff0c;C和B才相等转载于:https://www.cnblogs.c…

jitter 如何优化网络_网络推广如何做好网站SEO优化

网络推广做好网站整站SEO优化的方式有很多&#xff0c;如何才能做好SEO优化&#xff1f;网络推广如何做好网站SEO优化一、定位网站关键词SEO给一个网站刚开始做优化的时候&#xff0c;不是立马就设置关键词&#xff0c;而是先分析该网站主要是做什么产品/服务。知道网站的目的是…

PHP+Mysql查询上一篇和下一篇文章实例

PHPMysql查询上一篇和下一篇文章实例 简单的PHPMysql查询上一篇和下一篇文章实例&#xff0c;并输出上一篇和下一篇文章的标题和链接&#xff0c;适合新手学习获取当前浏览文章id&#xff1a; 1 $id isset($_GET[id]) > 0 ? intval($_GET[id]) : ""; 下一篇文章…

openssh-在win7上的搭建

2019独角兽企业重金招聘Python工程师标准>>> 参考 http://www.cnblogs.com/ericsun/archive/2012/06/10/2544413.html 1.下载OpenSSH&#xff1a;http://sourceforge.net/projects/sshwindows/files/OpenSSH%20for%20Windows%20-%20Release/3.8p1-1%2020040709%20B…

LeetCode8——String to Integer (atoi)(自己编写atoi函数)

题目&#xff1a; 参考解法&#xff1a; I think we only need to handle four cases: discards all leading whitespaces sign of the number overflow invalid input int myAtoi(char* str) {int sign 1, base 0, i 0;while (str[i] ) { i; }//去掉空格if (str[i…

5类6类7类网线对比_孩子们长高的黄金时期是从3月到5月,这阶段多吃6类食物长得快...

原标题&#xff1a;孩子们长高的黄金时期是从3月到5月&#xff0c;这阶段多吃6类食物长得快每个家长都希望孩子长大。当他们看到自己的孩子比同龄的孩子矮时&#xff0c;他们会非常担心。他们特别担心孩子的成长。事实上&#xff0c;儿童的生长发育有明显的季节性&#xff0c;一…

javascript之Partial Application

这一次来学习一下Partial Application。我们先看一下函数的介绍&#xff0c;在维基上有简单的介绍&#xff1a; 在数学中&#xff0c;一个函数是描述每个输入值对应唯一输出值的这种对应关系&#xff0c;符号为 f(x)。例如&#xff0c;表达式 f(x)x2表示了一个函数 f&#xff0…

根据对象的属性去重,获取新数组

() let cateArr arr.reduce(function (arr, current) { hash[current.category_id] ? : hash[current.category_id] true && arr.push(current); return arr }, []); () 转载于:https://www.cnblogs.com/gkxNB/p/11428433.html

快速地创建快顶尖的医学图像处理控件ImageGear Medical

ImageGear Medical控件使开发人员能够快速地创建快顶尖的医学图像处理控件&#xff0c;可以对DICOM文件进行浏览、创建、编辑&#xff0c;可以控制图像所有切面显示和打印&#xff0c;对图像进行注释&#xff0c;以及支持ISIS和TWAIN扫描和100多种图像文件格式&#xff0c;可用…

jj为什么会变大变小_为什么上过太空的种子果实会变大?射线会让生物向大变异吗?...

在科幻电影中&#xff0c;变异是不正常力量的重要来源之一&#xff0c;所谓“富人靠科技&#xff0c;穷人靠变异&#xff01;”。但其实科幻在某种意义上一起在误导着我们&#xff0c;多数科幻作品其实是以科学为外衣的魔法故事&#xff0c;比如“爱你三千遍”的钢铁侠&#xf…

公司人才招聘管理系统

课程设计名称 公司人才招聘管理系统 完成时间&#xff08;起、止&#xff09; 所属小组 课程设计的目的 1、学习和巩固C语言程序设计的方法&#xff0c;充分体会C语言在程序设计方面的强大功能和独特之处&#xff1b; 2、了解用C语言开发项目的一般过程&#xff0c;…

RabbitMQ 示例-生产者-消费者-direct-topic-fanout

这是生产者和消费者2个项目&#xff0c; 包含 direct&#xff0c;topic&#xff0c;fanout模式下的消费&#xff0c;springboot rabbitmq 代码地址&#xff1a;https://github.com/duende99/RabbitMQ.git转载于:https://www.cnblogs.com/duende99/p/11440435.html

CutJS – 用于 HTML5 游戏开发的 2D 渲染引擎

CutJS 是轻量级的&#xff0c;快速的&#xff0c;基于 Canvas 开发的 HTML5 2D 渲染引擎&#xff0c;可以用于游戏开发。它是开源的&#xff0c;跨平台的&#xff0c;与现代的浏览器和移动设备兼容。CutJS 提供了一个类似 DOM 树的数据模型来编写应用程序&#xff0c;并在内部…

LeetCode65——Valid Number(使用DFA)来判断字符串是否为数字

题目&#xff1a; 参考解法&#xff1a;&#xff08;DFA&#xff09; class Solution { public:bool isNumber(string str) {int state0, flag0; // flag to judge the special case "."while(str[0] ) str.erase(0,1);//delete the prefix whitespace while(str[s…

win10商店下载位置_Win10删应用商店下载记录|Win10删Microsoft Store下载记录

Win10中的Microsoft Store&#xff0c;也称微软应用商店&#xff0c;提供给Windows用户下载安装使用各种应用&#xff0c;因此有些用户&#xff0c;会在这里下载软件&#xff0c;不过&#xff0c;在使用时间长了&#xff0c;也是会产生下载记录的。这篇文章是PE吧给大家带来的W…

HDU 1000 A + B Problem

Problem DescriptionCalculate A B.InputEach line will contain two integers A and B. Process to end of file.OutputFor each case, output A B in one line.Sample Input1 1Sample Output2题意&#xff1a;求输入的两个数之和。分析&#xff1a;注意题目暗含循环输入输出…

背景框代码

新建一个html文件&#xff0c;放入即可github: https://github.com/duende99/background_XXX.git 转载于:https://www.cnblogs.com/duende99/p/11442302.html

【原创】什么是 wire protocol

2019独角兽企业重金招聘Python工程师标准>>> 究竟 wire protocol 是指什么&#xff1f;下面这段话可以比较清楚的解释&#xff08;原本来自 这里 &#xff09;。 In a network, a wire protocol is the mechanism for transmitting data from point a to point b. T…

负数除以整数的余数怎么算?

由带余除法&#xff0c;任何一个整数n&#xff0c;都可以表示成nk*qr&#xff0c;其中0<r<q 这里的r就是n除以q的余数&#xff0c;通常记为n≡r(mod q) 例如-9(-2)*51&#xff0c;则-9除以5的余数为1。