I modified some code I found on Wikipedia concerning maps to see if it would work before applying it to a project I'm working on that has a similar idea.
What I would want is for a user to be able to enter sentences and then be able to distinguish how many times a the person entered a word in a particular sentence. This is the code I came up with:
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
int main()
{
map<string, vector<int> > wordcounts;
string s;
vector<int> sentence;
while (cin >> s && s != "end")
{
for(int i = 0; i < 5; i++)
{ sentence;
while (cin >> s && s != "/n")
wordcounts++;
}
}
while (cin >> s && s != "end")
{
for(int i = 0; i < 5; i++)
{ sentence;
while (cin >> s && s != "/n")
cout << s << ' ' << wordcounts << '\n';
}
}
}
It compiled just fine, but I got the following error message at runtime after entering my first string:
I just learned about vectors today; I tried using an array to do this at first but that failed miserably... someone suggested to use a vector. Needless to say, I'm a bit clueless about this.
Here's some more information on what I'm actually trying to do just in case you think there might be a better way of doing this:
I need to count how many times each word appears in each sentence in a document (capitalization does not matter, punctuation is disregarded, and each sentence occupies its own line). Let's say my document contained the following:
The dog jumped
My friend ate
My friend jumped
I should, after running my program, have a matrix that looks something like:
the, 1, 0, 0
dog, 1, 0, 0
jumped, 1, 0, 1
my, 0, 1, 1
friend, 0, 1, 1
ate, 0, 1, 0
As you can probably tell, the first column corresponds to the first sentence, the second column to the second sentence, etc.
I was told that the best way to go about this is a hash table (or, in the case of C++, a map). My instructor told me this would be a better option than an array because our documents are rather large and the matrix will be rather sparse.