Hi, I'm a new member of this board. Currently I'm still studying about STL. I've some experiences with C and C++ (although mostly modular programming). I've used OOP before although quite messy and, from my point of view, not effective enough. But I hadn't use STL yet until recently. Most of the STL I used were string and vector. But recently I tried to implement map to effectively search for a certain data. I have some questions regarding it:

1. Can there be the same first variable of a map with a different second value?

2. When I used the map, when I tried to compile the program, there's warnings all over (up to 90). And it's worse if I used string as the first argument (440). Mostly warnings about identifier truncated to (255). Strangely, the program works fine. So what do the warnings mean? How can I remove these horribly annoying warnings?

3. What's the difference of map and hashmap?

Thanks in advance.

BTW, I used VC 6.

07-19-2006

laserlight

Quote:

1. Can there be the same first variable of a map with a different second value?

If I understand what you mean, then no. A std::map must have unique keys. However, there is also std::multimap which allows duplicate keys.

Quote:

2. When I used the map, when I tried to compile the program, there's warnings all over (up to 90). And it's worse if I used string as the first argument (440). Mostly warnings about identifier truncated to (255). Strangely, the program works fine. So what do the warnings mean? How can I remove these horribly annoying warnings?

Provide the smallest and simplest compilable program that demonstrates the errors.

As I said, the code worked fine. The annoying things were that hundreds of warning messages. It took minutes to compile the code. Yeah, maybe I forgot to delete the object., but that's not the issue here. The main thing is how do I turn the warnings off?

A map is just that. It MAPS a unique key to an object. This means it requires 2 parameters when creating it. The make_pair function will return pair(obj1,obj2).
You are attempting to create a map by only passing in a string or value which won't work.

And this:

Code:

Foo* temp;
if(!(temp = new Foo))
{

printf("ERROR creating object!");
return -1;
}

mFoo2["test"]=temp;
mFoo1[1]=temp;

Shows me you need to do some reading on std::map.
Also note that you are leaking objects in that code.

07-19-2006

grumpy

Quote:

Originally Posted by g4j31a5

2. When I used the map, when I tried to compile the program, there's warnings all over (up to 90). And it's worse if I used string as the first argument (440). Mostly warnings about identifier truncated to (255). Strangely, the program works fine. So what do the warnings mean? How can I remove these horribly annoying warnings?

You're using a Microsoft compiler, right?

This warning is a property of your compiler, and how it works when compiling for debugging - I would guess because of limitations of the debugging environment or IDE. The limit of 255 is a limit on the length of a type name that the debugger can handle, and it is easy to go over that limit when instantiating templates. Compile for release (i.e. turn off debugging features, and don't try to step through the program with the IDE) and the warnings will go away.

07-19-2006

CornedBee

Upgrade your compiler. Only VC++6, which is horribly outdated, generates these warnings.

07-19-2006

hk_mp5kpdw

Like grumpy said, you can build in release mode and those warnings will not show up. You could also add this line to your code above all your #include directives:

Code:

#pragma warning(disable:4786)

That turns off (disables) warning #4786.

07-19-2006

VirtualAce

make_pair is most certainly a MS-specific function from their utility classes that come with the MS version of the STL. It is located in <utility> and while handy, is prob not part of standard STL.

And since your code worked fine but had about a million warnings, I recommend changing compilers. MSVC 6 is a mess. You don't have to use make_pair, it's just a matter of personal preference.

I deleted my previous post as it was a bit harsh since your code and solution did work.

07-19-2006

CornedBee

std::make_pair is a completely valid and official part of the C++ standard library. However, the call make_pair("test",pTemp) returns an object of type std::pair<const char *, Foo*> (might be a different type in the first parameter, something like char*, const char (&)[5], or similar), which is not convertible to the std::pair<std::string, Foo *> the map expects. You need an explicit constructor call for that:
make_pair(std::string("test"), pTemp);

07-19-2006

Daved

>> 3. What's the difference of map and hashmap?

First, hashmap is not standard, but a hash based map has been sort of standardized by its addition to TR1 (technical report 1). Basically that means that compilers can already support it now and it will most likely be added to the next standard. The hashed based containers are unordered_map, unordered_multimap, unordered_set, and unordered_multiset. If you are using VC++ 6, I would not recommend using the hashmap that comes with that library implementation, since it is different than the ones that will be standardized and when you upgrade your compiler, your code will no longer work. If you'd like to use a hash based map, use boost's version of unordered_*.

As for the differences between a hash based map and a regular map, there are several. The STL map (and multimap, set and multiset) is always ordered. That means if you insert a bunch of data, then iterate through the map, it will come out in order. A hash based map is not ordered (hence the name unordered_map). Another difference is the algorithmic complexity. A regular map is usually implemented with a binary tree and guarantees O(log n) insert and lookup. A hash based map has a O(1) insert and lookup, although it has bad worst case times if your hash function isn't good or your container is not big enough.

Assuming you can use the TR1 version of unordered_map, then use the unordered one if you don't need your data in a particular order, and use the regular one if you do, or if performance isn't that important and you don't have a good hash function. The same advice applies to unordered_multimap versus multimap, unordered_set versus set, and unordered_multiset versus multiset.

07-19-2006

g4j31a5

Thanks guys. The annoying warnings are now gone. Yeah, VC6 really is outdated. But I can't afford to go .Net right now. :p

07-19-2006

Daved

You can get the latest Visual C++ compilers for free, as well as the Express edition of the 2005 IDE (which should be enough to run standard C++ code).

07-20-2006

VirtualAce

I erased this very line of code in my post before posting.

make_pair(std::string("test"), pTemp);

But I did not know that make_pair was standard STL. Thanks for the info CornedBee. You just never know what's standard and what's not with MS products. Any place I can get a copy of what is standard STL?

07-20-2006

laserlight

Quote:

Any place I can get a copy of what is standard STL?

SGI's STL guide lists what is from the standard library and what is from their own extensions, but it can be a little difficult to navigate, in my opinion.

A PDF copy of the C++ Standard would be another way, but I like Nicolai M. Josuttis' hardcover book The C++ Standard Library: A Tutorial and Reference as it is the easiest to navigate.