473,246 Members | 1,492 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,246 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 1584
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

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.