C++string类insert⽤法总结
basic_string& insert( size_type index, size_type count, Char T 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_t ype 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& st r );
index位置插⼊常量string string str = "meihao";
string sstr = str.insert(1,str); cout<<sstr<<endl; //mmeihaoeihao
basic_string& insert( size_type index, const basic_string& st r,
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& st r,
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.e nd());
string::iterator it = str1.insert(str1.begin(),std::begin(str2),std: :end(str2));
cout<<str1<<endl; //hellomeihao
字符串常量的用法cout<<*it<<endl; //h
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论