首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

【Boost】boost:string_algo详解六——replace相关函数

2012-12-29 
【Boost】boost::string_algo详解6——replace相关函数replace的主要函数(以及其包括的copy函数)包括:replace_

【Boost】boost::string_algo详解6——replace相关函数
replace的主要函数(以及其包括的copy函数)包括:
replace_range, replace_first, replace_last, replace_nth, replace_head, replace_tail, replace_regex, replace_all, replace_all_regex

void test_string_replace(){using namespace boost;std::string str = "Hello Dolly, Hello World!";std::cout << "#### replace_range ####" << std::endl;{std::string str1 = str;replace_range(str1, make_iterator_range(str1.begin() + 2, str1.end() - 2), "!!!");std::cout << str1 << std::endl;}std::cout << std::endl;std::cout << "#### replace_first and replace_first_copy ####" << std::endl;{std::string str2 = str;replace_first(str2, "Hello", "GoodBye");std::cout << str2 << std::endl;}{std::string str2 = str;std::string strc1 = replace_first_copy(str2, "Hello", "GoodBye");std::cout << strc1 << std::endl;std::string strc2 = "result = ";replace_first_copy(back_inserter(strc2), str, "Hello", "GoodBye");std::cout << strc2 << std::endl;}std::cout << std::endl;std::cout << "#### replace_last and replace_last_copy ####" << std::endl;{std::string str3 = str;replace_last(str3, "Hello", "GoodBye");std::cout << str3 << std::endl;}{std::string str3 = str;std::string strc1 = replace_last_copy(str3, "Hello", "GoodBye");std::cout << strc1 << std::endl;std::string strc2 = "result = ";replace_last_copy(back_inserter(strc2), str, "Hello", "GoodBye");std::cout << strc2 << std::endl;}std::cout << std::endl;// 注意nth的索引从0开始.std::cout << "#### replace_nth and replace_nth_copy ####" << std::endl;{std::string str4 = str;replace_nth(str4, "Hello", 1, "GoodBye");std::cout << str4 << std::endl;}{std::string str4 = str;std::string strc1 = replace_nth_copy(str4, "Hello", 1, "GoodBye");std::cout << strc1 << std::endl;std::string strc2 = "result = ";replace_nth_copy(back_inserter(strc2), str, "Hello", 1, "GoodBye");std::cout << strc2 << std::endl;}std::cout << std::endl;std::cout << "#### replace_all and replace_all_copy ####" << std::endl;{std::string str5 = str;replace_all(str5, "Hello", "GoodBye");std::cout << str5 << std::endl;}{std::string str5 = str;std::string strc1 = replace_all_copy(str5, "Hello", "GoodBye");std::cout << strc1 << std::endl;std::string strc2 = "result = ";replace_all_copy(back_inserter(strc2), str, "Hello", "GoodBye");std::cout << strc2 << std::endl;}std::cout << std::endl;std::cout << "#### replace_head and replace_head_copy ####" << std::endl;{std::string str6 = str;replace_head(str6, 5, "GoodBye");std::cout << str6 << std::endl;}{std::string str6 = str;std::string strc1 = replace_head_copy(str6, 5, "GoodBye");std::cout << strc1 << std::endl;std::string strc2 = "result = ";replace_head_copy(back_inserter(strc2), str, 5, "GoodBye");std::cout << strc2 << std::endl;}std::cout << std::endl;std::cout << "#### replace_tail and replace_tail_copy ####" << std::endl;{std::string str6 = str;replace_tail(str6, 6, "GoodBye");std::cout << str6 << std::endl;}{std::string str6 = str;std::string strc1 = replace_tail_copy(str6, 6, "GoodBye");std::cout << strc1 << std::endl;std::string strc2 = "result = ";replace_tail_copy(back_inserter(strc2), str, 6, "GoodBye");std::cout << strc2 << std::endl;}std::cout << std::endl;std::cout << "#### replace_regex, replace_regex_copy ####" << std::endl;{std::string str6 = str;regex reg("H.*?o");//replace_regex(str6, reg, "GoodBye");// 直接这么写会报错. 要用如下的方法.std::string strReplace = "GoodBye";replace_regex(str6, reg, strReplace);std::cout << str6 << std::endl;}{std::string str6 = str;regex reg("H.*?o");std::string strReplace = "GoodBye";std::string strc1 = replace_regex_copy(str6, reg, strReplace);std::cout << strc1 << std::endl;std::string strc2 = "result = ";replace_regex_copy(back_inserter(strc2), str, reg, strReplace);std::cout << strc2 << std::endl;}std::cout << std::endl;std::cout << "#### replace_all_regex, replace_all_regex_copy ####" << std::endl;{std::string str6 = str;regex reg("H.*?o");std::string strReplace = "GoodBye";replace_all_regex(str6, reg, strReplace);std::cout << str6 << std::endl;}{std::string str6 = str;regex reg("H.*?o");std::string strReplace = "GoodBye";std::string strc1 = replace_all_regex_copy(str6, reg, strReplace);std::cout << strc1 << std::endl;std::string strc2 = "result = ";replace_all_regex_copy(back_inserter(strc2), str, reg, strReplace);std::cout << strc2 << std::endl;}std::cout << std::endl;}




热点排行