讲解
词汇分割器库 -> 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;
}