c++ - 将字符串拆分为标记 - 没有操作系统特定的功能

标签 c++ c string tokenize

<分区>

Possible Duplicate:
How do I tokenize a string in C++?

strtok 函数不是线程安全的。 Microsoft 具有特定于 Windows 的 strtok_s 和 CString::Tokenize 安全函数。是否有跨平台的 CRT/C++ 库方法无需手动编码即可执行此操作?

最佳答案

boost::splithttp://www.boost.org/doc/libs/1_51_0/doc/html/string_algo/reference.html#header.boost.algorithm.string.split_hpp

使用示例

#include <vector>
#include <string>
#include <boost/algorithm/string.hpp>

int main()
{
   const std::string s = "hello and what";
   std::vector<std::string> v;
   boost::split(v, s, [](char c) { return c == ' '; }, boost::token_compress_on);
   for (const auto& str : v)
   {
      std::cout << str << std::endl;
   }
}

http://liveworkspace.org/code/3dfc9ee9c8497741f9976ac41a14a390

或者使用boost::tokenizer

关于c++ - 将字符串拆分为标记 - 没有操作系统特定的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12067434/

相关文章:

c++ - std::find 在遗留数组上

c - C中的Segmentation Fault 11 由较大的操作数引起

c# - 句子的英语到 PigLatin 转换

C++:如何直接写入文件(无 ASCII)?

c++ - 用于分析多个数据结构的 massif-visualizer

c++ - 如何为qt构建mysql驱动程序?

python - 通过 c (debian) 在终端中运行 python 模块

c - clang 选项的别名?

string - 测试字符串中的值

java - 从一个文件中获取整数列表