[英]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 :: 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;
并且请注意,如果您修改原始字符串,则所有迭代器都将无效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.