将std::string就地标记为键值映射
In place Tokenization of std::string into a map of Key Value
在C语言中,分隔符可以用null替换,并且可以使用带有比较函数的char* -> char*映射。
我正试图找出最快的方法在现代c++中做到这一点。这样做是为了避免在地图中复制字符。
std::string sample_string("name=alpha;title=something;job=nothing");
std::map<std::string,std::string> sample_map;
不复制字符
可以丢失原始输入字符串
两个std::字符串不能指向相同的底层字节,所以不可能使用字符串。
要避免复制字节,可以使用迭代器:
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;
并且要注意,如果修改了原始字符串,所有的迭代器都将无效。
相关文章:
- 使用一个考虑到std::map中键值的滚动或换行的键
- 为左值和右值的包装器实现C++范围
- 如何使用 std::variant 打印地图键/值?
- 将类指针类型转换为键时出错
- 当键值是 std 向量时,为什么使用 at in C++ 访问映射值如此缓慢?
- C++ 映射的键/值的用户自定义名称?
- 将 Qt 属性枚举值打印为键字符串
- 计算向量的std ::映射为键的值,并将双倍作为值
- 使用 k 个键值对为零的存储桶初始化 c++14 unordered_map
- 创建用于将结构坐标存储为键和线号作为值的映射
- 将字符数组转换为 ASCII 键值的快速方法
- boost无序映射键值为0
- 如何从以对为键的映射中检索值
- 将C++键/值结构转换为数据库(sqlite3)
- 我可以将键值对的内存映射文件重新解释为映射以便对它们进行排序吗?
- 如何将值输入到以结构为键的映射中
- 将十六进制的键值转换为十进制
- 将std::string就地标记为键值映射
- 使用C++将字符串拆分为键值对
- 以字符为键的数组,默认值为0