string::insert
string (1) |
string& insert (size_t pos, const string& str); |
---|---|
substring (2) |
string& insert (size_t pos, const string& str, size_t subpos, size_t sublen); |
c-string (3) |
string& insert (size_t pos, const char* s); |
buffer (4) |
string& insert (size_t pos, const char* s, size_t n); |
fill (5) |
string& insert (size_t pos, size_t n, char c); iterator insert (const_iterator p, size_t n, char c); |
single character (6) |
iterator insert (const_iterator p, char c); |
range (7) |
template <class InputIterator> iterator insert (iterator p, InputIterator first, InputIterator last); |
initializer list (8) |
string& insert (const_iterator p, initializer_list<char> il); |
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "i lyy";
string s2 = "love ";
s1.insert(2, s2);//s1的位置2插入s2
cout << s1 << endl;
string s3 = "aaenjoy00";
s1.insert(2, s3, 2, 5);//s1的位置2插入s3的位置2开始的5个字符
cout << s1 << endl;
s1.insert(7, " ");//在s1的位置7插入空格,原来的后移一个单位
cout << s1 << endl;
const char *s4 = "like";
s1.insert(2, " ");
s1.insert(2, s4);
cout << s1 << endl;
const char* s5 = "fond$$";
s1.insert(2, " ");
s1.insert(2, s5, 4);//在s1的位置插入s5的前4个字符
cout << s1 << endl;
s1.insert(2, " ");
s1.insert(2, 5, '@');//在位置2插入5个@
cout << s1 << endl;
s1.insert(2, " ");
s1.insert(s1.begin() + 2, 4, '$');//在迭代器的位置插入4个$
cout << s1 << endl;
s1.insert(2, " ");
s1.insert(s1.begin() + 2, '?');//在迭代器的位置插入一个?
cout << s1 << endl;
string s6 = "!!be keen on@@";
s1.insert(2, " ");
s1.insert(s1.begin() + 2, s6.begin() + 2, s6.end() -2);//在s1的迭代器位置插入s6迭代器的范围
cout << s1 << endl;
return 0;
}