正在加载……
专注、离线、切勿分心
basic_string& insert( size_type index, size_type count, CharT ch );
在index位置插入count个字符ch
string str = "meihao";
string sstr = str.insert(0,2,'a');
cout<<sstr<<endl;   //aameihao
basic_string& insert( size_type index, const CharT* s );
index位置插入一个常量字符串
string str = "meihao";
string sstr = str.insert(1,"hello~");
cout<<sstr<<endl;   //mhello~eihao
basic_string& insert( size_type index, const CharT* s, size_type count );
index位置插入常量字符串中的count个字符
string str = "meihao";
string sstr = str.insert(1,"hello~",3);
cout<<sstr<<endl;  //mheleihao
basic_string& insert( size_type index, const basic_string& str );
index位置插入常量string
string str = "meihao";
string sstr = str.insert(1,str);
cout<<sstr<<endl;  //mmeihaoeihao
basic_string& insert( size_type index, const basic_string& str,
                      size_type index_str, size_type count );
index位置插入常量str的从index_str开始的count个字符
string str1 = "meihao";
string str2 = "hello~";
string sstr = str2.insert(6,str1,3,3);
cout<<sstr<<endl;  //hello~hao
basic_string& insert( size_type index, const basic_string& str,
                      size_type index_str, size_type count = npos);
index位置插入常量str从index_str开始的count个字符,count可以表示的最大值为npos.这个函数不构成重载
npos表示一个常数,表示size_t的最大值,string的find函数如果未找到指定字符,返回的就是一个npos
string str1 = "meihao";
string str2 = "hello~";
string sstr = str2.insert(6,str1,3,string::npos);
cout<<sstr<<endl;  //hello~hao
cout<<string::npos<<endl;  //4294967295
iterator insert( iterator pos, CharT ch );
iterator insert( const_iterator pos, CharT ch );
string str1 = "meihao";
string::iterator it = str1.insert(str1.begin(),'a');
// string::iterator it = str1.insert(str1.cbegin(),'a');  //第二种
cout<<str1<<endl;  //ameihao
cout<<*it<<endl;  //a
void insert( iterator pos, size_type count, CharT ch );
迭代器指向的pos位置插入count个字符ch

string str1 = "meihao";
str1.insert(++str1.begin(),2,'a');
cout<<str1<<endl;  //maaeihao
iterator insert( const_iterator pos, size_type count, CharT ch );
迭代器指向的pos位置插入count个字符ch
string str1 = "meihao";
string::iterator it = str1.insert(++str1.cbegin(),2,'a');  //和上面的可以混用
cout<<str1<<endl;  //maaeihao
void insert( iterator pos, InputIt first, InputIt last );

iterator insert( const_iterator pos, InputIt first, InputIt last );
string str1 = "meihao";
string str2 = "hello";
//string::iterator it = str1.insert(str1.begin(),str2.begin(),str2.end());
string::iterator it = str1.insert(str1.begin(),std::begin(str2),std::end(str2));
cout<<str1<<endl;  //hellomeihao
cout<<*it<<endl;  //h
posted on 2018-09-18 18:33  正在加载……  阅读(35122)  评论(0编辑  收藏  举报