Thread: Some STL questions

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    Some STL questions

    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.

    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.

    3. What's the difference of map and hashmap?
    You may find SGI's Standard Template Library Programmer's Guide useful. hash_map is not part of the C++ standard library.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by laserlight
    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.
    Ah, I see. Thanks.

    Quote Originally Posted by laserlight
    Provide the smallest and simplest compilable program that demonstrates the errors.
    Here you go:
    Code:
    // Test.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "Foo.h"
    #include <map>
    #include <string>
    
    using namespace std;
    
    typedef map<string, Foo *> mapFooName;
    typedef mapFooName::iterator itrFooName;
    
    typedef map<unsigned int, Foo *> mapFooHandle;
    typedef mapFooHandle::iterator itrFooHandle;
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    	mapFooHandle mFoo1;
    	mapFooName mFoo2;
    
    	Foo* temp;
    	if(!(temp = new Foo))
    	{
    
    		printf("ERROR creating object!");
    		return -1;
    	}
    
    	mFoo2["test"]=temp;
    	mFoo1[1]=temp;
    
    	return 0;
    }
    And this one generates 81 warnings for example like this:
    c:\program files\microsoft visual studio\vc98\include\xtree(118) : warning C4786: 'std::_Tree<std::basic_string<char,std::char_trait s<char>,std::allocator<char> >,std:air<std::basic_string<char,std::char_traits<char>, std::allocator<char> > const ,
    Foo *>,std::map<std::basic_string<char,std::char_trait s<char>,std::allocator<char> >,Foo *,std::less<std::basic_string<char,std::char_trait s<char>,std::allocator<char> > >,std::allocator<Foo *> >::_Kfn,std::less<std::basic_string<char,std::char _trai
    ts<char>,std::allocator<char> > >,std::allocator<Foo *> >' : identifier was truncated to '255' characters in the debug information
    Quote Originally Posted by laserlight
    You may find SGI's Standard Template Library Programmer's Guide useful. hash_map is not part of the C++ standard library.
    Thanks again.

  4. #4
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    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?

    BTW, I've tried your code of 'make_pair' but it didn't work.

    Quote Originally Posted by Bubba
    Code:
    // Test.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "Foo.h"
    #include <map>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    	map<string, Foo *> mapFooName;
                    map<unsigned int, Foo *> mapFooHandle;
    
    	Foo* pTemp=new Foo;
    	if(!pTemp)
    	{
    
    		printf("ERROR creating object!");
    		return -1;
    	}
    
    	mapFooName[0]=make_pair("test",pTemp);
                    mapFooHandle[0]=make_pair(0,pTemp);
    
                   
    	return 0;
    }
    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.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Upgrade your compiler. Only VC++6, which is horribly outdated, generates these warnings.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.
    Last edited by VirtualAce; 07-19-2006 at 05:37 AM.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

  11. #11
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Thanks guys. The annoying warnings are now gone. Yeah, VC6 really is outdated. But I can't afford to go .Net right now.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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).

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  2. Array of Vectors amd other STL questions
    By kolistivra in forum C++ Programming
    Replies: 16
    Last Post: 04-12-2007, 09:11 AM
  3. 2 STL questions
    By arjunajay in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2006, 04:52 AM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM