Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,424 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,952 people online right now. Registration is fast and FREE... Join Now!




very beginner question on vectors

 
Reply to this topicStart new topic

very beginner question on vectors

talby
25 Jan, 2008 - 03:56 PM
Post #1

New D.I.C Head
*

Joined: 24 Jan, 2008
Posts: 9

Hello-

I am new to the forums and C++ and I have started reading Accelerated C++, which I heard was a great book because of it's real life examples. Anyways one of the sample problems is:

3-3. Write a program to count how many times each distinct word appears in its input.

I understand, based on what the chapter was about, that I have to put all of the words into a vector and then sort them somehow, but I have no idea what kind of sorting I would have to do, and how to count the words. This is what I have so far:

CODE

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(){

    cout << "Enter a list of words followed by end-of-file:";
    vector<string> wordlist;
    string x;
    while(cin >> x){
              wordlist.push_back(x);
              }


its not much haha
I just need some guidance in the right direction, thanks in advance.

p.s. I was just thinking that this might not be such a good book, since it's "accelerated" so if anyone has any suggestions for a first c++ book, I'd appreciate that too, or if this one's fine then let me know. thanks again.
User is offlineProfile CardPM
+Quote Post

MorphiusFaydal
RE: Very Beginner Question On Vectors
25 Jan, 2008 - 06:55 PM
Post #2

D.I.C Lover
Group Icon

Joined: 12 May, 2005
Posts: 1,113



Thanked: 9 times
Expert In: Hardware, Networking

My Contributions
Step through the vector, and compare each word to all the previously stored words. If it's already in there, discard.
User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: Very Beginner Question On Vectors
25 Jan, 2008 - 09:52 PM
Post #3

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

I think you've got yourself an infinite loop there on the input. How does one get out of the loop? Not sure what "end of file" means in regards to the "cin" prompt. I revised it a little. Here, one enters in words. When the user is finished, he/she enters DONE.

CODE

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<string> wordlist;
    string x;
    
    cout << "Enter a list of words followed by DONE: ";  
    cin >> x;
    while(x.compare ("DONE") != 0)
    {
        wordlist.push_back (x);
        cin >> x;
    }
    
    return 0;
}

User is offlineProfile CardPM
+Quote Post

Bench
RE: Very Beginner Question On Vectors
26 Jan, 2008 - 03:34 AM
Post #4

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 630



Thanked: 16 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Since I have AC++ to hand, i flicked to Section 3. The book should briefly mention a library facility called sort, which exists in the <algorithm> header

sort is one of those really useful functions, because it makes a traditionally fiddly programming task really easy. All you need to do is
CODE
sort( wordlist.begin(), wordlist.end() );


To count the unique entries should be easy now. You know that all duplicates have been lined up next to each other in the vector. You can examine every element in your vector in order, and compare it to its predecessor (Obviously, the first word is a special case, because it doesn't have a predecessor).


As for leaving the loop, AC++ makes the assumption that you know how to cause cin to turn 'bad'. On *nix and windows machines you can usually achieve this in console applications by pressing Ctrl+C. Otherwise, as VernonDozier said, your while block will go on forever.



Re: Books - AC++ is undoubtedly a great beginners' book. whether its right for you depends how comfortable you feel with it so far - its a very to-the-point book, which doesn't waste paper with long explanations, and crams more useful information in per page than almost any other book I've seen. This makes it quite heavy-going if C++ is your first programming language, although it encourages learning-by-example, and learning by making mistakes for yourself (Unlike some books which spend pages overloading you with details on everything which can go wrong). if you've reached the end of Ch3 without too many problems, then you're doing well IMHO.

AC++ certainly isn't a "read once" book. I've personally been through it two or three times in some detail (And used it as a reference on many other occasions). So don't worry if you feel that you've missed out on bits and pieces. Some experienced C++ programmers I know also claim to have learned more than a few new tricks just from reading the examples.

This post has been edited by Bench: 26 Jan, 2008 - 03:36 AM
User is offlineProfile CardPM
+Quote Post

talby
RE: Very Beginner Question On Vectors
26 Jan, 2008 - 10:51 AM
Post #5

New D.I.C Head
*

Joined: 24 Jan, 2008
Posts: 9

thank you very much guys for the help
i think i've got it now smile.gif
and thank you bench for advice on AC++
i'll continue reading unless I get lost
thanks again everyone for the help smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:35AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month