string::append函数用法

  • 在str字符串的末尾添加字符串
string& append (const string& str, size_t subpos, size_t sublen);//如果只有索引subpos,则添加从subpos开始的后面所有字符
string& append (const char* s, size_t subpos, size_t sublen);
  • 在字符串的的末尾添加str字符串中索引为(index, index+n)的子串
string& append (const string& str, size_t subpos, size_t sublen);//如果只有索引subpos,则添加从subpos开始的后面所有字符
string& append (const char* s, size_t subpos, size_t sublen);
  • 在字符串str后面添加char字符串的前n个字符
string& append (const char* s, size_t n)
  • 在字符串str的末尾添加n个字符c
string& append (size_t n, char c);
/******************************************************************
* string::append()函数的使用
* version 20211205
******************************************************************/

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
	//1.在str字符串的末尾添加字符串
	string str1 = "hello";
	string str2 = "the";
	string str3 = "world";
	char* chs1 = "come-on.";
	str1.append(str2);
	str1 += str3;
	str1.append("lusx.");
	str1 += chs1;
	cout << str1 << endl;//输出:hellotheworldlusx.(字符串之间没有空格符)

	//2.在字符串的的末尾添加str字符串中索引为(index, index+n)的子串

	string str4;
	string str5 = "six-six-six...";
	char* chs2 = "lue-lue-lue...";
	cout << str4.append(chs2, 4, 3) << endl;//输出:lue
	string str6;
	cout << str6.append(str5, 4, 3) << endl;//输出:six
	string str6_1;
	cout << str6_1.append(str5, 4) << endl;//six-six

	//3.在字符串str后面添加char字符串的前n个字符
	string str7,str8;
	char* chs3 = "h-e-l-l-o";
	cout << str7.append(chs3, 1) << endl;//输出:h
	cout << str8.append("h-e-l-l-o", 1) << endl;输出:h

	//4.在字符串str的末尾添加n个字符c
	string str9;
	str9 = string(chs3);
	cout << str9.append(3, '!') << endl;

    return 0;
}


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
重写string::append()、map和list函数需要分别进行操作,下面我将分别介绍: 1. 重写string::append() 要重写string::append()函数,可以使用继承的方法,并重新定义该函数,如下所示: ```c++ #include <iostream> #include <string> using namespace std; class MyString : public string { public: MyString() : string() {} MyString(const char* str) : string(str) {} MyString(const string& str) : string(str) {} MyString& append(const MyString& str) { string::append(str); cout << "Custom append method called" << endl; return *this; } }; int main() { MyString s1("Hello"); MyString s2(" world"); s1.append(s2); cout << s1 << endl; return 0; } ``` 2. 重写map函数 要重写map函数,可以使用模板类继承的方法,如下所示: ```c++ #include <iostream> #include <map> using namespace std; template<typename Key, typename Value> class MyMap : public map<Key, Value> { public: void insert(const pair<Key, Value>& p) { map<Key, Value>::insert(p); cout << "Custom insert method called" << endl; } }; int main() { MyMap<int, string> myMap; myMap.insert(make_pair(1, "Hello")); myMap.insert(make_pair(2, "world")); for (auto itr = myMap.begin(); itr != myMap.end(); itr++) { cout << itr->first << " " << itr->second << endl; } return 0; } ``` 在MyMap类中重写了map类的insert方法,并在其中添加了一些自定义的操作,这里只是简单地输出一句话。在main函数中创建了一个MyMap对象,分别调用了insert方法,并输出了结果。 3. 重写list函数 要重写list函数,可以使用继承的方法,并重新定义该函数,如下所示: ```c++ #include <iostream> #include <list> using namespace std; template<typename T> class MyList : public list<T> { public: void push_back(const T& val) { list<T>::push_back(val); cout << "Custom push_back method called" << endl; } }; int main() { MyList<int> myList; myList.push_back(1); myList.push_back(2); myList.push_back(3); for (auto itr = myList.begin(); itr != myList.end(); itr++) { cout << *itr << " "; } return 0; } ``` 在MyList类中重写了list类的push_back方法,并在其中添加了一些自定义的操作,这里只是简单地输出一句话。在main函数中创建了一个MyList对象,分别调用了push_back方法,并输出了结果。 需要注意的是,在重写这些函数时,需要使用父类的函数来进行实际的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值