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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值