Say I had a program like the following:
and I knew that there was going to be at least ten words entered. Would it be better to call words.reserve() to reserve that space and eliminate the need to allocate more memory each time push_back() is called? I know it costs performance when the entire vector has to be copied to a new location, but if it is just allocating more memory (without copying) does it make a difference?Code:#include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; int main() { vector<string> words; vector<string>::iterator it; string temp; cout << "Enter a series of words separated by spaces and terminated with [Enter]:" << endl; do { cin >> temp; words.push_back(temp); } while (cin.get() != '\n'); sort(words.begin(), words.end()); cout << "\n\nYou entered (sorted): "; for (it = words.begin(); it != words.end(); it++) { cout << *it << " "; } cin.get(); return 0; }