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

Occurence problem: different ideas

Dear all,

I tried to solve the occurence problem: to find the distinct occurences
of a word in an input. I know that I could use map and other STD lib
functions. I tried to do it the hard way. I tried sth and it is working
but I could not structure my algorithm very well so I had to add some
more lines after thinking it on a paper draft. I am posting the whole
code and waiting different ideas.
First I sorted them with the sort function, after that they are in
nondecreasing order(and also grouped by this way) and then I have
counted the number of items in these groups by checking whether I am at
the end or not

1 #include <iostream>
2 #include <algorithm>
3 #include <string>
4 #include <vector>
5 int main(){
6
7 std::string x;
8 std::vector<std::string> input;
9 std::vector<std::string> sorted_input;
10 std::vector<std::string> words;
11 std::vector<int> counts;
12 std::vector<std::string>::size_type vec_sz;
13 int count=1;
14
15
16 while(std::cin >> x)
17 input.push_back(x);
18
19 sort(input.begin(),input.end());
20
21 std::string test=input[0];
22
23 vec_sz=input.size();
24
25 for(int i=0;i!=vec_sz;++i)
26 std::cout << input[i] << std::endl;
27
28
29 for(int k=1;k!=vec_sz;++k){
30
31 if(input[k]==test){
32 ++count;
33 if(k==vec_sz-1){
34 words.push_back(test);
35 counts.push_back(count);
36 }
37 }
38 else{
39 words.push_back(test);
40 counts.push_back(count);
41 test=input[k];
42 count=1;
43 if(k==vec_sz-1){
44 words.push_back(test);
45 counts.push_back(count);
46 }
47 }
48 }
49
50 vec_sz=words.size();
51 std::cout << "WORDS" <<"\t"<<"OCCURENCE#"<<std::endl;
52 for(int i=0;i!=vec_sz;++i)
53 std::cout << words[i]<<"\t" << counts[i]<<
std::endl;
54
55 return 0;
56
57 }

May 7 '06 #1
3 1700

"utab" <um********@gmail.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
Dear all,

I tried to solve the occurence problem: to find the distinct occurences
of a word in an input. I know that I could use map and other STD lib
functions.
Then why don't you?
I tried to do it the hard way.
Why?
I tried sth and it is working
but I could not structure my algorithm very well so I had to add some
more lines after thinking it on a paper draft. I am posting the whole
code and waiting different ideas.
My idea is to use those tools suited for a particular purpose,
e.g. a map in this case.
First I sorted them with the sort function, after that they are in
nondecreasing order(and also grouped by this way) and then I have
counted the number of items in these groups by checking whether I am at
the end or not
See below.
1 #include <iostream>
2 #include <algorithm>
3 #include <string>
4 #include <vector>
5 int main(){
6
7 std::string x;
8 std::vector<std::string> input;
9 std::vector<std::string> sorted_input;
10 std::vector<std::string> words;
11 std::vector<int> counts;
12 std::vector<std::string>::size_type vec_sz;
13 int count=1;
14
15
16 while(std::cin >> x)
17 input.push_back(x);
18
19 sort(input.begin(),input.end());
20
21 std::string test=input[0];
22
23 vec_sz=input.size();
24
25 for(int i=0;i!=vec_sz;++i)
26 std::cout << input[i] << std::endl;
27
28
29 for(int k=1;k!=vec_sz;++k){
30
31 if(input[k]==test){
32 ++count;
33 if(k==vec_sz-1){
34 words.push_back(test);
35 counts.push_back(count);
36 }
37 }
38 else{
39 words.push_back(test);
40 counts.push_back(count);
41 test=input[k];
42 count=1;
43 if(k==vec_sz-1){
44 words.push_back(test);
45 counts.push_back(count);
46 }
47 }
48 }
49
50 vec_sz=words.size();
51 std::cout << "WORDS" <<"\t"<<"OCCURENCE#"<<std::endl;
52 for(int i=0;i!=vec_sz;++i)
53 std::cout << words[i]<<"\t" << counts[i]<<
std::endl;
54
55 return 0;
56
57 }


#include <ios>
#include <iostream>
#include <map>
#include <string>

int main()
{
std::map<std::string, std::streamsize> m;
std::string word;

std::cout << "Enter data: ";

while(std::cin >> word)
++m[word];

std::map<std::string, std::streamsize>::
const_iterator it(m.begin());

const
std::map<std::string, std::streamsize>::
const_iterator en(m.end());

while(it !=en)
{
std::cout << it->first << " occurs "
<< it->second << " times.\n";
++it;
}

return 0;
}

Test run:
--------------------------------------------------------------------------------
Enter data: In base class, I have a function "Initialize(int a)". In the
derived

class , I need the funciton "Initialized" to do more things, therefore
I redefined the function as "Initialize(int a, int b)".

Now I want the Derived::initialize to include all the operations
contained in Base::initialize. Are there and simple ways to do that?
Currently, I just copy all the codes in Base::initialize to
Derived::initialize.

Thanks.
^Z
"Initialize(int occurs 2 times.
"Initialized" occurs 1 times.
, occurs 1 times.
Are occurs 1 times.
Base::initialize occurs 1 times.
Base::initialize. occurs 1 times.
Currently, occurs 1 times.
Derived::initialize occurs 1 times.
Derived::initialize. occurs 1 times.
I occurs 5 times.
In occurs 2 times.
Now occurs 1 times.
Thanks. occurs 1 times.
a occurs 1 times.
a)". occurs 1 times.
a, occurs 1 times.
all occurs 2 times.
and occurs 1 times.
as occurs 1 times.
b)". occurs 1 times.
base occurs 1 times.
class occurs 1 times.
class, occurs 1 times.
codes occurs 1 times.
contained occurs 1 times.
copy occurs 1 times.
derived occurs 1 times.
do occurs 2 times.
funciton occurs 1 times.
function occurs 2 times.
have occurs 1 times.
in occurs 2 times.
include occurs 1 times.
int occurs 1 times.
just occurs 1 times.
more occurs 1 times.
need occurs 1 times.
operations occurs 1 times.
redefined occurs 1 times.
simple occurs 1 times.
that? occurs 1 times.
the occurs 6 times.
there occurs 1 times.
therefore occurs 1 times.
things, occurs 1 times.
to occurs 4 times.
want occurs 1 times.
ways occurs 1 times.
--------------------------------------------------------------------------------
-Mike
P.S. BTW if you want help, you should make it
easy for folks to help. With those line numbers
we can't paste and compile your code. It was
far easier for me to show you how I would do it
than to try to make your code compilable. (imo
your code is far more complicated than need be
anyway).
May 7 '06 #2
In article <11**********************@y43g2000cwc.googlegroups .com>,
"utab" <um********@gmail.com> wrote:
Dear all,

I tried to solve the occurence problem: to find the distinct occurences
of a word in an input. I know that I could use map and other STD lib
functions. I tried to do it the hard way.
You could have made it much harder on yourself and not used vector. Why
are you willing to use vector but not map or set?

I tried sth and it is working
but I could not structure my algorithm very well so I had to add some
more lines after thinking it on a paper draft. I am posting the whole
code and waiting different ideas.


Here's one solution:

struct word_count
{
word_count( string w, int c ): word( w ), count( c ) { }
string word;
int count;
};

struct word_is : unary_function<word_count, bool >
{
string word;
word_is( string w ): word( w ) { }
bool operator()( word_count wc ) const
{
return wc.word == word;
}
};

template < typename container >
void count_input_words( istream& is, container& c )
{
string s;
while ( is >> s ) {
word_count wc( s, 1 );
typename container::iterator it =
find_if( c.begin(), c.end(), word_is( s ) );
if ( it == c.end() )
c.push_back( wc );
else
++it->count;
}
}

ostream& operator<<( ostream& os, word_count wc )
{
return os << wc.word << "\t" << wc.count;
}

int main()
{
vector<word_count> result;
count_input_words( cin, result );
std::cout << "WORDS\tOCCURENCE#\n";
copy( result.begin(), result.end(),
ostream_iterator<word_count>( cout, "\r" ) );
}

Seems pretty easy to understand, we have only one function with a
Cyclomatic Complexity over 1, and it only has a CC of 3. Compare that to
your code with a CC of 8.

Of course my code doesn't sort the words. That may be part of the
requirements, and would probably also speed up the algorithm by reducing
search time.

It's a simple change. Just change out "word_is" for a StrictWeakOrdering
comparison functor, and switch out the "find_if" for "lower_bound". That
and a minor change to the "if" statement and you are good to go. (I'll
leave it as an exorcise for the reader to implement though. :-)
May 8 '06 #3
utab wrote:
Dear all,

It is difficult to change your code to better without knowing where you
plan to move with it. If you want to create a useful word counting
class, you may consider methods for inputting another word, getting word
counter for a given word, getting an iterator for word counters for all
words etc.. If this is just a sample program, nothing is particularly
wrong (except for not processing the case where there is no words on the
strandard input..).

As for the selected data structure, yours will probably be the fastest
of suggested alternatives (linear search and ordered map as far as I
could understand), especially if you have a good idea about the target
size for your vector and reserve it in the beginning. Hashmap could be
even faster in most of the cases (depending on the implementation and
words on the input stream); it is coming into the Standard from TR1 as
std::tr1::unordered_set..

Hope this will help,
Pavel

May 8 '06 #4

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

Similar topics

2
by: Tesla | last post by:
Hey guys, I have a string like "lalala: djkahsd : dajkdassd : adasd :" Is there a function to find the position of the "I"th occurence of a character/string? Like lets say I want to find out...
1
by: Roland | last post by:
hi all having designed my schema to use repeating groups of elements, I found that some applications (eg. Microsoft InfoPath) refused to recognised that element as a repeating element. I...
0
by: Armin Wagenknecht | last post by:
Hello, i am modelling a TopicMap, and I have the following problem: I want to use two scopes in the topicmap for one occurence which has to satsify BOTH scopes!! Example: I have the...
20
by: truetype | last post by:
Greetings! I consider to create an application using .NET Framework. It would be used by any kind user, mostly those who do not know anyting about programming and ..NET Framework. But have...
2
by: Th.-Fischer | last post by:
hi, i have this xml structure <users> <user id="root"/> <!-- mandatory! --> <user id="a"/> <!-- optional --> <user id="b"/> <!-- optional --> <user id="c"/> <!-- optional -->...
1
by: Gary Cobden | last post by:
Hi I have a routine that uses VBA to open a hidden occurence of Excel, and do background computations. However, in the event that the routine terminates abnormally, I have not been able to...
10
by: Sean Berry | last post by:
I need to find the second to last occurence of a "." in a string. Basically I am taking a URL like http://this.is.mydomin.com/path/to/file.txt and want to extract /path/to/file.txt I...
6
by: utab | last post by:
Dear all, I would like to find the occurence of numbers in an array(I solved this problem before now I could not see the solution) but not with iterators or vectors I want to apply them later, I...
3
by: das | last post by:
Hi all, How can I get a row that has only one occurence in a table? Not through 'distinct' because this gets a single row that might have multiple occurences, I want to get only rows that have...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.