Hi all,
For all those who are knowleadgeable enough I have a question for you. I am creating a program for my assignment in my Data Structures course and a piece of this assignment requires me to take in a file and analyze the existence and frequency of letters in my file. This is what I have so far:

My program compiles and I do not know if I am missing something logically but as far as I can tell, it is not even entering the nested for loop. Please help.

11-29-2003

bennyandthejets

Code:

for(MI = m.begin(); MI != m.end(); MI++)

My knowledge of STL maps is basically nil, but I can see a potential problem with this line. Before you have added any items to the map, wouldn't m.begin() be the same as m.end()? That would stop the loop ever running.

11-29-2003

Lurker

Quote:

Originally posted by bennyandthejets

Code:

for(MI = m.begin(); MI != m.end(); MI++)

My knowledge of STL maps is basically nil, but I can see a potential problem with this line. Before you have added any items to the map, wouldn't m.begin() be the same as m.end()? That would stop the loop ever running.

Almost right. begin() would be the first element, or basically the last element, too. end() is the last element + 1 (one AFTER the end of the map). The program loops through this for once. Be sure to use the insert() function to put pairs into the map.

11-29-2003

grib

I appaud your use of the STL here, but you can make life easyer with just an array of 256 int's set to 0 that you index by char.

The first problem you have is you are searching for your chars like you might from a simple array, and map provides the find() method (indeed this is the point of a map);

Code:

if((MI = m.find(ch)) == m.end()) {
m[ch]=0;
} else {
MI->second++;
}

We can simpify this, because the [] operator will create a default constructed object if it does not already exist in the map. Raw int's don't have default constructors, I don't know what a map does in this situation but we can fix this easily enough.

Funny thing is that I ended up doing the exact same thing today to check some indexing code. I don't it's right that Sang should take all of the credit :) It's in the beginning of Stroustrup's book. The ++ thing that is.

12-01-2003

Sang-drax

Quote:

Originally posted by grib Raw int's don't have default constructors,

Yes they do! (in a way)
Try:

Code:

int p = int();
cout << p;
cout << int();

This is very useful in template containers, such as maps.
A map<int> will always initialize the values with 0.