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
请按任意键继续. . .



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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

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