C++基础---string类的insert

1. string类的insert

1.1 string:string& insert (size_t pos, const string& str);

  • 原型:string& insert (size_t pos, const string& str);
  • 说明:在源字符串下标为pos处插入string型字符串str。
  • 代码示例:

    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str="to be question";
        string str1="the ";
    
        cout<<"string& insert (size_t pos, const string& str);"<<endl;
        cout<<"源字符串  :"<<str<<endl;
        str.insert(6, str1);
        cout<<"目标字符串:"<<str<<endl;
    
        system("pause");
        return 0;
    }
    =>string& insert (size_t pos, const string& str);
      源字符串 :to be question
      目标字符串:to be the question

1.2 substring:string& insert (size_t pos, const string& str, size_t subpos, size_t sublen = npos);

  • 原型:string& insert (size_t pos, const string& str, size_t subpos, size_t sublen = npos);
  • 说明:在源字符串下标为pos处插入string型字符串str中以下标处为subpos开始的sublen个字符组成的子字符串。
  • 代码示例:

    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str="to be question";
        string str2="or not to be the ";
    
        cout<<"string& insert (size_t pos, const string& str, size_t subpos, size_t sublen = npos);"<<endl;
        cout<<"源字符串  :"<<str<<endl;
        str.insert(6, str2, 13, 4);
        cout<<"目标字符串:"<<str<<endl;
    
        system("pause");
        return 0;
    }
    =>string& insert (size_t pos, const string& str, size_t subpos, size_t sublen = npos);
      源字符串  :to be question
      目标字符串:to be the question

1.3 c-string:string& insert (size_t pos, const char* s);

  • 原型:string& insert (size_t pos, const char* s);
  • 说明:在源字符串下标为pos处插入char型字符串s。
  • 代码示例:

    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str="to be question";
    
        cout<<"string& insert (size_t pos, const char* s);"<<endl;
        cout<<"源字符串  :"<<str<<endl;
        str.insert(6, "the ");
        cout<<"目标字符串:"<<str<<endl;
    
        system("pause");
        return 0;
    }
    =>string& insert (size_t pos, const char* s);
      源字符串  :to be question
      目标字符串:to be not question

1.4 buffer:string& insert (size_t pos, const char* s, size_t n);

  • 原型:string& insert (size_t pos, const char* s, size_t n);
  • 说明:在源字符串下标为pos处插入char型字符串s的前n个字符组成的子字符串。
  • 代码示例:

    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str="to be question";
    
        cout<<"string& insert (size_t pos, const char* s, size_t n);"<<endl;
        cout<<"源字符串  :"<<str<<endl;
        str.insert(6, "the question ", 4);
        cout<<"目标字符串:"<<str<<endl;
    
        system("pause");
        return 0;
    }
    =>string& insert (size_t pos, const char* s, size_t n);
      源字符串  :to be question
      目标字符串:to be not question

1.5 fill:string& insert (size_t pos, size_t n, char c);

  • 原型:string& insert (size_t pos, size_t n, char c);
  • 说明:在源字符串下标为pos处插入n个字符c。
  • 代码示例:

    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str="to be question";
    
        cout<<"string& insert (size_t pos, size_t n, char c);"<<endl;
        cout<<"源字符串  :"<<str<<endl;
        str.insert(6, 1, 'a');
        str.insert(7, 1, ' ');
        cout<<"目标字符串:"<<str<<endl;
    
        system("pause");
        return 0;
    }
    =>string& insert (size_t pos, size_t n, char c);
      源字符串  :to be question
      目标字符串:to be a question

1.6 fill:iterator insert (const_iterator p, size_t n, char c);

  • 原型:iterator insert (const_iterator p, size_t n, char c);
  • 说明:在源字符串的迭代器p处插入n个字符c,返回插入后迭代器的位置。
  • 代码示例:

    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str="to be question";
    
        cout<<"iterator insert (const_iterator p, size_t n, char c);"<<endl;
        cout<<"源字符串  :"<<str<<endl;
        str.insert(str.begin()+6, 1, 'a');
        str.insert(str.begin()+7, 1, ' ');
        cout<<"目标字符串:"<<str<<endl;
    
        system("pause");
        return 0;
    }
    =>iterator insert (const_iterator p, size_t n, char c);
      源字符串  :to be question
      目标字符串:to be a question

1.7 single character:iterator insert (const_iterator p, char c);

  • 原型:iterator insert (const_iterator p, char c);
  • 说明:在源字符串的迭代器p处插入字符c,返回插入后迭代器的位置。
  • 代码示例:

    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str="to be question";
    
        cout<<"iterator insert (const_iterator p, char c);"<<endl;
        cout<<"源字符串  :"<<str<<endl;
        str.insert(str.begin()+6, 'a');
        str.insert(str.begin()+7, ' ');
        cout<<"目标字符串:"<<str<<endl;
    
        system("pause");
        return 0;
    }
    =>iterator insert (const_iterator p, char c);
      源字符串  :to be question
      目标字符串:to be a question

1.8 range:template < class InputIterator>iterator insert (iterator p, InputIterator first, InputIterator last);

  • 原型:template < class InputIterator>iterator insert (iterator p, InputIterator first, InputIterator last);
  • 说明: Inserts a copy of the sequence of characters in the range [first,last), in the same order.
  • 代码示例:

    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str="to be question";
        string str3="or not to be";
        string::iterator it = str.begin()+6;
    
        cout<<"template < class InputIterator>iterator insert (iterator p, InputIterator first, InputIterator last);"<<endl;
        cout<<"源字符串  :"<<str<<endl;
        str.insert (it, str3.begin(), str3.end()); 
        cout<<"目标字符串:"<<str<<endl;
    
        system("pause");
        return 0;
    }
    =>template < class InputIterator>iterator insert (iterator p, InputIterator first, InputIterator last);
      源字符串  :to be question
      目标字符串:to be or not to bequestion

参考文献:
[1] 网络资源: http://www.cplusplus.com/reference/string/string/insert/

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值