c++中 append()函数用法

c++中 append()函数用法append()函数常用的函数原型是:basic_string&append(constbasic_string&str);basic_string&append(constchar*str);basic_string&append(constbasic_string&str,size_typeindex,size_typelen);basic_string&append(constchar

大家好,又见面了,我是你们的朋友全栈君。

string::append官方介绍网址
append()函数:是向string 的后面追加字符或字符串。

常用的函数原型、简例:

1.在字符串的末尾添加字符串str

  string& append (const string& str);	
  string& append (const char* s);

1)在string的末尾添加string。如下:

string s1="hello";
string s2= "the";
string s3="world";
s1.append(s2);  //把字符串s连接到当前字符串的结尾
s1+=s3;
s1="hello the";
s1="hello the world";

2)在string的末尾添加C-string。把c类型字符串s连接到当前字符串结尾,如下:

string s = "hello";
const char*c="world";
s.append(c);   
s="hello world";

2.在字符串的末尾添加字符串str的子串,子串以subpos索引开始,长度为sublen

string& append (const string& str, size_t subpos, size_t sublen);

1)在string的后面添加string的一部分,即一个子串,子串以4索引开始,长度为5。如下:

string s1 = "hello";
string s2 = "the world";
s1.append(s2,4,5);  //把字符串从s2中从4开始的5个字符连接到当前字符串的结尾
s1 = "hello world";

2)若是添加的子串中只有索引开始的位置,没有长度,则表示字符串从第n个字符到末尾的字符连接到当前字符串末尾,如下:

string s1 = "hello";
string s2 = "the world";
s1.append(s2, 3);
运行结果为:s1="hello world"

3.在字符串的末尾添加字符串s中的前n个字符

string& append (const char* s, size_t n)

在string的后面添加C-string的一部分。把c类型字符串c的前n个字符连接到当前字符串结尾,如下:

string s = "hello";
const char*c = "the world";
s.append(c,3);
运行结果为:s="hellothe";

4.在字符串的末尾添加n个字符c;

string& append (size_t n, char c);

在string后面添加多个相同字符,如下:

  string s1 = "hello";
  s1.append(3, '!'); //在当前字符串结尾添加3个字符!
  运行结果为 s1 = "hello!!!";

5)在字符串的末尾添加以迭代器first和last表示的字符序列

string& append (InputIterator first, InputIterator last););

把s2的迭代器begin()+4和end()之间的部分连接到当前字符串的结尾

string s1 = "hello"
string s2 = "the world";
s1.append(s2.begin()+4,s2.end());//把s2的迭代器begin()+4和end()之间的部分连接到当前字符串的结尾
运行结果为:s1 = "hello world";

例1.string str4(“Wow”);与string str4 = “wow”;等同。

#include<iostream> 
#include <string>
using namespace std;

int main()
{ 
   
	string str1 = "Hey";
	string str2 = ",look the world.";
	string str3 = "Hello";
	//string str4("Wow");
	string str4 = "wow";
	//................................................

	str1.append(str2);
	//str3.append(str2, 5);
	str3.append(str2, 5, 11);
	str4.append(5, '.');
    //................................................

	cout << str1 << endl;
	cout << str3 << endl;
	cout << str4 << endl;
	system("pause");

	return 0;

}

运行结果为

Hey,look the world.
Hello the world.
wow.....
请按任意键继续. . .

例2.

// appending to string
#include <iostream>
#include <string>

int main()
{ 
   
	std::string str;
	std::string str2 = "Writing ";
	std::string str3 = "print 10 and then 5 more";

	// used in the same order as described above:
	str.append(str2);                       // "Writing "
	str.append(str3, 6, 3);                   // "10 "
	str.append("dots are cool", 5);          // "dots "
	str.append("here: ");                   // "here: "
	str.append(10u, '.');                    // ".........."
	str.append(str3.begin() + 8, str3.end());  // " and then 5 more"
	//str.append<int>(5, 0x2E); // "....."

	std::cout << str << '\n';
	return 0;
}

运行结果为

Writing 10 dots here: .......... and then 5 more
请按任意键继续. . .



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150525.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • r软件的下载与安装_R语言怎么安装

    r软件的下载与安装_R语言怎么安装一、R软件下载下载地址:https://cran.r-project.org/图1R软件下载页面下载之后是.exe执行文件,不是zip压缩格式文件,可以直接点击安装。二、R软件安装过程一直向下执行,直到安装完毕。最后一步是设置系统环境变量:三、Rstudio下载及安装过程下载地址:https://www.rstudio.com/products/rstudio/downl…

    2022年4月19日
    82
  • windows下如何安装Composer?

    windows下如何安装Composer?

    2021年10月20日
    116
  • 提高 Activity 跳转速度

    提高 Activity 跳转速度从一个页面跳转到另外一个页面,这在Android中是很常见的操作;有的页面跳转是瞬间打开,有的是会微微钝一下才打开;或者在页面里弹出一个Dialog弹框,明明已经调用完了show()方法,但弹框没马上弹出来,延迟了一百多毫秒才显示出来,这样给人的体验就很不好了。为什么会出现这种情况呢?页面跳转迟钝和弹框的展示延迟的原因都一样,基本分为两大类。第一类就是看看是否在UI线程左耗时操作,这个经常是数据方面的问题,比如说要从数据库里面查询数据,或者说对数据做了一些复杂逻辑的操作,例如要对集合中的数据,

    2022年5月21日
    35
  • CSS3之border-radius圆角

    CSS3之border-radius圆角

    2021年9月21日
    51
  • RPN网络讲解

    RPN网络讲解讲完了anchor机制,接下来我们讲RPN(regionproposalnetwork)区域候选网络。_build_network:https://github.com/endernewton/tf-faster-rcnn/blob/master/lib/nets/network.py原理解释FeatureMap进入RPN后,先经过一次33的卷积,同样,特征图大小依然是6040,数量…

    2022年6月23日
    43
  • RestEasy 调用Rest接口使用详解[通俗易懂]

    RestEasy 调用Rest接口使用详解[通俗易懂]RestEasy 调用Rest接口使用详解

    2022年6月17日
    67

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号