int main()
{
/*
**测试basic_string& append (const basic_string& str);
*/
string st1("123456789");
string str1("abc");
string& ss = st1.append(str1);
cout << ss << endl;//123456789abc
cout << st1 << endl;//123456789abc
/*
**测试basic_string& append (const basic_string& str, size_type subpos, size_type sublen);
*/
string st2("123456789");
string st3("123456789");
string str2("abcdefg");
st2.append(str2, 2, 3);
cout << st2 << endl;//123456789cde
st3.append(str2, 3, 20);//超出范围
cout << st3 << endl;//123456789defg 当用数字表示范围时,若超出范围,则直到字符串的结尾,不会补空格,也不会出现运行时错误
cout << st3.length() << endl;//13 尽管如此,在实际编程时,也必须保证不超范围
/*
**测试basic_string& append (const charT* s);
*/
string st4("123456789");
st4.append("abc");
cout << st4 << endl;//123456789abc
/*
**测试basic_string& append (const charT* s, size_type n);
*/
string st5("123456789");
st5.append("abc", 5);//超出范围
cout << st5 << endl;//123456789abc+乱码
/*
**测试basic_string& append (size_type n, charT c);
*/
string st6("123456789");
st6.append(3, 0x41);//可以用16进制的ASCII码
cout << st6 << endl;//123456789AAA
/*
**测试template <class InputIterator>
**basic_string& append (InputIterator first, InputIterator last);
*/
/*
string st7("123456789");
string str3("abcdefg");
st6.append(str3.begin() + 2, str3.begin() + 10);//超出范围
cout << st7 << endl;//当使用迭代器时,若超出范围,则会出现运行时错误
*/
return 0;
}
7.replace()
原型:
//string (1)
basic_string& replace (size_type pos, size_type len, const basic_string& str);
basic_string& replace (const_iterator i1, const_iterator i2, const basic_string& str);
//substring (2)
basic_string& replace (size_type pos, size_type len, const basic_string& str,
size_type subpos, size_type sublen);
//c-string (3)
basic_string& replace (size_type pos, size_type len, const charT* s);
basic_string& replace (const_iterator i1, const_iterator i2, const charT* s);
//buffer (4)
basic_string& replace (size_type pos, size_type len, const charT* s, size_type n);
basic_string& replace (const_iterator i1, const_iterator i2, const charT* s, size_type n);
//fill (5)
basic_string& replace (size_type pos, size_type len, size_type n, charT c);
basic_string& replace (const_iterator i1, const_iterator i2, size_type n, charT c);
//range (6)
template <class InputIterator>
basic_string& replace (const_iterator i1, const_iterator i2,
InputIterator first, InputIterator last);
//initializer list (7)
basic_string& replace (const_iterator i1, const_iterator i2, initializer_list<charT> il);
(7)不详述
示例:(这里仅给出部分原型的测试,其它的读者可参照前面几个函数的实例自行测试)
#include<iostream>
#include<string>
using namespace std;
int main()
{
/*
**测试basic_string& replace (size_type pos, size_type len, const basic_string& str);
*/
string st1("123456789");
string str1("abcde");
string str2("ab");
st1.replace(2, 3, str1);//将位置2开始的3个字符(345)换成abcde
cout << st1 << endl;//12abcde6789
string st2("123456789");
st2.replace(1, 7, str1);
cout << st2 << endl;//1abcde9
string st3("123456789");
st3.replace(6, 9, str2);//超出范围
cout << st3 << endl;//123456ab
/*
**测试basic_string& replace (const_iterator i1, const_iterator i2, const basic_string& str);
*/
/*string st4("123456789");
st4.replace(st4.begin() + 8, st4.begin() + 10, str1); //迭代器超范围,出现运行时错误
cout << st4 << endl;*/
/*
**测试basic_string& replace (size_type pos, size_type len, const charT* s, size_type n);
*/
string st5("123456789");
st5.replace(2, 3, "abcdefg", 5);
cout << st5 << endl;//12abcde6789
string st6("123456789");
st6.replace(2, 3, "abc", 20);//超出范围
cout << st6 << endl;//12abc+乱码+6789 对于char*类型,若超出范围,就会出现乱码
return 0;
}