C++ string类的insert函数的介绍及使用

1.在特定位置插入const char *字符串

(1)string &insert(int p0, const char *s)
功能:在原字符串下标为pos的字符前插入字符串str
返回值:插入字符串后的结果

(2)string &insert(int p0, const char *s, int n);
功能:在p0位置插入字符串s的前n个字符
返回值:插入字符串后的结果

    string str1="Working people ";
    string str2="Working soul ";
    string str3="Working people are Exalted";

    //string &insert(int p0, const char *s)
    //在原字符串下标为pos的字符前插入字符串str
    //返回值:插入字符串后的结果

    auto str=str1.insert(15,"Working soul ");
    cout<<"str1: "<<str1<<endl;                          		//str1: Working people Working soul
    cout<<"str: "<<str<<endl;                           	 	 //str: Working people Working soul

    //string &insert(int p0, const char *s, int n);
    //在p0位置插入字符串s的前n个字符
    //返回值:插入字符串后的结果

    str=str1.insert(28,"Working people are Exalted",26);
    cout<<"str1: "<<str1<<endl;                         	     //str1: Working people Working soul Working people are Exalted
    cout<<"str: "<<str<<endl;                                   //str: Working people Working soul Working people are Exalted

2.在特定位置插入 const string型字符串

(1)string &insert(int p0,const string &s);
功能:在p0位置插入字符串s
返回值:插入字符串后的结果
(2)string &insert(int p0,const string &s, int pos, int n);
功能:在p0位置插入字符串s从pos开始的连续n个字符
返回值:插入字符串后的结果

	//string &insert(int p0,const string &s);
    //在p0位置插入字符串s
    //返回值:插入字符串后的结果
	str1="Working people Working soul Working people are Exalted";
    string str4="I am ";
    str=str1.insert(0,str4);
    cout<<"str1: "<<str1<<endl;             //str1: I am Working people Working soul Working people are Exalted
    cout<<"str: "<<str<<endl;               //str: I am Working people Working soul Working people are Exalted

    //string &insert(int p0,const string &s, int pos, int n);
    //在p0位置插入字符串s从pos开始的连续n个字符
    //返回值:插入字符串后的结果
    str1="Hello ";
    str2="World";
    str=str1.insert(6,str2,0,5);
    cout<<"str1: "<<str1<<endl;             //str1: Hello World
    cout<<"str: "<<str<<endl;               //str: Hello World

3.在特定位置插入char型字符.

(1)string &insert(int p0, int n, char c);
功能:在p0处插入n个字符c
返回值:插入字符串后的结果
(2)void insert(iterator it, int n, char c);
功能:在it处插入n个字符c
(3)iterator insert(iterator it, char c)
功能:在it处插入字符c
返回值:插入字符的位置

	//string &insert(int p0, int n, char c);
    //在p0处插入n个字符c
    //返回值:插入字符串后的结果
    str1="Hello ";
    str=str1.insert(6,5,'y');
    cout<<"str1: "<<str1<<endl;          //str1: Hello yyyyy
    cout<<"str: "<<str<<endl;            //str:Hello yyyyy

    //void insert(iterator it, int n, char c);
    //在it处插入n个字符c
    str1="Hello ";
    str1.insert(str1.end(),5,'y');
    cout<<"str1: "<<str1<<endl;          //str1: Hello yyyyy

    //iterator insert(iterator it, char c)
    //在it处插入字符c
    //返回值:插入字符的位置
    str1="ello ";
    string::iterator it=str1.insert(str1.begin(),'H');
    cout<<"str: "<<str1<<endl;           //str:Hello
    cout<<"*it: "<<*it<<endl;            //*it: H

4.插入迭代器所指的字符串

(1)void insert(iterator it, const_iterator first, const_iterator last);
功能:在it处插入从first开始至last-1的所有字符

	//void insert(iterator it, const_iterator first, const_iterator last);
    //在it处插入从first开始至last-1的所有字符
    str1="Hello ";
    str2="World";
    str1.insert(str1.end(),str2.begin(),str2.end());
    cout<<"str1: "<<str1<<endl;          //str1: Hello World
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值