• string::replace


    string (1)
    string& replace (size_t pos,        size_t len,        const string& str);
    string& replace (const_iterator i1, const_iterator i2, const string& str);
    
    substring (2)
    string& replace (size_t pos,        size_t len,        const string& str,
                     size_t subpos, size_t sublen);
    
    c-string (3)
    string& replace (size_t pos,        size_t len,        const char* s);
    string& replace (const_iterator i1, const_iterator i2, const char* s);
    
    buffer (4)
    string& replace (size_t pos,        size_t len,        const char* s, size_t n);
    string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);
    
    fill (5)
    string& replace (size_t pos,        size_t len,        size_t n, char c);
    string& replace (const_iterator i1, const_iterator i2, size_t n, char c);
    
    range (6)
    template <class InputIterator>
      string& replace (const_iterator i1, const_iterator i2,
                       InputIterator first, InputIterator last);
    
    initializer list (7)
    string& replace (const_iterator i1, const_iterator i2, initializer_list<char> il);

    #include <iostream>
    #include<string.h>

    using namespace std;
    int main()
    {
    string s1 = "i love zhh";
    string s2 = "lyy";
    string s3 = "zhh";
    const char *s4 = "love";
    const char *s5 = "like";
    const char *s6 = "enjoyooo";
    string s7 = "00be fond of11";
    s1.replace(7, 4, s2);
    cout << s1 << endl;
    s1.replace(7, 4, s3);
    cout << s1 << endl;
    try{
    s1.replace(20, 1, s3);
    }catch(out_of_range){
    cout << "out_of_range" << endl;
    }
    cout << "2" << s1 << endl;
    s1.replace(s1.begin() + 7, s1.end(), s2);
    cout << s1 << endl;

    s1.replace(2, 4, s3, 0, 4);
    cout << s1 << endl;
    try{
    s1.replace(22, 4, s3, 0, 4);
    }catch(out_of_range){

    cout << "out_of_range" << endl;
    }
    cout << s1 << endl;

    s1.replace(2, 3, s4);
    cout << s1 << endl;

    s1.replace(s1.begin() + 2, s1.begin() + 6, s5);
    cout << s1 << endl;

    s1.replace(2, 4, s6, 5);
    cout << s1 << endl;

    s1.replace(s1.begin() + 2, s1.begin() + 7, s5, 4);
    cout << s1 << endl;

    s1.replace(2, 4, 5, '$');
    cout << s1 << endl;

    s1.replace(s1.begin() + 2, s1.begin() + 7, 4, '@');
    cout << s1 << endl;

    s1.replace(s1.begin() + 2, s1.begin() + 6, s7.begin() + 2, s7.end() -2);
    cout << s1 << endl;

    initializer_list<char> s8 = {'l', 'o', 'v', 'e'};//interesting  !!!
    s1.replace(s1.begin() + 2, s1.end() - 4, s8);
    cout << s1 << endl;

    return 0

    }

  • 相关阅读:
    WinForm事件中的Object sender和EventArgs e参数
    Day 25:Python 模块 collections 3 个常用类
    Day 23:Python 中的一些高频面试题及解答小记
    Day 22:Python 迭代器和生成器小记
    Day 21:Python 多线程和协程
    Day 20:python中几个常用的内置函数
    Day 19:Python 函数五类参数
    遍历容器auto方法
    JS鼠标提示框效果
    数据库(二)
  • 原文地址:https://www.cnblogs.com/xpylovely/p/12071749.html
Copyright © 2020-2023  润新知