在 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/