Using vectors

Can someone please explain me how I can use vectors to count how many times each word appears in an input?
For example:

Input:
I write blah blah

Output:
"I" = 1, "write" = 1, "blah" = 2.
First, you'll want to find each word somehow. If you search for spaces, that shouldn't be too hard.

Then, you'll need to keep track of two things, requiring two vectors (or one vector containing std::pairs, but for simplicity, the two vectors solution only will be discussed here):
1. The words that were found, in this case "I", "write", and "blah".
2. The number of each word.

When you find a word, search for it in your first vector. If it appears, increment the value of the element in the same position on the other vector. Else, add an element representing your word to your first vector, and an element with the value "1" to the second vector.

BIG NOTE: I would suggest the use of std::maps instead of either of the above

-Albatross
Last edited on
Topic archived. No new replies allowed.