Storing words from an input stream into a vector I'm extremely new to C++ and am doing the exercises on the book Accelerated C++. Here is one of the exercises:


  4-5. Write a function that reads words from an input stream and stores
  them in a vector. Use that function both to write programs that count
  the number of words in the input, and to count how many times each
  word occurred.


How can I make this program better by only using vectors?

#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;vector&gt;
#include&lt;string&gt;
#include &lt;stdexcept&gt;
using std::cout; using std::cin; using std::vector; using std::sort; using std::string; using std::endl; using std::domain_error; using std::cerr;

//it said "count the number of words in the input, and to count how many times each word occurred". I cannot think of any return value if a function do two things, so I used void.
void run(const vector&lt;string&gt;&amp; v) {
    if (v.size() == 0) throw domain_error("Nothing to count");
    cout &lt;&lt; "words count: " &lt;&lt; v.size() &lt;&lt; endl;
    string unique = v[0];
    int count = 1;
    for (vector&lt;string&gt;::size_type i = 1; i &lt; v.size(); ++i) {
        if (v[i-1] == v[i]) {
            count++;
        } else {
            cout &lt;&lt; unique &lt;&lt; ": " &lt;&lt; count &lt;&lt; endl;
            unique = v[i];
            count = 1;
        }
    }
    cout &lt;&lt; unique &lt;&lt; ": " &lt;&lt; count &lt;&lt; endl;
}

int main() {
    cout &lt;&lt; "Enter all words followed by end of file." &lt;&lt; endl;
    vector&lt;string&gt; words;
    string word;
    while (cin &gt;&gt; word) {
        words.push_back(word);
    }
    sort(words.begin(), words.end());
    try {
        run(words);
    } catch (domain_error e) {
        std::cerr &lt;&lt; e.what() &lt;&lt; endl;
    }
    return 0;
}
Minor inconsistency here:


#include&lt;string&gt;



Add a space in between them, like all the others:

#include &lt;string&gt;

Just get rid of this:


using std::cout; using std::cin; using std::vector; using std::sort; using std::string; using std::endl; using std::domain_error; using std::cerr;



Even if you put them onto separate lines, it'll still be lengthy.  Just put std:: where necessary and try to keep it consistent.
Exception objects usually should not be passed by value:


catch (domain_error e)



They should be passed by const&amp;:

catch (domain_error const&amp; e)

Consider separating this into additional lines:


if (v.size() == 0) throw domain_error("Nothing to count");



While it's okay to put short statements on a single line, it may be a little harder to read and maintain longer lines, also with the lack of curly braces.

In addition, just call empty() instead of comparing size() to 0.

if (v.empty())
{
    throw domain_error("Nothing to count");
}

Instead of getting the first element via []:


string unique = v[0];



use front():

string unique = v.front();



I'd also like to add a compliment:

Thank you for using std::size_type instead of int.  I hardly see that done by others.  It's good to see that you're paying attention to return types and compiler mismatch warnings.