c++ - 将 std::string 标记化到键值映射中

标签 c++ string c++11 tokenize

在 C 中,分隔符可以用 Null 替换,并且 char* -> char* 的映射具有比较功能。

我正在尝试找出在现代 C++ 中执行此操作的最快方法。这个想法是为了避免在 map 中复制字符。

std::string sample_string("name=alpha;title=something;job=nothing");

std::map<std::string,std::string> sample_map;

不复制字符。

丢失原始输入字符串是可以的。

最佳答案

两个 std::strings 不能指向相同的底层字节,所以不可以用字符串来做。

为了避免处理字节,你可以使用迭代器:

struct Slice {
  string::iterator begin, end;
  bool operator < (const& Slice that) const {
    return lexicographical_compare(begin, end, that.begin, that.end);
  }
};

std::map<Slice,Slice> sample_map;

请注意,如果您修改原始字符串,所有迭代器都将无效。

关于c++ - 将 std::string 标记化到键值映射中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39276796/

相关文章:

c# - String.Compare 答案随着字符添加到字符串末尾而变化

c++ - 使用 boost::async w/o c++11

c++ - 具有未指定操作数的C++计算器

c++ - RE2 嵌套正则表达式小组赛

c++ - 创建一个 array2D 类在编译时崩溃

c++ - 告诉 std::thread 在满足条件时终止/停止自身

android - _GLIBCXX_USE_C99 未定义,to_wstring 不可用

c++ - 讨厌的 unicode 和 C++ : Easy way to read ASCII/UTF-8/UTF-16 BE/LE text file

c++ - 当前日期和时间作为字符串

c - 在多行字符串中循环遍历行的好方法是什么?