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


相关推荐

  • ubuntu16.04安装cuda10.2_opencv cuda

    ubuntu16.04安装cuda10.2_opencv cuda由于实验需要,在实验室电脑上搭建深度学习Caffee框架。一共花了两天的时间,其中遇到了不少的问题,记录一下。Caffee在配置上相对来说比较麻烦,需要前期安装的东西比较多,逐一介绍。CUDA:NVIDIA系列显卡支持的GPU编程框架,其实如果本身电脑是AMD的显卡,不用装也可用Caffee,只是速度会比较慢。所以最好有一块像样的显卡,最后我就败在这个上面了。。。MKA或是OpenAtlas

    2025年6月22日
    2
  • JS 面试题 大全

    JS 面试题 大全1、介绍一下js的数据类型有哪些,值是如何存储的?2、说一下js的数据类型的转换都有哪些?3、如何去判断js数据类型?4、介绍js有哪些内置对象?5、javascript创建对象的几种方式?6、js获取原型的方法?7、什么是闭包,为什么要用它?8、三种事件模型是什么?9、哪些操作会造成内存泄漏?10、简述javascript中this的指向?![在这里插入图片描述](https://img-blog.csdnimg.cn/2021032219142296.png

    2022年8月26日
    8
  • latex参考文献为网址「建议收藏」

    latex参考文献为网址「建议收藏」1、引入hyperref包:\usepackage{hyperref }2、举例:@misc{A:2005,  author = {AlexanderTotok},  title = {hello },  howpublished = {\url{http://cs.nyu.edu/totok/professional/software/tpcw/tpcw.html}}

    2025年10月16日
    4
  • web版聊天功能简单实现

    web版聊天功能简单实现一、问题核心点:如何找到要发送的人?要完成一个功能我觉得首先要分析该功能的逻辑及技术难点,而不是盲目的直接就撸代码,这样非常浪费时间。个人觉得web版聊天功能没什么实际应用场景,以前看过中国移动好

    2022年7月2日
    26
  • 投巧解决JavaScript split方法出现空字符的问题

    投巧解决JavaScript split方法出现空字符的问题直接使用split,前后各有一个“”值。>>varstr=’,a,b,c,d,e,f,’;>>str.split(‘,’);//(8)[“”,”a”,”b”,”c”,”d”,”e”,”f”,””]临时方法:split后,可以用filter过滤掉空值。>>varstr=’,a,b,c,d,e,f,’;>&…

    2025年8月8日
    4
  • 冒泡法排序_冒泡选择排序算法

    冒泡法排序_冒泡选择排序算法/*冒泡法排序:共进行N-1趟比较,每趟比较中要进行N-1-i次两两比较时间复杂度:最差、平均都是O(n^2),最好是O(n)空间复杂度:O(1)稳定排序*/

    2022年10月18日
    1

发表回复

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

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