471,585 Members | 1,157 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,585 software developers and data experts.

Word Counts and Function recommendations

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

Feb 17 '06 #1
3 1499
utab wrote:
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'.
[...]

Feb 17 '06 #2
utab wrote:
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());
}
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 17 '06 #3
Victor Bazarov wrote:
utab wrote:
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...
Feb 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by jester.dev | last post: by
10 posts views Thread by Douglas G | last post: by
3 posts views Thread by Oliver Saunders | last post: by
3 posts views Thread by Adam Faulkner via DotNetMonster.com | last post: by
3 posts views Thread by arnuld | last post: by
3 posts views Thread by waynejr25 | last post: by
3 posts views Thread by frozz85 | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.