Is this Implementation Defined ?

This is a discussion on Is this Implementation Defined ? within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523

    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 ?
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    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 04:26 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,618
    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, Sunshine, and read this, this, and this before posting again.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    Didn't know that the map was being changed.
    ...would stick with find when necessary...
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    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 02:29 PM.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 01:02 PM
  2. C 89 Implementation-Defined Behavior?
    By billhavens in forum C Programming
    Replies: 17
    Last Post: 04-18-2010, 05:37 PM
  3. Replies: 7
    Last Post: 10-01-2008, 08: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, 04:04 AM
  5. #if defined...
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-06-2002, 08:08 PM

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