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; Originally Posted by manav thanks! there are kludgey ways. alright. there are ways around every problem. ok. but the thinking ...

  1. #31
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    thanks!
    there are kludgey ways. alright. there are ways around every problem. ok.
    but the thinking of original implementers is still lacking ... i feel
    But what would (internally) m.has_key(somekey) do different from m.find(somekey) != m.end()?

    It serves no real purpose to implement a function that does the has_key() when it's trivial to make such a function from the existing functions - where do you draw the limit for this sort of feature-creep and resulting code-bloat?

    --
    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.

  2. #32
    Banned
    Join Date
    Nov 2007
    Posts
    678
    there are such trivial functions:
    why use printf when there is fprintf?
    why use scanf when there is fscanf?
    just a trivial matter of giving stdin as argument! what say?

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,101
    there are such trivial functions:
    why use printf when there is fprintf?
    why use scanf when there is fscanf?
    just a trivial matter of giving stdin as argument! what say?
    The use cases for those functions are far more common than has_key() without a find() for std::map.
    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

  4. #34
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    The use cases for those functions are far more common than has_key() without a find() for std::map.
    And would even hazard a guess that the code size difference for the average C program (assuming it uses printf at least a little bit), the printf(...) function vs. fprintf(stdout, ...) would be noticeable.

    If you use has_key often enough, maybe you would want to implement a function to do that.

    --
    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.

  5. #35
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,887
    My vote goes to a function

    m.has_key(somekey)

    Instead of

    m.find(somekey) != m.end()

    Because it's easier to read and it looks better.
    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.

  6. #36
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    My vote goes to a function

    m.has_key(somekey)

    Instead of

    m.find(somekey) != m.end()

    Because it's easier to read and it looks better.
    Yes, but you can easily do:
    Code:
    bool has_key(std::map<...> m, KEYTYPE key)
    {
       return m.find(somekey) != m.end();
    }
    --
    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.

  7. #37
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,101
    And would even hazard a guess that the code size difference for the average C program (assuming it uses printf at least a little bit), the printf(...) function vs. fprintf(stdout, ...) would be noticeable.
    I was thinking of that too, but I am too young to know what kind of impact that would have made with respect to hard disk capacity in the 1970s. It probably does not matter nowadays.
    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

  8. #38
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Quote Originally Posted by matsp View Post
    And would even hazard a guess that the code size difference for the average C program (assuming it uses printf at least a little bit), the printf(...) function vs. fprintf(stdout, ...) would be noticeable.
    Sorry! What you mean actually? (I did not understand)

    If you use has_key often enough, maybe you would want to implement a function to do that.
    Of course! I would even like to extend the class std::map.
    But some would advice against it, just like they did in case of mystring class example!

  9. #39
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The typical use case isn't m.find(somekey) != m.end(), though. It's
    Code:
    mymap::iterator it = m.find(somekey);
    if(it != m.end()) {
      std::cout << it->second;
    }
    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. #40
    Banned
    Join Date
    Nov 2007
    Posts
    678
    [quote=matsp;740658]Yes, but you can easily do:
    Code:
    bool has_key(std::map<...> m, KEYTYPE key)
    {
       return m.find(somekey) != m.end();
    }
    What is somekey here? Where do i get it from?

  11. #41
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    Sorry! What you mean actually? (I did not understand)
    I mean that the size of some decent size project (not a "Hello, World" program) would be significantly larger by simply replacing all printf's with fprintf's - because it passes one extra argument, which requires at the very least one more instruction for EVERY printf - and there are often quite a few printf in any non-trivial (console) application, which means that the benefit of having the simplified function is valuable.

    As laserlight points out, the benefit is probably less noticeable in todays world, when applications are:
    1. more often graphical, so printf() isn't very important for that reason.
    2. disk/memoy space is counted in gigabytes rather than kilobytes, so saving a few thousand bytes in one application is not quite so important.

    You would need to use has_key a lot for an equivalent benefit, and if you do, then you probably would want to provide such a function in one way or another.

    --
    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.

  12. #42
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    [QUOTE=manav;740663]
    Quote Originally Posted by matsp View Post
    Yes, but you can easily do:
    Code:
    bool has_key(std::map<...> m, KEYTYPE key)
    {
       return m.find(somekey) != m.end();
    }
    What is somekey here? Where do i get it from?
    Stupid typo (copy'n'paste, actually):
    Code:
    bool has_key(std::map<...> m, KEYTYPE key)
    {
       return m.find(key) != m.end();
    }
    --
    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.

  13. #43
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,101
    My vote goes to a function
    But you see, if you want to do what manav wants to do (print the element corresponding to a key, as per the Python example), has_key() is a complete waste of time. You would just write something along these lines:
    Code:
    std::map<K,V>::iterator iter = map.find(key);
    if (iter != map.end())
    {
        std::cout << iter->second << std::endl;
    }
    else
    {
        std::cout << "No such key: " << key << std::endl;
    }
    If you write:
    Code:
    if (map.has_key(key))
    {
        std::cout << map[key] << std::endl;
    }
    else
    {
        std::cout << "No such key: " << key << std::endl;
    }
    ... you would be finding the key twice, assuming the key exists.

    Of course! I would even like to extend the class std::map.
    But some would advice against it, just like they did in case of mystring class example!
    There is nothing wrong with extending std::map by providing free functions that extend its interface, or by wrapping it in another class. However, you should not inherit from std::map since it was not designed to be a base class and thus its destructor is non-virtual.

    What is somekey here? Where do i get it from?
    A working example would be:
    Code:
    template<typename K, typename V>
    bool has_key(const std::map<K,V>& map, const K& key)
    {
        return map.find(key) != map.end();
    }
    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

  14. #44
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    A working example would be:
    Code:
    template<typename K, typename V>
    bool has_key(const std::map<K,V>& map, const K& key)
    {
        return map.find(key) != map.end();
    }
    Thanks, my template writing skills are still low - we don't use templates much where I work, so I didn't want to produce something that wasn't correct - and it backfired.

    --
    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.

  15. #45
    Banned
    Join Date
    Nov 2007
    Posts
    678
    thanks laser!
    that utility function deserves to be in my toolbox!

Page 3 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, 08:38 PM
  2. C++ list Strange Behavior
    By yongzai in forum C++ Programming
    Replies: 19
    Last Post: 12-29-2006, 02:56 AM
  3. Strange behavior of Strings
    By shyam168 in forum C Programming
    Replies: 9
    Last Post: 03-27-2006, 07:41 AM
  4. strange behavior
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 10-17-2005, 01:03 PM
  5. Strange behavior with CDateTimeCtrl
    By DonFiasco in forum Windows Programming
    Replies: 2
    Last Post: 12-19-2004, 02:54 PM

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