My guess is to use vector
, because the contiguous storage is good for performance, reserve an expected amount of entries beforehand, take advantage of move semantics and sort the vector at the end of the file only:
std::vector<std::string> m_words;
m_words.reserve(10000);
std::string buf;
while (ss >> buf)
{
m_words.push_back(std::move(buf));
buf.clear();
}
std::sort(m_words.begin(), m_words.end());
(You can also check the file size in a preprocessing phase to get a better hint instead of a fixed one)
Because of moving, the buf must be reset to an empty state.
Unless otherwise specified, all standard library functions that accept
rvalue reference parameters (such as std::vector::push_back) are
guaranteed to leave the moved-from argument in valid but unspecified
state. That is, only the functions without preconditions, such as the
assignment operator, may be used on the object after it was moved into
a standard library container (cppreference)