473,230 Members | 1,867 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,230 software developers and data experts.

Words Words

Dear all,

I am trying to count the occurence of words in an input stream I can
keep my words in a vector<string> as below;

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

int main(){
vector<string> str;
vector<string>::size_type sz;
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;
}
return 0;
}

Now I think that sorting and finding the occurences are easier so sort
them alphabetically and then find the occurences. But the occurences
causing problems in the algorithms I try on paper. Please DO NOT USE
LISTS, VECTOR.ERASE(), ITERATORS IN THE PROBABLE SOLUTIONS.
Can someone give me any ideas on this?

Thx.

Feb 16 '06 #1
6 1481
utab wrote:
Dear all,

I am trying to count the occurence of words in an input stream I can
keep my words in a vector<string> as below;

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

int main(){
vector<string> str;
vector<string>::size_type sz;
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;
}
return 0;
}

Now I think that sorting and finding the occurences are easier so sort
them alphabetically and then find the occurences. But the occurences
causing problems in the algorithms I try on paper.
I would use std::map<std::string, int> instead of a vector.
Please DO NOT USE LISTS, VECTOR.ERASE(), ITERATORS IN THE PROBABLE
SOLUTIONS.


Why not?

Feb 16 '06 #2
In article <11**********************@o13g2000cwo.googlegroups .com>,
"utab" <um********@gmail.com> wrote:
Dear all,

I am trying to count the occurence of words in an input stream I can
keep my words in a vector<string> as below;

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

int main(){
vector<string> str;
vector<string>::size_type sz;
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;
}
return 0;
}

Now I think that sorting and finding the occurences are easier so sort
them alphabetically and then find the occurences. But the occurences
causing problems in the algorithms I try on paper. Please DO NOT USE
LISTS, VECTOR.ERASE(), ITERATORS IN THE PROBABLE SOLUTIONS.
Can someone give me any ideas on this?

Thx.


Try using a std::map instead, and check out the bottom of my response to
you in the "occurence problem" thread.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 16 '06 #3
I am following a book and I have not covered map yet. I asked a close
friend also he recommended map. But I first would like to find the
answer this way. If can't then dive into map.

thanks for the quick replies

Feb 16 '06 #4
In article <11**********************@o13g2000cwo.googlegroups .com>,
"utab" <um********@gmail.com> wrote:
Dear all,

I am trying to count the occurence of words in an input stream I can
keep my words in a vector<string> as below;

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

int main(){
vector<string> str;
vector<string>::size_type sz;
string x;
while(cin >> x){
str.push_back(x);
Don't load every word into the string, only unique words. (ie: if x not
in str, push_back x.

Once you do the above, post your code and I'll help you more.
}
sz=str.size();
for(vector<string>::size_type i=0; i!=sz; i++){
cout << str[i] << endl;
}
return 0;
}

Now I think that sorting and finding the occurences are easier so sort
them alphabetically and then find the occurences. But the occurences
causing problems in the algorithms I try on paper. Please DO NOT USE
LISTS, VECTOR.ERASE(), ITERATORS IN THE PROBABLE SOLUTIONS.
Can someone give me any ideas on this?

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 16 '06 #5
How to control that is not that easier to sort and apply. I am now
trying that but still problems with the algoritm I have created two
more vectors. One holding the words and the other holding the repeat
times corresponding to the words. Code is wrong but I am sending it
anyway. I think you will understand what I try to do

Thx,

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

}

void count(const vector<string> & S, vector<string> & ss,vector<int>
&nn){

int cnt=1;
vector<string>::size_type i=0;
string compare(S[i]);
// problems with this while loop

while(i != S.size()){

if(S[i+1]==compare)
cnt++;
else{
compare=S[i+1];
if(S[i+1]==compare)
cnt++;
}
ss.push_back(compare);
nn.push_back(cnt);
i++;
}

}

int main(){

vector<string> str;
vector<string>::size_type sz;
vector<string> s;
vector<int> n;

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

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

cout << s[i] << ' ' << n[i] << endl;

}

return 0;
}

Feb 16 '06 #6
In article <11**********************@o13g2000cwo.googlegroups .com>,
"utab" <um********@gmail.com> wrote:
How to control that is not that easier to sort and apply.
No, it isn't easer.
I am now
trying that but still problems with the algoritm I have created two
more vectors. One holding the words and the other holding the repeat
times corresponding to the words. Code is wrong but I am sending it
anyway. I think you will understand what I try to do


You need to start smaller, and work your way up. Paste the code below
into your cpp file (overwrite what you have) and then run the program.
What you are supposed to see is "working" what you will actually see is
some error message. Put code in the "insert your code here" spot and
keep running the program until you can get "working" to appear on the
screen. Once you do that, I'll take you to the next step.

@begin code

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

void count_words( const vector<string>& S, vector<string>& ss,
vector<int>& nn )
{
// insert code here
}

int main() {
{
vector<string> in;
in.push_back( "word" );
vector<string> str;
vector<int> count;
count_words( in, str, count );
assert( str.size() == 1 );
assert( count.size() == 1 );
assert( str[0] == "word" );
assert( count[0] == 1 );
}
{
vector<string> in;
in.push_back( "word" );
in.push_back( "big" );
vector<string> str;
vector<int> count;
count_words( in, str, count );
assert( str.size() == 2 );
assert( count.size() == 2 );
assert( str[0] == "word" );
assert( count[0] == 1 );
assert( str[1] == "big" );
assert( count[1] == 1 );
}
cout << "working";
}

@end code

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 16 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: superprad | last post by:
"X-No-Archive: yes" what I am looking for is 1. To create a list of different words of various lengths(1-15) using A-Z,a-z,0-9 and punctuations.Basically anything that could be found on a...
2
by: CV | last post by:
How can I match 'n' number of neighbouring words of a pattern using regular expressions? For example, suppose I am looking for the pattern "length xyz cm" in some text. where xyz is a number -...
8
by: Rick | last post by:
I have a program that reads from a file. In the file are a series of words. I read in all the words into a string array, find the average length, count the number of words, display the longest...
7
by: Sling | last post by:
I code in Rexx on the mainframe which has 2 built-in functions: word(s,i) & words(s). word(s,i) returns the ith word in the s(tring), and words(s) returns the number of words within the s(tring)....
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
7
by: abraxas | last post by:
Ok maybe someone here can help me. I have been learning c++ and have been toying with some basic programming and have run into something that I would have assumed would have been fairly easy. I...
7
by: Jay | last post by:
How would I be able to grab random words from an internet source. I'd like to grab a random word from a comprehensive internet dictionary. What would be the best source and the best way to go...
20
by: dmurray14 | last post by:
Hey guys, I'm a C++ newbie here - I've messed with VB, but I mostly stick to web languages, so I find C++ to be very confusing at times. Basically, I am trying to import a text file, but I want...
6
by: Xernoth | last post by:
Hi, I have an exercise that requests the following: Write a function that reads words from an input stream and stores them in a vector. Use that function both to write programs that count the...
14
bugboy
by: bugboy | last post by:
I'm a beginner at this and am confused... I have three tables: 1. words / wordpk, word 2. definitions / definitionspk and definition 3. associations / wordpk, definitionspk 'words' holds...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
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...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
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: 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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.