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

c++中 append()函数用法相关推荐

  1. python中append函数什么意思_python中append函数用法讲解

    python中append函数用法讲解 如果在做一个地区的统计工作,可以使用列表来帮助我们.输入汉字或者其他字符,比如"01代表汉族",那么在写民族的时候有下拉列表,就可以打01, ...

  2. pythonappend用法_python中append实例用法总结

    append()函数 描述:在列表ls最后(末尾)添加一个元素object 语法:ls.append(object) -> None 无返回值 例: a=[1,2,3] a.append(5) ...

  3. python中append的用法是什么?

    append(object) 是将一个对象作为一个整体添加到列表中,添加后的列表比原列表多一个元素,该函数的参数可以是任何类型的对象 例如: a=[1,2,3,4] a.append(5) 此时,运行 ...

  4. C++的append函数用法

    C++的append函数用法 append是向string对象的尾部添加字符串或者字符. 1.添加C字符串 string A = "abc"; const char* B = &q ...

  5. php foreach嵌套foreach,php中foreach怎么嵌套foreach PHP中foreach函数用法?

    foreach的使用方法小编不是很明确,分享达人指教一下.foreach (array_expressforeach($array as $key) { if(xxxx) { break; //bre ...

  6. C++中substr()函数用法详解

    C++中substr()函数用法详解 原型: string substr (size_t pos = 0, size_t len = npos) const; 返回一个新构造的string对象,其值初 ...

  7. python print函数用法_Python3.2中Print函数用法实例详解

    本文实例讲述了Python3.2中Print函数用法.分享给大家供大家参考.具体分析如下: 1. 输出字符串 >>> strHello = 'Hello World' >> ...

  8. format函数python的顺序_[转载] Python中format函数用法

    Python中format函数用法 format优点 format是python2.6新增的格式化字符串的方法,相对于老版的%格式方法,它有很多优点. 1.不需要理会数据类型的问题,在%方法中%s只能 ...

  9. php使用referer,php中HTTP_REFERER函数用法实例

    本文实例分析了php中HTTP_REFERER函数用法.分享给大家供大家参考.具体分析如下: 利用php的http_referer函数来判断用户的来路,这是比较简单的,实例代码如下: if (isse ...

  10. php中 param,php中bind_param()函数用法分析

    本文实例讲述了php中bind_param()函数用法.分享给大家供大家参考,具体如下: 从字面上不难理解,绑定的参数:下面我通过一个绑定参数的例子讲一下: for example: bind_par ...

最新文章

  1. [node 工具] 用 Node.js 将 bugzilla 上的 bug 列表导入到 excel 表格在线版本之一(server 端)...
  2. 神经网络相关名词解释
  3. QMsgPack的用法DEMO
  4. 全国计算机技术与软件专业技术资格(水平)考试徽标
  5. viterbi算法_序列比对(十四)——viterbi算法和后验解码的比较
  6. STM32学习之C语言知识复习
  7. re:Invent 2020首日发布43项新产品与功能,涵盖新实例、容器、Serverless、机器学习等领域!
  8. c++ STL平常练习-3
  9. ask调制与解调matlab仿真,ask调制与解调的matlab仿真.doc
  10. Multisim10~14软件包及安装手册+pojie软件
  11. zmap扫描mysql_zmap/masscan 快速扫描网络
  12. php网站渗透实战_PHP网站安全-漏洞渗透及解决方式—概述
  13. MybatisCodeHelperPro的使用
  14. PSV破解流程+软件游戏安装(最简单/最快的方法整理,已测支持3.65~3.68,理论上支持全系列版本)
  15. 立体匹配 -- PSM-Net 网络模型代码剖析
  16. Win10便签在哪?Win10桌面便签怎么打开和使用?
  17. 米扑科技助力公益:寻找失踪儿童一起回家
  18. bidirectional PIM
  19. 云服务器性能测试的方法
  20. 网线直连,Synergy低延迟顺滑共享鼠标键盘

热门文章

  1. QCY T3 蓝牙耳机连接电脑 声音断续卡顿
  2. AndrOid系统亭子运行,饼干智能好物开箱 篇二十二:给父母换个手机,让他成楼下凉亭的拍照KOL...
  3. php blowfish 解密,php blowfish加密解密算法
  4. 【Excel2019(十五):条件格式与公式】【使用简单的条件格式+定义多重条件的条件格式+使用公式定义条件格式】
  5. 聚美优品广告词和经典分析
  6. 渗透测试 ( 10 ) --- 扫描 web目录、文件 (dirb、wfuzz、wpscan、nikto)
  7. 会议OA项目之我的审批签字功能
  8. 静水流深,闻喧享静 空山鸣响,见惯司空
  9. python怎么循环合并数组_python数组循环合并python执行系统命令四种方法比较
  10. 新词发现-helloNLP