Thread: Is this Implementation Defined ?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Is this Implementation Defined ?

    Code:
    #include<iostream>
    #include<map>
    int main()
    {
        int x = 8;
        std::map<int,int*> m={{1,&x},{5,&x}};
        if(m[9]==nullptr)
            std::cout<<"Works";
        return 0;
    }
    It "Works" for me.
    Should I rely on this(when the 'second' of the pair is a pointer type) instead of 'find'ing, which appears to be slower ?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    map::operator[] performs look up which is as fast as find().

    Look up things that may not be in the map, such as m[9], then find() will return end(). operator[] would just return something, the second part of the pair that would be at end() but isn't, which could be anything or it could segfault.
    Last edited by whiteflags; 10-30-2011 at 03:26 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    operator[] inserts an element into the map if one isn't there (with the second (int * in your case) default constructed) . The problem with your technique is therefore that it changes the map. find() reports what is or isn't in the map - it doesn't change the map.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Didn't know that the map was being changed.
    ...would stick with find when necessary...

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    find is faster than the [] operator of a map. In fact the [] operator may call find internally.
    You do know that you're mapping to a local variable is only of use for demonstratory purposes right? Don't do that in a real program.
    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"

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You do know that you're mapping to a local variable is only of use for demonstratory purposes right
    Yes..
    In fact I have a global map that 'maps' the addresses(integers or as I'd change it later... some address class) of some 'node's (of a graph like structure) to the pointers pointing to the actual nodes... I'm adding only 'new'-ed nodes to that map...(but am trying to think up something which will automatically allocate identical new space if by chance local pointers are used... though I don't think that would be necessary if I'm careful )
    Last edited by manasij7479; 10-30-2011 at 01:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Machine dependent and implementation defined
    By _arjun in forum C Programming
    Replies: 3
    Last Post: 10-29-2011, 12:02 PM
  2. C 89 Implementation-Defined Behavior?
    By billhavens in forum C Programming
    Replies: 17
    Last Post: 04-18-2010, 04:37 PM
  3. Replies: 7
    Last Post: 10-01-2008, 07:45 PM
  4. how to use a self-defined lib in VC6
    By wu7up in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 03:04 AM
  5. #if defined...
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-06-2002, 08:08 PM