tags:

views:

141

answers:

4

I have the following C++ map example code:

map<string,string> &weight2price  
....  
weight = ...  
price = ...  
if(weight2price.find(weight) == weight2price.end())  
    weight2price[weight] = price;

could anyone tell me what it means by

if(weight2price.find(weight) == weight2price.end())
+6  A: 

If the key passed to find() does not exist, find() returns end(). So it's saying "If it wasn't found...".

The code could be written better as:

weight2price.insert(std::make_pair(weight, price));

So look-up isn't done twice. (If the key already exists, insert won't do anything.)

GMan
+1: Good call -- probably faster, *and* makes the intent clear.
Jerry Coffin
+1  A: 

It means that weight was not found. Then if it wasn't found, it adds it.

deinst
I see, thanks :)
ladyfafa
A: 

map in C++ is a like hashmap, where a key (in your case weight) is mapped to a value (price).

Here it checks to see if the particular weight is already mapped in this map, if not the find method will return weight2price.end()

In that case the program enters a key-value pair in the map with the line that you greyed out.

iniju
It is not a hash map.
GMan
`std::map` in C++ is not a hash map. It is *like* a hash map in that it's a key/value lookup system, but there's no hashing involved; it's just a search tree.
jamesdlin
so is it the same as a dictionary in python?
ladyfafa
Thanks for the correction, adjusted my answer.
iniju
+4  A: 

The STL Map is a standard C++ container. It uses unique keys that can be compared to refer to value data being stored inside the container. It works much like an array but unlike an array, the key can be anything and doesn't need to be an integer. The syntax for a map is:

std::map < Key, Data, Compare, Alloc>

Key - the type of object that will be used as key
Data - the type of object that will be used as data
Compare - a comparison function for the keys
Alloc - The map's allocator, used for all internal memory management

usage:

map[Key] = Data

For further reading about STL maps look here : http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29 and here: http://www.sgi.com/tech/stl/Map.html

In the code snippet, you are using the function find() from the map. This function returns an iterator pointing to the element from the map that contains the key you were searching for. If that key is not found, the find function will return the iterator pointing to the end of the map. That is what is being checked in the code snippet you attached to your question and if the find function returned the value of the end iterator, this means that the key is not in the map, so it adds the key and data to the map. An optimization for your code was provided by GMan in his comment.

A very basic explanation for an iterator (although not complete) would be that an iterator is a pointer to a pair< Key,Data>.

Hope this helps!

John Paul
so does that mean I could use a dictionary in python to do the same things? I mean, are these two working basically the same?
ladyfafa
weight2price = {} weight = ... price = ... if weight not in weight2price.keys(): weight2price[weight] = price
ladyfafa
btw, how to make this in a nice code format when I type it inside the textfield????
ladyfafa
I must confess that I didn't know how a dictionary in python looks like but I googled it :). After 5 minutes look I can say that the base functionality of a dictionary in Python looks (after a short look) like a the functionality of a C++ STL map, yes.
John Paul
if weight not in weight2price.keys(): weight2price[weight] = price this is equivalent to the if in your code snippet (c++)
John Paul
the question about the textfield I'm afraid, it's much more complicated.. depends.. what do you want to do exactly in the text field ? you want to have 2 text fields, one with the keys and the other with the data (weight in your case) ? What GUI framework do you use ? MFC ? I suggest reading about the map and playing a bit with the code.. both the GUI code and with a map.. it will not be long until you understand how to do what you are trying, doing it, is much easier than explaining :). Good luck!
John Paul
A dictionary in Python is more similar to `unordered_map` than `map`, but yes, they accomplish a similar thing :) Also: you don't need the `.keys()` call, just "if weight not in weight2price:" is sufficient (and probably faster?)
Peter
@ladyfafa: For reference, the usual generic term for these sorts of structures is "associative array". Their implementation varies wildly, and some aremore efficient for certain tasks than others, but they all act pretty similar on the outside.
Nicholas Knight
thanks all :)))
ladyfafa
yes, dont have to use the .keys() call, could also work
ladyfafa
@John Paul, thanks, i didnt realize the STL map is sorted though
ladyfafa
@ladyfafa: Your wellcome :). Yes, The keys are sorted, that's why the Compare function is needed for custom types. For strings and standard types, you don't have to provide it as the < operator is already implemented.
John Paul