tags:

views:

102

answers:

5

This is a question(3-3) in ACCELERATED C++. I am a fresh man for C++. I have thought for a long time, however, failed figuring it out. Will anyone kind to resolve this problem for me? Thank you very much. Please explain it in detail, you know I am not very good at programming. Tell me the meaning of the variables you use.

THANKS A LOT!

A: 

Here's my hint. std::map will be your friend.

TokenMacGuy
Thank you very much.
Eric Wang
A: 

Well, you need a way of getting individual words from the input stream (perhaps something like an "input stream" method applied to the "standard input stream") and a way of storing those strings and counts in some sort of "collection".

My natural homework cynicism and general apathy towards life prevent me from adding more detail at the moment :-)

The meaning of any variables I use is fairly self-evident since I tend to use things like objectsRemaining or hasBeenOpened. Thanks for asking.

paxdiablo
I don't know when I chose the tag homework, actually I am learning it by myself. You very funny~
Eric Wang
@Eric: The `homework` tag was added by Paul, presumably in order to prevent people from posting pastable solutions.
sbi
Who is Paul? Is it a program name? You've mess me up.
Eric Wang
@Eric: (You need to properly @address people in comment replies, or your replies won't show up in their profile's replies tab.) Paul Hadfield has edited your question and added the `homework` tag. This is indicated right under your question. You might want to read the [FAQ](http://stackoverflow.com/faq).
sbi
A: 

Here is an algorthm you could use, try coding something and put you results here. People can then help you get further.

Scan down the string collecting each letter until you get to a word boundary (say space or . or , etc). Take that word and compare it to the words you've already found, if already found then add one to the count for that word. If it's not then add that word to the list of words found with a count of 1.

Carry on down the string

Preet Sangha
3Q. My algorithm is the same as yours. The question is how to compare the word you just entered with the word before. I mean the data type is string, are there any function such as comparing string?
Eric Wang
+1  A: 

To provide maximal learning experience, I will not provide pastable code. That's an exercise. You have to do it yourself to learn as much as you can.

This is the perfect scenario for employing a kind of map that creates its value type upon accessing a non-existing key. Fortunately, C++ has such a map in its standard library: std::map<key_type,value_type> is exactly what you need.

So here's the jigsaw pieces:

  • you can read word by word from a stream into a string by using operator >>
  • you can store what you find in a map of words (strings) to occurrences (unsigned number type)
  • when you access an entry in the map through a non-existing key, the map will helpfully create a new default-constructed value under that key for you; if the value happens to be a number, default-construction will set it to 0 (zero)

Have fun put this together!

sbi
Thank you very much. I'll try it myself.
Eric Wang
+3  A: 

The best data structure for this is something like a std::map<std::string,unsigned>, but you don't encounter maps until chapter 7.

Here are some hints based on the contents of chapter 3:

  • You can put strings in a vector, so you can have std::vector<std::string>
  • Strings can be compared, so std::sort works with std::vector<std::string>, and you can check if two strings are the same with s1==s2 just like for integers.
  • You saw in chapter 1 that std::cin >> s reads a word from std::cin into s if s is a std::string.
Anthony Williams
THANK YOU VERY MUCH. I think I've got what I need.
Eric Wang
`+1` for knowing which knowledge can be acquired in different chapters of the book.
sbi