18.4.5 元素插入
成员函数insert用于将string对象、字符数组或字符插入到string字符串中,这和追加append有些相似,只是元素插入不再局限于尾部,可以插入到中间,因此需要一个指示插入位置的参数,该参数可以是位置,也可以是迭代器,数据将被插入该位置的前面,insert()函数的重载形式如下所示。
(1)string&append(size_type pos,const char*s);
(2)string&append(size_type pos,const char*s,size_type n);
(3)string&append(size_type pos,const string&str);
(4)string&append(size_type pos,const string&str,size_type pos1,size_type n);
(5)string&append(size_type pos,size_type n,char c);
(6)void insert(Iterator pos,Iterator First,Iterator Last);
(7)void insert(Iterator pos,size_type n,char c);
如果插入位置超过了目标字符串长度,或者说pos1超过了要插入的字符串str的结尾,out_of_rang异常将被抛出,如果插入后得到的字符串长度大于最大长度,length_error异常将被抛出。