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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 计算机定时关机命令,定时关机命令,小编教你怎么使用命令行定时关机

    计算机定时关机命令,定时关机命令,小编教你怎么使用命令行定时关机当我们在操作电脑的时候,有时会有需要定时关机,或者不在电脑前操作是需要过段时间自动关机,但是没有自带的定时关机软件,很多电脑用户又不喜欢安装第三方软件来完成该操作。那么怎么定时关机?下面,小编给大家带来了使用命令行定时关机的图文。有时候,下载一个东西,但是又要关闭电脑睡觉了,但是又想等东东下载好了,才去睡觉,想想如果能定时关机就好了。作为程序员的我,有时候就是作践自己啊,那怎么使用命令行定时关机?…

    2022年5月15日
    40
  • c语言printf()输出格式大全

    c语言printf()输出格式大全1.转换说明符     %a(%A)    浮点数、十六进制数字和p-(P-)记数法(C99)     %c            字符     %d            有符号十进制整数     %f             浮点数(包括float和doulbe)     %e(%E)    浮点数指数输出[e-(E-)记数法]     %g(%G)    浮点数不显无…

    2022年7月24日
    29
  • 如何在java中输出保留两位小数「建议收藏」

    如何在java中输出保留两位小数「建议收藏」在输出时用以下的代码:System.out.println(String.format(“%.2f”,sum));其中”%.2f”为保留两位小数,sum为要输出的数字。当然,用print输出也是可以的:System.out.print(String.format(“%.2f”,sum));…

    2022年7月7日
    77
  • Android中Calendar类的用法总结[通俗易懂]

    Android中Calendar类的用法总结[通俗易懂]Calendar是Android开发中需要获取时间时必不可少的一个工具类,通过这个类可以获得的时间信息还是很丰富的,下面做一个总结,以后使用的时候就不用总是去翻书或者查资料了。

    2022年9月23日
    1
  • 视频编解码基本流程

    视频编解码基本流程视频编解码基本框架

    2022年7月13日
    13
  • 数据库之多表联合查询

    数据库之多表联合查询一、知识点名称多表联合查询(一对多数据显示、多对多数据展示)二、知识点业务场景一对多关联:学生和成绩的关系用户与订单的关系企业与员工的关系用户与银行卡的关系多对多关联:学生和选课的关系订单和商品的关系用…

    2022年5月13日
    30

发表回复

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

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