Dear,
The thing I would like to do is to write a function(count) that counts
the occurence of words in my input. If the vector is like
"st1"
"st1"
"st2"
"st2"
"st2"
"st3"
"st3"
"st4"
(st = string)
after the sort function invoked, I wanted my function to give the
output as
st1 = 2
st2 = 3
st3 = 2
st4 = 1
I tried again but all the things I am trying for count are wrong.
Solutions without map are appreciated because I am looking for a
solution in the usual loops with while, for, .....
Regards,
#include <iostream>
#include <cmath>
#include <vector>
#include <iomanip>
#include <string>
#include <cstdlib>
using std::cout; using std::cin;
using std::vector; using std::endl;
using std::setprecision; using std::setw;
using std::max; using std::string;
bool compare(const string & str1 ,const string & str2 ){
return str1 < str2;
}
// function will be sth like this
void count(const vector<string> & S, vector<string> & store,vector<int>
&num){
....codes // could not fill in this however
// store and num are the vectors that I would like to
hold the strings and number of
// occurences respectively.
}
int main(){
vector<string> str; //
vector<string>::size_type sz;
vector<string> s; //occurence vector
vector<int> n; // number of occurence
vector
string x;
while(cin >> x){
str.push_back(x);
}
sz=str.size();
for(vector<string>::size_type i=0; i!=sz; i++){
cout << str[i] << endl;
}
sort(str.begin(),str.end(),compare);
for(vector<string>::size_type i=0; i!=sz; i++){
cout << str[i] << endl;
}
count(str,s,n); // that
count function will be called
for(vector<string>::size_type i=0; i!=sz; i++){
cout << s[i] << ' ' << n[i] << endl;
}
return 0;
}