I have a data structure question. I have a collection of strings which grows throughout the lifetime of a process. I want to be able to pass references to these strings around the program with varying durations. I don't want to add duplicates to the collection, so when I pass one in I want to be returned a reference to the existing entry, thus:
const std::string& add_new_entry(const std::string&)
{
// Check if string exists
// Return reference if it does
// Otherwise add to collection
// Return reference to it
}
The most naive implementation would be a list of strings and a call to std::find
every time, but I can't help feeling that's deeply suboptimal, particularly since I'm dealing with upwards of 50,000 strings. I've created an extending array container so I can add elements arbitrarily without forcing a resize and move, and I'm indexing them with a std::set
of std::string*
ordered alphabetically using a dereferencing comparison predicate: can anyone do any better? 15 string comparisons seems like a lot.