1. 程式人生 > >Boost(五)——字串處理(三):詞彙分割操作

Boost(五)——字串處理(三):詞彙分割操作

講解

詞彙分割器庫 -> Boost.Tokenizer

可以在指定某個字元為分隔符後,遍歷字串的部分表示式。

字元分割:

boost::char_separator<char(或者wchar_t)>

#include <iostream> 
#include <boost/tokenizer.hpp> 
#include <string> 
#include <iostream> 
using namespace std;
int main()
{
	typedef boost::tokenizer<boost::char_separator<char>> token;
	string s = "Boost C++ libraries";
	token t(s);
	for (token::iterator it = t.begin(); it != t.end(); it++)//迭代器遍歷
	{
		cout << *it << endl;
	}
	system("pause");
#include <iostream> 
#include <boost/tokenizer.hpp> 
#include <string> 
#include <iostream> 
using namespace std;
int main()
{
	typedef boost::tokenizer<boost::char_separator<wchar_t>, std::wstring::const_iterator, std::wstring> tokenizer;
	std::wstring s = L"Boost C++ libraries";
	boost::char_separator<wchar_t> sep(L" ");
	tokenizer tok(s, sep);
	for (tokenizer::iterator it = tok.begin(); it != tok.end(); ++it)
		std::wcout << *it << std::endl;
	system("pause");
}

1、 輸出

Boost    

C    

+    

+    

libraries

boost::char_separator<char> sep;//預設空格隱藏,顯示標點("+"也是標點)

 2、輸出

Boost    

C++    

libraries

boost::char_separator<char> sep(" ");//" "空格作為分隔符

3、 輸出

Boost  

C    

+    

+    

libraries(和1一樣)

boost::char_separator<char> sep(" ","+");//“+”顯示

 4、輸出

Boost    

C    

+  

空格   

+    

空格  

libraries

//"+"後加上了空格(顯式)

boost::char_separator<char> sep(" ", "+", boost::keep_empty_tokens); 

逗號分割:

boost_escaped_list_separator<char>

#include <boost/algorithm/string.hpp> 
#include <locale> 
#include <iostream> 
#include <boost/tokenizer.hpp> 
#include <string> 
#include <iostream> 
using namespace std;
int main()
{
	typedef boost::tokenizer<boost::escaped_list_separator<char> > tokenizer;
	std::string s = "Boost,\"C++ libraries\"";
	tokenizer tok(s);
	for (tokenizer::iterator it = tok.begin(); it != tok.end(); ++it)
		std::cout << *it << std::endl;
	system("pause");
}

輸出結果:

Boost    

C++    libraries

//預設","作為分隔符,其他標點符號隱藏,但空格顯示。

偏移分割

boost::offset_separator

#include <boost/algorithm/string.hpp> 
#include <locale> 
#include <iostream> 
#include <boost/tokenizer.hpp> 
#include <string> 
#include <iostream> 
using namespace std;
int main()
{
	typedef boost::tokenizer<boost::offset_separator> tokenizer;
	std::string s = "Boost C++ libraries";
	int offsets[] = { 5, 5, 9 };
	boost::offset_separator sep(offsets, offsets + 3);//指定在第三個字串結束(開始是第一個)
	tokenizer tok(s, sep);
	for (tokenizer::iterator it = tok.begin(); it != tok.end(); ++it)
		std::cout << *it << std::endl;
}