I need a "string pool" object into which I can repeatedly insert a "sequence of chars" (I use this phrase to mean "string" without confusing it with std::string or a C string), obtain a pointer to the sequence, and be guaranteed that the pointer will not become invalidated if/when the pool needs to grow. Using a simple std::string
as the pool won't work, because of the possibility for the string to be reallocated when it outgrows its initial capacity, thus invalidating all previous pointers into it.
The pool will not grow without bound -- there are well-defined points at which I will call a clear()
method on it -- but I don't want to reserve any maximum capacity on it, either. It should be able to grow, without moving.
One possibility I'm considering is inserting each new sequence of chars into a forward_list<string>
and obtaining begin()->c_str()
. Another is inserting into an unordered_set<string>
, but I'm having a hard time finding out what happens when an unordered_set has to grow. The third possibility I'm considering (less enthusiastically) is rolling my own chain of 1K buffers into which I concatenate the sequence of chars. That has the advantage (I guess) of having the highest performance, which is a requirement for this project.
I'd be interested in hearing how others would recommend approaching this.
UPDATE 1: edited to clarify my use of the phrase "sequence of chars" to be equivalent to the general notion of a "string" without implying either std::string or null-terminated char array.
my_unordered_set.insert("foo")
return an iterator that would remain valid even after "rehashing"? – Guffeystd::deque
satisfy the requirements? (If you only add to the beginning or end) – Azazelmy_unordered_set.insert("foo")
and expect the returned iterator to remain valid over time.) Maybe I'm missing your point. – GuffeyT * p = &*it
, etc. – Scrivingsit
points to an entry in a vector: are you saying thatp
remains valid even after appending thousands more entries onto the vector? (Presuming that at some point the vector needs to be grown) – Guffeystd::list
and all the associative standard library containers. Incidentally,std::deque
also preserves references, but not iterators, for insertions at the ends. – Scrivings