c语言 java append_C++中append函数的用法和函数定义。谢谢!

展开全部

要想使用标准C++中string类,必须要包含

#include // 注意是,不62616964757a686964616fe78988e69d8331333339663434是,带.h的是C语言中的头文件

using std::string;

using std::wstring;

using namespace std;

下面你就可以使用string/wstring了,它们两分别对应着char和wchar_t。

string和wstring的用法是一样的,以下只用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,

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::npos

int 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::npos

int 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相似,只不过是从后向前查找

本回答被提问者采纳

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

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

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

相关文章

很好用的程序员在线画图软件

今天向大家推荐一个很好用的在线画图软件&#xff1a;今天向大家推荐一个很好用的在线画图软件&#xff1a;今天向大家推荐一个很好用的在线画图软件&#xff1a;&#xff08;重要的事情说三篇&#xff09;连接地址如下&#xff1a;https://www.processon.com/i/55e3195de4b0df…

java breakpoint_java断点

第一步&#xff1a;用firefox运行程序&#xff0c;当点击保存&#xff0c;提示保存失败后&#xff0c;启动firebug通过请求找到addNew.ezt出现错误&#xff0c;在eztnews.xml里通过ctrlF查找找到请求执行的类和方法找到NewsAction类的doAddNew方法然后在通过找到NewsActions.ja…

OC之ARC环境中的循环strong问题

2019独角兽企业重金招聘Python工程师标准>>> main.m文件&#xff1a; #import <Foundation/Foundation.h> #import "Person.h" #import "Dog.h"int main() {Person *p [[Person alloc] init];Dog *d [[Dog alloc] init];p.dog d;d.per…

Android自定义view之圆形进度条

本节介绍自定义view-圆形进度条思路&#xff1a;根据前面介绍的自定义view内容可拓展得之&#xff1b;1&#xff1a;新建类继承自View2&#xff1a;添加自定义view属性3&#xff1a;重写onDraw(Canvas canvas)4&#xff1a;实现功能下面上代码1.自定义view代码&#xff1a; pub…

java二级考试备考_2017计算机二级考试《JAVA》备考测试题「带答案」

2017计算机二级考试《JAVA》备考测试题「带答案」为确保同学们将所涉及的考点全面复习到位&#xff0c;让大家充满信心的步入考场&#xff0c;以下是百分网小编搜索整理的一份计算机二级考试《JAVA》备考测试题【带答案】&#xff0c;供参考练习&#xff0c;希望对大家有所帮助…

java流类图结构_java学习之IO流(学习之旅,一)

个人在学习IO流的时候看到如下所示java 流类图结构的时候&#xff0c;我的感想是&#xff0c;这么多处于蒙的状态。Java流类图结构这么多&#xff0c;没有分类不好学&#xff0c;那我们就慢慢一口一口的吃&#xff0c;这样每天学习一点就好了&#xff0c;其实很多类并不是常用的…

php 安装xdebug扩展

php 扩展获取地址 http://pecl.php.net/package/ 编译安装的过程 wget http://pecl.php.net/get/xdebug-2.2.2.tgz tar -zxvf xdebug-2.2.2.tgz cd xdebug-2.2.2/ /data/klj/php/bin/phpize ./configure --enable-xdebug --with-php-config/data/klj/php/bin/php-config mak…

拨打电话 java_简单拨打电话程序

众所周知,对于一个手机,能拨打电话是其最重要也是最常用的一个功能.而在Android里是怎么样实现拨打电话的程序呢?我在这里写了一个简单的拨打电话的Demo,供大家参考.一共分为5个步骤.Step 1:新建一个Android工程,命名为phoneCallDemo.Step 2:设计程序的界面,打开main.xml把内容…

WPF01(xaml)

XAML&#xff1a;&#xff08;转自http://www.cnblogs.com/huangxincheng/archive/2012/06/17/2552511.html&#xff09; <Window x:Class"WpfApplication1.MainWindow"xmlns"http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x"…

java 线程 状态 图_Java提高——多线程(一)状态图

操作系统中的进程和线程的概念进程是指一个内存运行的应用程序&#xff0c;每个进程都有自己独立的一块内存空间&#xff0c;一个进程中可以启动多个线程&#xff0c;比如windows下的一个运行的应用程序.exe就是一个进程。线程是指进程中的一个执行流&#xff0c;一个进程可以运…

幽幽的灵光射不出你想要的疯狂

秋天到了&#xff0c;忧伤便无处可逃&#xff0c;秋天的忧伤的气息&#xff0c;就像一个妖艳的美女躺在你的身边&#xff0c;让你热血沸腾&#xff0c;冲动无比&#xff0c;而又悲喜交加&#xff0c;忧愁满地。如果不信&#xff0c;你可以试试。分享一首去年的诗歌&#xff0c;…

找规律

找规律填写NN方阵。如N8时, 其方阵为: 1 1 1 1 1 1 1 11 2 2 2 2 2 2 11 2 3 3 3 3 2 11 2 3 4 4 3 2 11 2 3 4 4 3 2 11 2 3 3 3 3 2 11 2 2 2 2 2 2 11 1 1 1 1 1 1 1 上代码&#xff1a; 1 #include <stdio.h&g…

arm qt5 iconv 问题

2019独角兽企业重金招聘Python工程师标准>>> 问题 3&#xff1a;./system/rootlib/helloworld -qws &#xff0c;程序运行起来&#xff0c;仍报错 QIconvCodec::convertFromUnicode: using Latin-1 for conversion, iconv_open failed …

[转]用Whois获得电信运营商的IP地址是如何分配的?

[转]用Whois获得电信运营商的IP地址是如何分配的? Linux下获得一些中国电信运营商的IP地址分配情况: APNIC是管理亚太地区IP地址分配的机构&#xff0c;它有着丰富准确的IP地址分配库&#xff0c;同时这些信息也是对外公开的&#xff0c;并提供了一个查询工具&#xff0c;下面…

庆祝教师节,李宁老师课程优惠劵疯抢中、会员卡优惠中,先到先得

李宁老师会员卡&#xff08;9-10至9-14&#xff09;大优惠&#xff1a;http://edu.51cto.com/member/id-12_1.html优惠劵只能购买李宁老师的视频课程&#xff1a;http://edu.51cto.com/member/id-12_1.html 优惠劵有效期&#xff1a;2015-9-10 至 2015-9-14 购买规则&#xf…

java mset_Java 反射机制(包括组成、结构、示例说明等内容)

第1部分 Java 反射机制介绍Java 反射机制。通俗来讲呢&#xff0c;就是在运行状态中&#xff0c;我们可以根据“类的部分已经的信息”来还原“类的全部的信息”。这里“类的部分已经的信息”&#xff0c;可以是“类名”或“类的对象”等信息。“类的全部信息”就是指“类的属性…

自己动手,实现一种类似ListT的数据结构(二)

前言&#xff1a; 首先&#xff0c;小匹夫要祝各位看官圣诞快乐&#xff0c;新年愉快&#xff5e;。上一篇文章《自己动手&#xff0c;实现一种类似List<T>的数据结构(一&#xff09;》 介绍了一下不依靠List<T>实现的各种接口&#xff0c;仿造一个轻量级数据结构的…

局域网内连接MySQL

2019独角兽企业重金招聘Python工程师标准>>> 局域网内连接MySQL 博客分类&#xff1a; MySQL MySQL局域网连接grant 我们都知道连接MySQL一般用的语句就是 jdbc:mysql://localhost:3306/database&#xff0c; 但是当你要连接到其他机器上的mysql的时候&#xff0c;…

[leetcod] Clone Graph

题目&#xff1a; Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJs undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and …

【iOS7一些总结】9、与列表显示(在):列表显示UITableView

列表显示&#xff0c;顾名思义它是在一个列表视图的形式显示在屏幕上的数据的内容。于ios在列表视图UITableView达到。这个类在实际应用中频繁&#xff0c;是很easy理解。这里将UITableView的主要使用方法总结一下以备查。UITableView定义在头文件UITableView.h中&#xff0c;详…