473,382 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 1591
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jester.dev | last post by:
Hello, I'm learning Python from Python Bible, and having some problems with this code below. When I run it, I get nothing. It should open the file poem.txt (which exists in the current...
11
by: Ron | last post by:
Hello, I'm having an aggravating time getting the "html" spewed by Word 2003 to display correctly in a webpage. The situation here is that the people creating the documents only know Word, and...
10
by: Douglas G | last post by:
I've tried various ideas on this problem, but I don't see word wrapping. Can you point out what is wrong? It's a K&R exercise, and I'm still new to programming. Other pointers would be helpful...
3
by: Oliver Saunders | last post by:
I was really shocked not to find this already submitted by someone in the PHP manual notes. <? function jnWordCount($text) { $results = array(); $pattern = '/+\S+/'; // matches wordy...
3
by: Adam Faulkner via DotNetMonster.com | last post by:
I want to create a method within a class that opens a Microsoft Word 2000 Document and has the facility to Create a new word document and then extract a Page that exists within the original Word...
3
by: arnuld | last post by:
this is an example programme that counts lines, words and characters. i have noticed one thing that this programme counts space, a newline and a tab as a character. i know: 1. a newline is...
3
by: waynejr25 | last post by:
can anyone debug my program and get it to run. #include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <map> using namespace std;
209
by: arnuld | last post by:
I searched the c.l.c archives provided by Google as Google Groups with "word input" as the key words and did not come up with anything good. C++ has std::string for taking a word as input from...
3
by: frozz85 | last post by:
I'm trying to print email address and each of their frequency on it's right by reading a text file. The counting part is correct but it prints other things as well. I'm not sure how to solve this....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.