【Boost】boost::string_algo详解5——erase相关函数

erase的主要函数(以及其包括的copy函数)包括:
erase_range, erase_first, erase_last, erase_nth, erase_head, erase_tail, erase_regex, erase_all, erase_all_regex
void test_string_erase()
{
	using namespace boost;
	std::string str = "Hello Dolly, Hello World!";

	std::cout << "#### erase_range ####" << std::endl;
	{
		std::string str1 = str;
		erase_range(str1, make_iterator_range(str1.begin() + 2, str1.end() - 2));
		std::cout << str1 << std::endl;
	}
	std::cout << std::endl;

	std::cout << "#### erase_first and erase_first_copy ####" << std::endl;
	{
		std::string str2 = str;
		erase_first(str2, "Hello");
		std::cout << str2 << std::endl;
	}
	{
		std::string str2 = str;
		std::string strc1 = erase_first_copy(str2, "Hello");
		std::cout << strc1 << std::endl;

		std::string strc2 = "result = ";
		erase_first_copy(back_inserter(strc2), str, "Hello");
		std::cout << strc2 << std::endl;
	}
	std::cout << std::endl;

	std::cout << "#### erase_last and erase_last_copy ####" << std::endl;
	{
		std::string str3 = str;
		erase_last(str3, "Hello");
		std::cout << str3 << std::endl;
	}
	{
		std::string str3 = str;
		std::string strc1 = erase_last_copy(str3, "Hello");
		std::cout << strc1 << std::endl;

		std::string strc2 = "result = ";
		erase_last_copy(back_inserter(strc2), str, "Hello");
		std::cout << strc2 << std::endl;
	}
	std::cout << std::endl;

	// 注意nth的索引从0开始.
	std::cout << "#### erase_nth and erase_nth_copy ####" << std::endl;
	{
		std::string str4 = str;
		erase_nth(str4, "Hello", 1);
		std::cout << str4 << std::endl;
	}
	{
		std::string str4 = str;
		std::string strc1 = erase_nth_copy(str4, "Hello", 1);
		std::cout << strc1 << std::endl;

		std::string strc2 = "result = ";
		erase_nth_copy(back_inserter(strc2), str, "Hello", 1);
		std::cout << strc2 << std::endl;
	}
	std::cout << std::endl;

	std::cout << "#### erase_all and erase_all_copy ####" << std::endl;
	{
		std::string str5 = str;
		erase_all(str5, "Hello");
		std::cout << str5 << std::endl;
	}
	{
		std::string str5 = str;
		std::string strc1 = erase_all_copy(str5, "Hello");
		std::cout << strc1 << std::endl;

		std::string strc2 = "result = ";
		erase_all_copy(back_inserter(strc2), str, "Hello");
		std::cout << strc2 << std::endl;
	}
	std::cout << std::endl;

	std::cout << "#### erase_head and erase_head_copy ####" << std::endl;
	{
		std::string str6 = str;
		erase_head(str6, 3);
		std::cout << str6 << std::endl;
	}
	{
		std::string str6 = str;
		std::string strc1 = erase_head_copy(str6, 3);
		std::cout << strc1 << std::endl;

 		std::string strc2 = "result = ";
 		erase_head_copy(back_inserter(strc2), str, 3);
		std::cout << strc2 << std::endl;
	}
	std::cout << std::endl;

	std::cout << "#### erase_tail and erase_tail_copy ####" << std::endl;
	{
		std::string str6 = str;
		erase_tail(str6, 3);
		std::cout << str6 << std::endl;
	}
	{
		std::string str6 = str;
		std::string strc1 = erase_tail_copy(str6, 3);
		std::cout << strc1 << std::endl;

		std::string strc2 = "result = ";
		erase_tail_copy(back_inserter(strc2), str, 3);
		std::cout << strc2 << std::endl;
	}
	std::cout << std::endl;

	std::cout << "#### erase_regex, erase_regex_copy ####" << std::endl;
	{
		std::string str6 = str;
		regex reg("H.*?o");

		erase_regex(str6, reg);
		std::cout << str6 << std::endl;
	}

	{
		std::string str6 = str;
		regex reg("H.*?o");
		std::string strc1 = erase_regex_copy(str6, reg);
		std::cout << strc1 << std::endl;

		std::string strc2 = "result = ";
		erase_regex_copy(back_inserter(strc2), str, reg);
		std::cout << strc2 << std::endl;
	}
	std::cout << std::endl;

	std::cout << "#### erase_all_regex, erase_all_regex_copy ####" << std::endl;
	{
		std::string str6 = str;
		regex reg("H.*?o");

		erase_all_regex(str6, reg);
		std::cout << str6 << std::endl;
	}
	{
		std::string str6 = str;
		regex reg("H.*?o");
		std::string strc1 = erase_all_regex_copy(str6, reg);
		std::cout << strc1 << std::endl;

		std::string strc2 = "result = ";
		erase_all_regex_copy(back_inserter(strc2), str, reg);
		std::cout << strc2 << std::endl;
	}
	std::cout << std::endl;
}





你可能感兴趣的:(【Boost】boost::string_algo详解5——erase相关函数)