Chinaunix首页 | 论坛 | 博客
  • 博客访问: 338661
  • 博文数量: 88
  • 博客积分: 1695
  • 博客等级: 上尉
  • 技术积分: 1380
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-06 15:48
个人简介

喜欢美食, 旅行..

文章分类

全部博文(88)

文章存档

2014年(2)

2013年(12)

2012年(14)

2010年(8)

2009年(52)

我的朋友

分类: C/C++

2012-12-16 22:55:17

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

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

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