Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding and couting tokens in a vector.

Status
Not open for further replies.

Milleniumlegend

IS-IT--Management
Dec 16, 2003
135
Could someone please help me. I am trying to count tokens from a vector and then assign the most common tokens with a mark.

e.g. What is what of what.
this is a sentence in the Vector<string>
I have tokenized the string into a vector containing each word. Now i need to traverse through the Vector and find count for each word and their frequency.
e.g. what is occuring 3 times. and it will have a mark of 1/3
and is occuring once so it will have a mark of 1/1
and so on.

Could someone shed some light on how I can do that using a vector.
Many thanks
Arun
 
Use a map

Code:
vector<string> strings;  // this is what you have
map<string,int> counters;

// iterate over vector of strings
// use some kind of for loop
counters[strings[i]]++; // count how many times each string occurs

// iterate over the map to output frequency
// use another for loop
cout << "word " << i << " appears " << counters[i] << "times" << endl;

--
 
Thanks Salem. Say how do you think we can implement this in a map. your idea does sound interesting. Say if I have a word in a vector
e.g. Vector contains {What is what is this}

so this should turn out to be what,2 is,2 and this,1

So that i can then calculate a score for the whole sentence based on the frequency. The most popular to the least popular.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top