无论如何,有没有将键,值,值存储到映射中 [英] Is there anyway to store key, value, value into map
问题描述
在阅读了大多数maps
问题之后,我最终从此链接中得到了一个主意:
After reading through most of the maps
questions, I eventually got an idea from this link: How to unique my data that are stored in an object which are stored in a vector?
我的任务是从用户输入中存储X
,Y
,Z
坐标.为了防止用户输入重复的数据,我决定使用地图容器,因为它们不允许重复的数据.
I have a task of storing X
, Y
, Z
coordinates from the user input. To prevent the user from entering duplicated data, I have decided to use map containers as they do not allow duplicated data.
我测试了代码.
我将X
用作key
,将Y用作value
我可以通过以下方式存储X
和Y
:
I am able to store X
and Y
via this:
map<int, int> mapp2d;
mapp2d.insert(pair<int, int>(X, Y));
并通过以下方式访问它们:
And to access them via this:
map<int, int>::iterator p = mapp2d.begin();
map<int, int>::iterator e = mapp2d.end();
while ( p != mapp2d.end())
{
cout << "X: " << p->first << " Y: " << p->second << endl;
p++;
}
是否可以对X
,Y
,Z
执行此操作?像这样:
Is it possible to do it for X
, Y
, Z
? Like this:
map<int, int, int> mapp3d
mapp3d.insert(pair<int, int, int>(X, Y, Z))
我测试了代码,但出现了以下错误:
I tested out the code but i got this errors:
error: wrong number of template arguments (3, should be 2)
error: provided for ‘template<class _T1, class _T2> struct std::pair
是的,我确实知道这样做是违法的,但是没有关于该怎么做的教程,而且我的想法已经用光了,我该如何使用它们呢?
Yes, i do know it is illegal for me to do that, but there're no tutorials on how should i do so, and i kind of ran out of ideas, and how can i access them ?
感谢你们的光临,谢谢.
Appreciate you guys for taking a look, thanks in advance.
推荐答案
您应该将坐标存储在一起,而不是将一个坐标用作键:
You should store the coordinates together instead of using one coordinate as key:
struct Point
{
int x, y, z;
Point(int x, int y, int z) : x(x), y(y), z(z) {}
};
然后只实现一个自定义比较器:
Then just implement a custom comparator:
struct PointComparator
{
bool operator()(const Point& a, const Point& b)
{
if (a.x < b.x) return true;
if (a.x == b.x && a.y < b.y) return true;
if (a.x == b.x && a.y == b.y && a.z < b.z) return true;
return false;
}
};
最后,请使用 set 而非地图(因为您的情况是,值和键是相同的):
And finally, use a set instead of a map (because in your case, values and keys are the same):
#include <set>
std::set<Point, PointComparator> points;
集合是按顺序排列的(就像地图一样),默认情况下是从低到高的值.您需要在此处指定一个自定义比较器,因为如果您只是执行std::set<Point>
,则使用的默认比较器是std::less
.该比较器可以比较数字等,但不知道如何比较类型为Point
的对象.因此,该集合无法正确排序其元素,也无法确定两个元素是否相同(这就是您要追求的).如果执行std::set<Point, PointComparator>
,则会创建一个使用PointComparator
中的逻辑比较其元素的集合.
Sets are (just like maps) ordered, by default from low to high values. You need to specify a custom comparator here because the default one that is used if you just do std::set<Point>
is std::less
. This comparator can compare numbers etc, but has no clue how to compare objects of type Point
. The set could therefore not order its elements properly and also not figure out if two elements are identical (which is what you are after). If you do std::set<Point, PointComparator>
, you create a set that uses the logic in PointComparator
to compare its elements.
此示例将打印"1"和"2":
This example will print "1" and "2":
points.insert(Point(1, 2, 3));
points.insert(Point(1, 2, 3));
points.insert(Point(2, 3, 4));
std::set<Point, PointComparator>::iterator it = points.begin();
while (it != points.end()) {
std::cout << "x = " << it->x << std::endl;
it++;
}
这篇关于无论如何,有没有将键,值,值存储到映射中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!