CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    What is the correct way of inserting an item into a std::map?

    Code:
    std::map<Point3, bool> m_mHitMap;
    
    if (bHit) {
    	m_mHitMap.insert(std::make_pair(testRay.p, true));
    }
    else {
    	m_mHitMap.insert(std::make_pair(testRay.p, false));
    }
    Error 2 error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const Point3' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125 1 PixelNavMeshExporter

    Thanks
    Jack

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: What is the correct way of inserting an item into a std::map?

    Assuming testRay.p is of type Point3 and Point3 is a class, then the class Point3 needs to provide an operator overload for <. See http://msdn.microsoft.com/en-us/library/5tk49fh2.aspx for more details about operator overloading.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: What is the correct way of inserting an item into a std::map?

    Do you need an ordered map? Have you considered using an unordered map? See http://www.cplusplus.com/reference/u...unordered_map/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Dec 2010
    Posts
    907

    Re: What is the correct way of inserting an item into a std::map?

    Is it true that the key type must be a primitive type (int, char, char* etc) in order to avoid the compare function?
    Thanks
    Jack

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: What is the correct way of inserting an item into a std::map?

    ALL types use a comparison function. By default, it is operator <.
    Types such as int , char , std::string have operator < defined, so the
    user does not need to do anything if the default behavior is OK.

  6. #6
    Join Date
    Dec 2010
    Posts
    907

    Re: What is the correct way of inserting an item into a std::map?

    I got it, thanks
    Jack

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: What is the correct way of inserting an item into a std::map?

    You can also specify a compare so that you don't need to add the < operator to the class. See http://www.cplusplus.com/reference/map/map/map/ and the example.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Dec 2010
    Posts
    907

    Re: What is the correct way of inserting an item into a std::map?

    Code:
    struct classcomp {
      bool operator() (const Point3& lhs, const Point3& rhs) const
      {
    	  if (lhs.x < rhs.x && lhs.y < rhs.y && lhs.z < rhs.z)
    		return true;
    	  else
    	    return false;
    
      }
    };
    
    std::unordered_map<Point3, bool, classcomp> m_mMap;
    
    m_mMap[testRay.p] = bHit;

    Error 3 error C2064: term does not evaluate to a function taking 1 arguments c:\program files (x86)\microsoft visual studio 10.0\vc\include\xhash 154 1 PixelNavMeshExporter


    THX

  9. #9
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: What is the correct way of inserting an item into a std::map?

    1) Your classcomp struct is is suitable for a std::map (but I am not
    convinced it will work. Which should come first in the container,
    (1,3,4) or (2,2,3) ?

    2) I have not used std::unordered_map, but I would guess that it would
    only take one argument (not 2 like yours does). And it should return a
    size_t , not a bool.

  10. #10
    Join Date
    Dec 2010
    Posts
    907

    Re: What is the correct way of inserting an item into a std::map?

    how come unordered_map takes only one parameter
    Like this?
    std::unordered_map<Point3> and returns size_t?
    I need an associative container to map a Point3 to a boolean...
    Point3 has x,y and z
    THX
    Last edited by lucky6969b; October 8th, 2014 at 05:46 PM.

  11. #11
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: What is the correct way of inserting an item into a std::map?

    Here is some sample code to give you some ideas:

    Code:
    #include <map>
    #include <unordered_map>
    
    using namespace std;
    
    struct Point3
    {
        int x,y,z;
    
        bool operator == (const Point3 & rhs) const
        {
            return (x==rhs.x && y==rhs.y && z==rhs.z);
        }
    };
    
    
    
    struct classcomp_map 
    {
      bool operator() (const Point3& lhs, const Point3& rhs) const
      {
        if (lhs.x < rhs.x) return true;
        if (lhs.x > rhs.x) return false;
    
        // x values are equal, check y
    
        if (lhs.y < rhs.y) return true;
        if (lhs.y > rhs.y) return false;
    
        // x and y are equal, use z
    
        return lhs.z < rhs.z;
      }
    };
    
    struct classcomp_unordered_map 
    {
      // note: this will have a lot of collisions
      size_t operator() (const Point3& p) const
      {
         return p.x + p.y + p.z;
      }
    };
    
    int main()
    {
        std::unordered_map<Point3, bool, classcomp_unordered_map> mm;
        std::map<Point3,bool,classcomp_map> m;
    
        Point3 p;
    
        m[p] = true;
        mm[p] = true;
    
    
        return 0;
    }
    Last edited by Philip Nicoletti; October 8th, 2014 at 06:11 PM.

  12. #12
    Join Date
    Dec 2010
    Posts
    907

    Re: What is the correct way of inserting an item into a std::map?

    Thanks, that's so nice of you.
    This is a very clear explanation.
    Jack

  13. #13
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: What is the correct way of inserting an item into a std::map?

    Quote Originally Posted by Philip Nicoletti View Post
    Code:
      bool operator() (const Point3& lhs, const Point3& rhs) const
      {
        if (lhs.x < rhs.x) return true;
        if (lhs.x > rhs.x) return false;
    
        // x values are equal, check y
    
        if (lhs.y < rhs.y) return true;
        if (lhs.y > rhs.y) return false;
    
        // x and y are equal, use z
    
        return lhs.z < rhs.z;
      }
    or alternatively, you can make use of tie<> to (automatically) create a tuple of the right type to do the comparison chain for you (this might not be that important for a 3segment key, but it'll make it a lot easier for even bigger chains).

    Code:
      bool operator() (const Point3& lhs, const Point3& rhs) const
      {
         return std::tie(lhs.x, lhs.y, lhs.z) < std::tie(rhs.x, rhs.y, rhs.z);
      }

  14. #14
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: What is the correct way of inserting an item into a std::map?

    Nice. I wasn't aware that operator < existed.

    I wonder how I missed that ?

  15. #15
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: What is the correct way of inserting an item into a std::map?

    Quote Originally Posted by Philip Nicoletti View Post
    Nice. I wasn't aware that operator < existed.
    which is why it's a good thing to lurk around on CG, even if you don't have any questions or answers


    I wonder how I missed that ?
    the thing is...
    even if you had known it existed, it might not have made the click in your head as to why/how/when such a thing would be actually useful in actual code.

    for example: I'm still confused as to the whole "bind" thing, I sort of understand what it does, but I just never been in a real situation where it's use was Obvious or even necessary.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured