Table 11.5. Erase/Replace

Algorithm name Description Functions
replace/erase_first Replace/Erase the first occurrence of a string in the input replace_first()
replace_first_copy()
ireplace_first()
ireplace_first_copy()
erase_first()
erase_first_copy()
ierase_first()
ierase_first_copy()
replace/erase_last Replace/Erase the last occurrence of a string in the input replace_last()
replace_last_copy()
ireplace_last()
ireplace_last_copy()
erase_last()
erase_last_copy()
ierase_last()
ierase_last_copy()
replace/erase_nth Replace/Erase the nth (zero-indexed) occurrence of a string in the input replace_nth()
replace_nth_copy()
ireplace_nth()
ireplace_nth_copy()
erase_nth()
erase_nth_copy()
ierase_nth()
ierase_nth_copy()
replace/erase_all Replace/Erase the all occurrences of a string in the input replace_all()
replace_all_copy()
ireplace_all()
ireplace_all_copy()
erase_all()
erase_all_copy()
ierase_all()
ierase_all_copy()
replace/erase_head Replace/Erase the head of the input replace_head()
replace_head_copy()
erase_head()
erase_head_copy()
replace/erase_tail Replace/Erase the tail of the input replace_tail()
replace_tail_copy()
erase_tail()
erase_tail_copy()
replace/erase_regex Replace/Erase a substring matching the given regular expression replace_regex()
replace_regex_copy()
erase_regex()
erase_regex_copy()
replace/erase_regex_all Replace/Erase all substrings matching the given regular expression replace_all_regex()
replace_all_regex_copy()
erase_all_regex()
erase_all_regex_copy()
find_format Generic replace algorithm find_format()
find_format_copy()
find_format_all()
find_format_all_copy()()
http://wuerping.cnblogs.com/
示例


    1     // replace/erase_first

    2     /*

    3     template<typename SequenceT, typename Range1T, typename Range2T>

    4     inline void replace_first(

    5             SequenceT& Input, // 输入字符串

    6             const Range1T& Search, // 被搜索的子串

    7             const Range2T& Format  // 用来替换的字符串

    8         )

    9     */

   10     string str("AfirstBfirstCfirstD");

   11 

   12     // replace_first, replace_first_copy, ireplace_first, ireplace_first_copy

   13     assert("ArrrrBfirstCfirstD" == replace_first_copy(str, "first", "rrrr"));

   14     replace_first(str, "first", "rrrr");

   15     assert("ArrrrBfirstCfirstD" == str);

   16     assert("ArrrrBiiiiCfirstD" == ireplace_first_copy(str, "FIRst", "iiii"));

   17     ireplace_first(str, "FIRst", "iiii");

   18     assert("ArrrrBiiiiCfirstD" == str);

   19 

   20     // erase_first, erase_first_copy, ierase_first, ierase_first_copy

   21     assert("ABiiiiCfirstD" == erase_first_copy(str, "rrrr"));

   22     erase_first(str, "rrrr");

   23     assert("ABiiiiCfirstD" == str);

   24     assert("ABiiiiCD" == ierase_first_copy(str, "FIRST"));

   25     ierase_first(str, "FIRST");

   26     assert("ABiiiiCD" == str);

   27 

   28 

   29     // replace/erase_last

   30     // 与 replace/erase_first 只是方向相反

   31 

   32 

   33     // replace/erase_nth

   34     /*

   35     template<typename SequenceT, typename Range1T, typename Range2T>

   36     inline void replace_nth(

   37             SequenceT& Input, // 输入字符串

   38             const Range1T& Search, // 被搜索的子串

   39             unsigned int Nth, // 第N个打算被匹配的子串(下标从0开始)

   40             const Range2T& Format // 用来替换的字符串

   41             )

   42 

   43     */

   44     // replace_nth_copy, replace_nth, ireplace_nth_copy, ireplace_nth

   45     str = "0search1search2search3";

   46     assert("0search1search2OK3" == replace_nth_copy(str, "search", 2, "OK"));

   47     replace_nth(str, "search", 2, "OK");

   48     assert("0search1search2OK3" == str);

   49     assert("0search1OK2OK3" == ireplace_nth_copy(str, "SEARCH", 1, "OK"));

   50     ireplace_nth(str, "SEARCH", 1, "OK");

   51     assert("0search1OK2OK3" == str);

   52     // erase_nth_copy, erase_nth, ierase_nth_copy, ierase_nth

   53     str = "0search1search2search3";

   54     assert("0search1search23" == erase_nth_copy(str, "search", 2));

   55     erase_nth(str, "search", 2);

   56     assert("0search1search23" == str);

   57     assert("0search123" == ierase_nth_copy(str, "SEARCH", 1));

   58     ierase_nth(str, "SEARCH", 1);

   59     assert("0search123" == str);

   60 

   61     // replace/erase_all

   62     // replace_all_copy, replace_all, ireplace_all_copy, ireplace_all

   63     str = "0search1search2search3";

   64     assert("0ALL1ALL2ALL3" == replace_all_copy(str, "search", "ALL"));

   65     replace_all(str, "search", "ALL");

   66     assert("0ALL1ALL2ALL3" == str);

   67     assert("0iall1iall2iall3" == ireplace_all_copy(str, "aLL", "iall"));

   68     ireplace_all(str, "aLL", "iall");

   69     assert("0iall1iall2iall3" == str);

   70     // erase_all_copy(), erase_all(), ierase_all_copy(), ierase_all()

   71     str = "0search1search2search3";

   72     assert("0123" == erase_all_copy(str, "search"));

   73     erase_all(str, "search");

   74     assert("0123" == str);

   75     str = "0search1search2search3";

   76     assert("0123" == ierase_all_copy(str, "SEarch"));

   77     ierase_all(str, "seaRCH");

   78     assert("0123" == str);

   79 

   80     // replace/erase_head

   81     /*

   82     template<typename SequenceT, typename RangeT>

   83         inline SequenceT replace_head_copy(

   84             const SequenceT& Input, // 输入字符串

   85             unsigned int N,    // 字符串头的长度

   86             const RangeT& Format // // 用来替换的字符串

   87             )

   88     */

   89     // replace_head_copy(), replace_head(), erase_head_copy(), erase_head()

   90     str = "123456*654321";

   91     assert("head*654321" == replace_head_copy(str, 6, "head"));

   92     replace_head(str, 6, "head");

   93     assert("head*654321" == str);

   94     str = "123456*654321";

   95     assert("*654321" == erase_head_copy(str, 6));

   96     erase_head(str, 6);

   97     assert("*654321" == str);

   98 

   99     // replace/erase_tail

  100     // 与 replace/erase_head 只是方向相反

  101 

  102     // replace/erase_regex 替换一个子串

  103     // replace_regex_copy, replace_regex, erase_regex_copy, erase_regex

  104     str = "*12345*12345678*ABCDE";

  105     boost::regex rx("[0-9]+");

  106     assert("*rx*12345678*ABCDE" == replace_regex_copy(str, rx, string("rx")));

  107     replace_regex(str, rx, string("rx"));

  108     assert("*rx*12345678*ABCDE" == str);

  109     str = "*12345*12345678*ABCDE";

  110     assert("**12345678*ABCDE" == erase_regex_copy(str, rx));

  111     erase_regex(str, rx);

  112     assert("**12345678*ABCDE" == str);

  113 

  114     // replace/erase_regex_all

  115     // replace_all_regex_copy, replace_all_regex, erase_all_regex_copy, erase_all_regex

  116     str = "*12345*12345678*ABCDE";

  117     assert("*rx*rx*ABCDE" == replace_all_regex_copy(str, rx, string("rx")));

  118     replace_all_regex(str, rx, string("rx"));

  119     assert("*rx*rx*ABCDE" == str);

  120     str = "*12345*12345678*ABCDE";

  121     assert("***ABCDE" == erase_all_regex_copy(str, rx));

  122     erase_all_regex(str, rx);

  123     assert("***ABCDE" == str);

  124 

  125     // find_format

  126     // find_format_copy(), find_format(), find_format_all_copy(), find_format_all()

  127     str = "*abcde*abcde";

  128     assert("*ABCDE*abcde" == find_format_copy(str, first_finder("abcde", is_iequal()), upcase_formatter));

  129     find_format(str, first_finder("abcde", is_iequal()), upcase_formatter);

  130     assert("*ABCDE*abcde" == str);

  131     str = "*abcde*abcde";

  132     assert("*ABCDE*ABCDE" == find_format_all_copy(str, first_finder("abcde", is_iequal()), upcase_formatter));

  133     find_format_all(str, first_finder("abcde", is_iequal()), upcase_formatter);

  134     assert("*ABCDE*ABCDE" == str);


  ---------------------------------------------------------------------------------------------
   

    1     inline string upcase_formatter(const iterator_range<string::const_iterator>& Replace )

    2     {

    3         string Temp(Replace.begin(), Replace.end());

    4         to_upper(Temp);

    5         return Temp;

    6     }