Chinaunix首页 | 论坛 | 博客
  • 博客访问: 665377
  • 博文数量: 209
  • 博客积分: 26
  • 博客等级: 民兵
  • 技术积分: 326
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-21 09:29
文章分类

全部博文(209)

文章存档

2015年(6)

2014年(40)

2013年(154)

2012年(11)

我的朋友

分类:

2012-12-17 10:48:45

这篇博文只是为了给自己留个笔记而已.

1.boost::tokenizer 的用法.
当一个字符串使用某个分隔符的时候, 想快速的拿到分割后的字串, 可以使用.
  1. #include <boost/tokenizer.hpp>
  2. int main()
  3. {
  4.     std::string teststr = "14,24,rt| &44,56";
  5.     boost::char_separator<char> separator(",");
  6.     boost::tokenizer<boost::char_separator<char> > tokens(teststr, separator);

  7.     std::vector<std::string> splits;
  8.     boost::tokenizer<boost::char_separator<char> >::iterator token_iter;
  9.     for (token_iter = tokens.begin(); token_iter != tokens.end(); token_iter++)
  10.         splits.push_back(*token_iter);

  11.     return 0;
  12. }
2.boost::split 的用法.
可以使用多个分隔符, 下面的示例代码就是用 | & \ 这三个字幅作为分隔符,任何一个都OK.
缺点是在有的编译器会出现讨厌的告警, 不过最新版本的boost库使用的底层算法应该没有这个问题, 我没有验证, 猜测而已.

  1. #include <boost/algorithm/string/classification.hpp>
  2. #include <boost\algorithm\string\split.hpp>
  3. int main()
  4. {
  5.     std::vector<std::wstring> split;
  6.     std::wstring ext_path(L"\\Device HarddiskVolume1|Windows&&System32 Drivers\\tdi.sys");
  7.     boost::split(split, ext_path, boost::is_any_of(L"|& \\"), boost::algorithm::token_compress_on);

  8.     return 0;
  9. }

阅读(1059) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~