string插入和删除

1.功能:对string字符串进行插入和删除的操作

2.函数原型:

  1. string &insert(int pos,const char *s); //插入字符
  2. string &insert(int pos,const string &str); //插入字符
  3. string &insert(int pos,int n,char c); //在指定位置插入n个字符c
  4. string &erase(int pos,int n=npos)l; //删除从pos开始的n个字符
    #include<iostream>
    #include<string>
    using namespace std;
    
    void test1()
    {
    	string s="hllo";
    	
    	//插入 
    	cout<<"插入之前 s = "<<s<<endl; 
    	s.insert(1,"e"); //在下标1位置插入e,原下标1及其之后的字符全部都后移 
    	cout<<"插入之后 s = "<<s<<endl;
    	
    	//删除
    	cout<<"删除之前 s = "<<s<<endl; 
    	s.erase(1,3);  //删除从下标1开始及其后面2个,共3个字符 
    	cout<<"删除之后 s = "<<s<<endl; 
    	
    	
    }
    void test2()
    {
    }
    int main()
    {
    	test1();
    	test2();
    	return 0;
     } 
    
    /*
    打印结果:
    插入之前 s = hllo
    插入之后 s = hello
    删除之前 s = hello
    删除之后 s = ho
    */

     

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值