strange std::map behavior

This is a discussion on strange std::map behavior within the C++ Programming forums, part of the General Programming Boards category; A good interface is one where your guess as to what things do is what they actually do. A large ...

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    A good interface is one where your guess as to what things do is what they actually do. A large part of that is function naming too. Unfortunately the [] convenience function for a map doesn't have a descriptive name, and many people that guess its behaviour, get it wrong.
    That said, it is terribly convenient, and many of us would be lost without it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    Always conduct the documentation first
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Unfortunately the [] convenience function for a map doesn't have a descriptive name, and many people that guess its behaviour, get it wrong.
    The interface design of vector:perator[] is perfectly intuitive for people who learned C arrays. Migrating C programmers have always been an important target for the designers of C++ and its standard library.
    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

  4. #19
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by CornedBee View Post
    The interface design of vector:: operator[] is perfectly intuitive for people who learned C arrays. Migrating C programmers have always been an important target for the designers of C++ and its standard library.
    Oh certainly the vector one is, but I was talking about the map one.
    And in all fairness, if all your code ever does is uses the [] operator on the map, in-effect treating it like an ultra large array, then it pretty much will act like just an ultra large array.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Oh certainly the vector one is, but I was talking about the map one.
    That's a more weird case. map's [] works the way it does because there is simply no other way it could work. It's not exactly intuitive to newbs, but when you think it through, you find that there's no alternative.
    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

  6. #21
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, it could throw an exception if the key doesn't exist, so it was an interface design decision to make it work as it does?

    Anyway, when comparing C++ to Python, then the good practices concerning use of exception is rather different. In Python, heavy usage of exception is encouraged. It is encouraged to try an operation without previous checks, catch any possible exceptions and carry on. For example, even the for construct uses exceptions under the hood to stop looping, if I'm not mistaken.

    In addition the dict type is probably not quite the same as std::map in C++. In C++ it is just an associative container. In Python it is a built-in object, there's even special syntax for unpacking dictionaries in function arguments, so its usage seems a bit different.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #22
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Indeed - exceptions are very expensive in many C++ implementations, and the standard explicitly encourages implementors to optimize the non-exception case at the expense of the exception case. (Or was it the Speed TR? Not sure.)

    If [] threw an exception on a missing element, you couldn't do this:
    Code:
    m[key] = value;
    and have it set the map element at key to value, no matter if it's already there or not.
    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

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,747
    If [] threw an exception on a missing element, you couldn't do this:

    and have it set the map element at key to value, no matter if it's already there or not.
    Of course, but it throwing an exception on a missing element implies that that (cumbersome) behaviour is intended. It becomes about as convenient as using find() and insert(), with possibly worse performance due to the exception handling, but that is different from saying that there is no alternative.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Banned
    Join Date
    Nov 2007
    Posts
    678
    hats off to the strange behavior of C++ in the name of ... i don't know whom

    give an out of range index to [] operator of vector class and it gives garbage!
    give a non-existing key to [] operator of map class it creates stupid stuff!

    C++ is an excellent language, you can do everything, anyway you like, with it.
    but the mind of the people who have already implemented so much stuff (and ready availability of such stuff is a must), i can't understand what they were thinking of!

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,747
    but the mind of the people who have already implemented so much stuff (and ready availability of such stuff is a must), i can't understand what they were thinking of!
    That's easy:
    give an out of range index to [] operator of vector class and it gives garbage!
    They were thinking of efficiency, and provided the alternative of at() if efficiency is not desired.

    give a non-existing key to [] operator of map class it creates stupid stuff!
    As we have been trying to tell you, it does not create stupid stuff. It creates an object using the default constructor. If this is stupid, it implies that your usage of it is stupid, since you should use member find() instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Banned
    Join Date
    Nov 2007
    Posts
    678
    why there is not a has_key() function? so sad

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,747
    why there is not a has_key() function?
    has_key() is equivalent to m.find(key) != m.end(), for a map named m.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    why there is not a has_key() function? so sad
    Isn't that what "find()" is for?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #29
    Banned
    Join Date
    Nov 2007
    Posts
    678
    thanks!
    there are kludgey ways. alright. there are ways around every problem. ok.
    but the thinking of original implementers is still lacking ... i feel

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,747
    but the thinking of original implementers is still lacking ... i feel
    Explain why you think the thinking of the library designers is lacking.

    Note that find() is more powerful than a has_key() function. Not only that, but the use case of not inserting an element if one exists implies searching for an element with that key. It is a waste of time to check if the map has a key, and then search for the element with that key, because the act of checking if the map has a key involves searching for it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  2. C++ list Strange Behavior
    By yongzai in forum C++ Programming
    Replies: 19
    Last Post: 12-29-2006, 01:56 AM
  3. Strange behavior of Strings
    By shyam168 in forum C Programming
    Replies: 9
    Last Post: 03-27-2006, 06:41 AM
  4. strange behavior
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 10-17-2005, 12:03 PM
  5. Strange behavior with CDateTimeCtrl
    By DonFiasco in forum Windows Programming
    Replies: 2
    Last Post: 12-19-2004, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21