Word Counts and Function recommendations

U

utab

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 << endl;
}

sort(str.begin(),str.end(),compare);

for(vector<string>::size_type i=0; i!=sz; i++){

cout << str << endl;

}
count(str,s,n); // that
count function will be called

for(vector<string>::size_type i=0; i!=sz; i++){

cout << s << ' ' << n << endl;

}
return 0;
}
 
V

Victor Bazarov

utab said:
The thing I would like to do is to write a function(count) that counts
the occurence of words in my input. [..]

I tried again but all the things I am trying for count are wrong.

Try using 'unique_copy'.
 
D

Dietmar Kuehl

utab said:
The thing I would like to do is to write a function(count) that counts
the occurence of words in my input.

Just do it:

#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>

template <typename It>
void print_count(It it, It end)
{
for (It tmp = it; it != end; tmp = it)
{
it = std::adjacent_find(it, end,
std::not2(std::equal_to<std::string>()));
if (it != end)
++it;
std::cout << *tmp << ": " << (it - tmp) << "\n";
}
}

int main()
{
std::istream_iterator<std::string> beg(std::cin), end;
std::vector<std::string> vec(beg, end);
std::sort(vec.begin(), vec.end());
print_count(vec.begin(), vec.end());
}
 
V

Victor Bazarov

Victor said:
utab said:
The thing I would like to do is to write a function(count) that counts
the occurence of words in my input. [..]

I tried again but all the things I am trying for count are wrong.


Try using 'unique_copy'.

Yeah, I probably am totally off here, don't listen to me.. Sorry...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top