To decide upon a perfect container, follow this strategy.
1.) firstly, since you realize that your ultimate goal is to sort file full of some strings (which you call keywords), you should definitely use a contiguous memory container like vector or a deque, since running sort on a linked container (list, map) will be painstakingly slow. But since, you take the data from a file, you can't initialize the size of your vector in advance !!! This may cause you to keep stuffing new elements in the vector (with push_back()) and internally the vector has to copy its elements with every relocation of the container. Although, you can circumvent this problem, by ensuring the capacity of the vector to reflect the expected size of your input, but lets just assume that you don't have any idea on the input size.
2.) Now, given this situation a simple but effective strategy will be to first read the file and store all the keywords in a linked structure like list.
- list<string> myList ;
then, you insert the elements in this list one by one at the end. The good thing now is that no amount of insertion(s) will cause a reallocation like in the case of a vector.
Now, once you're done reading all the elements, you declare a vector of strings and in it's constructor supply the iterator range => myList.begin() and myList.end().
- vector<string> myVect(myList.begin(), myList.end()) ; // assigns all elements in myList to myVect.
this will effectively make a vector with same elements that is in the list, but now all the elements are in a contiguous manner and applying sort() algorithm will work like a charm !!!
A further level of optimization can also be done in this strategy. Since, once you have copied all the elements from the list to the vector, you can let go of the list and free its memory. To achieve this use this code snippet, after you have copied myList to myVect.
myList.clear(); // erases all elements from list