-
October 8th, 2014, 05:17 AM
#1
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
-
October 8th, 2014, 05:29 AM
#2
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)
-
October 8th, 2014, 05:35 AM
#3
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)
-
October 8th, 2014, 06:13 AM
#4
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
-
October 8th, 2014, 06:17 AM
#5
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.
-
October 8th, 2014, 06:28 AM
#6
Re: What is the correct way of inserting an item into a std::map?
-
October 8th, 2014, 06:41 AM
#7
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)
-
October 8th, 2014, 04:39 PM
#8
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
-
October 8th, 2014, 05:37 PM
#9
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.
-
October 8th, 2014, 05:43 PM
#10
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.
-
October 8th, 2014, 06:02 PM
#11
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.
-
October 8th, 2014, 10:51 PM
#12
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
-
October 9th, 2014, 08:18 AM
#13
Re: What is the correct way of inserting an item into a std::map?
Originally Posted by Philip Nicoletti
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);
}
-
October 9th, 2014, 08:52 AM
#14
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 ?
-
October 10th, 2014, 07:11 AM
#15
Re: What is the correct way of inserting an item into a std::map?
Originally Posted by Philip Nicoletti
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|