one.eye
one.eye

Reputation: 133

In place Tokenization of std::string into a map of Key Value

In C the delimiters can be replaced by Nulls and a map of char* -> char* with a comparison function would work.

I am trying to figure out the fastest possible way to do this in Modern C++ . The idea is to avoid Copying characters in the Map.

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

to

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

Without copying characters.

It's ok to lose original input string.

Upvotes: 0

Views: 297

Answers (1)

Jeffrey Rennie
Jeffrey Rennie

Reputation: 3443

Two std::strings cannot point to the same underlying bytes, so no it's not possible to do with strings.

To avoid coping bytes, you could to use iterators:

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;

And beware that if you modify the original string, all the iterators will be invalid.

Upvotes: 1

Related Questions