Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 是否有方法将键、值、值存储到地图中_C++_Dictionary_Containers_Duplicate Removal - Fatal编程技术网

C++ 是否有方法将键、值、值存储到地图中

C++ 是否有方法将键、值、值存储到地图中,c++,dictionary,containers,duplicate-removal,C++,Dictionary,Containers,Duplicate Removal,在阅读了大部分的地图问题后,我最终从这个链接中得到了一个想法: 我的任务是存储用户输入的X,Y,Z坐标。为了防止用户输入重复数据,我决定使用地图容器,因为它们不允许重复数据 我测试了代码 我使用X作为键,Y作为值 我可以通过以下方式存储X和Y: map<int, int> mapp2d; mapp2d.insert(pair<int, int>(X, Y)); map<int, int>::iterator p = mapp2d.begin(); map&l

在阅读了大部分的
地图
问题后,我最终从这个链接中得到了一个想法:

我的任务是存储用户输入的
X
Y
Z
坐标。为了防止用户输入重复数据,我决定使用地图容器,因为它们不允许重复数据

我测试了代码

我使用
X
作为
,Y作为

我可以通过以下方式存储
X
Y

map<int, int> mapp2d;
mapp2d.insert(pair<int, int>(X, Y));
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++; 
}
我测试了代码,但出现以下错误:

error: wrong number of template arguments (3, should be 2)
error: provided for ‘template<class _T1, class _T2> struct std::pair
错误:模板参数的数量错误(3,应该是2)
错误:为“模板结构std::pair”提供
是的,我知道我这样做是违法的,但是没有关于我应该怎么做的教程,我的想法有点用完了,我怎样才能获得它们


感谢你们的关注,提前谢谢。

你们考虑过使用元组吗?它们可用于容纳2个以上的元素

我想,如果你真的一心想使用地图,另一种可能是做如下链接,其中地图的值是:


您应该将坐标存储在一起,而不是使用一个坐标作为键:

struct Point
{
    int x, y, z;

    Point(int x, int y, int z) : x(x), y(y), z(z) {}
};
然后只需实现一个自定义比较器:

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;
    }
};

<>我不是C++专家,但我认为你应该使用集合而不是映射,并使用元组来计算这些值。集合通常用于保存唯一的值(尽管我是从Java和Python知识而不是C++来讲)。查看集合和无序集合;显然,无序的_集速度更快,但集合已排序。它们都不跟踪您放置东西的顺序,也不跟踪映射,因此如果您需要,那么您需要找到不同的解决方案。我认为元组可以存储任意数量的元素

区分如何存储数据和如何访问数据通常很有用

例如,您可以将点存储在向量中,但可以使用(例如)std::set在其上放置“唯一性索引”。下面是一些独立的代码,演示了这个想法:

#include <functional>
#include <iostream>
#include <memory>
#include <set>
#include <vector>

class Point {
 public:
  Point(int x, int y, int z) : m_x(x), m_y(y), m_z(z) {}

  int x() const { return m_x; }
  int y() const { return m_y; }
  int z() const { return m_z; }

 private:
  int m_x;
  int m_y;
  int m_z;
};

bool operator<(const Point & a, const Point & b) {
  return (a.x() < b.x()
          || (a.x() == b.x()
              && (a.y() < b.y()
                  || (a.y() == b.y()
                      && a.z() < b.z()))));
}


class Points {
 public:
  Points() {}

  bool add(Point p) {
    if (m_uniqueIndex.count(p) == 0) {
      m_points.emplace_back(new Point(p));
      m_uniqueIndex.emplace(*m_points.back());
      return true;
    }
    return false;
  }

 private:
  std::set<std::reference_wrapper<Point>> m_uniqueIndex;
  std::vector<std::unique_ptr<Point>> m_points;
};

const char * s(bool b) {
  return b ? "true" : "false";
}

int main(int argc, char * argv[]) {
  Point a(1, 2, 3);
  Point b(2, 1, 0);
  Point c(-1, 0, 1);
  Point a2(1, 2, 3);

  Points points;

  std::cout << "Adding a: " << s(points.add(a)) << std::endl;
  std::cout << "Adding b: " << s(points.add(b)) << std::endl;
  std::cout << "Adding c: " << s(points.add(c)) << std::endl;
  std::cout << "Adding a2: " << s(points.add(a2)) << std::endl;
}
#包括
#包括
#包括
#包括
#包括
类点{
公众:
点(intx,inty,intz):m_x(x),m_y(y),m_z(z){
int x()常量{return m_x;}
int y()常量{return m_y;}
int z()常量{return m_z;}
私人:
int m_x;
国际货币基金组织;
国际货币基金组织;
};

布尔运算符将“点”对象存储在一个集合中不是更有意义吗?在
std::map
中存储坐标
(x,y)
实际上没有意义,因为不能有任何重复的
,这意味着不能有多个坐标具有相同的
x
值。@Niko在什么意义上?@Galik因为,例如,用户可能输入
X:1
Y:1
Z:1
,并且可能忘记了他这样做,并且可能再次输入相同的数据,当打印存储的数据时,容器中会有重复的数据,因此,我试图使用地图来禁止存储重复项问题是,即使用户添加
X:1y:4
X:1y:1
的原始坐标也将被删除,因为你不能有两个
X:1
元素,因为
X
是关键..我可以使用任何,只要它能够存储3个数据变量,包括对象类型,并防止重复,感谢我将使用C++11查看,
PointComparator
可能编写得更简单:
return std::tie(a.x,a.y,a.z) @ JAROD42我没有,这是一个任务,我们只限于C++98@Niko我不太明白你的代码是如何工作的,从我所学的,我只知道用这种方式使用它。
set myset
@TKM我已经添加了更多的解释和示例,希望能有所帮助。@Niko谢谢你,现在我很清楚了,我将尝试实现它谢谢分享,我在生活中从未使用过元组,因此我必须重新学习,这将需要一段时间让我理解和实现感谢您提出了一个示例,我将研究它们并尝试将它们实现到我的工作中
#include <set>
std::set<Point, PointComparator> points;
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++;
}
#include <functional>
#include <iostream>
#include <memory>
#include <set>
#include <vector>

class Point {
 public:
  Point(int x, int y, int z) : m_x(x), m_y(y), m_z(z) {}

  int x() const { return m_x; }
  int y() const { return m_y; }
  int z() const { return m_z; }

 private:
  int m_x;
  int m_y;
  int m_z;
};

bool operator<(const Point & a, const Point & b) {
  return (a.x() < b.x()
          || (a.x() == b.x()
              && (a.y() < b.y()
                  || (a.y() == b.y()
                      && a.z() < b.z()))));
}


class Points {
 public:
  Points() {}

  bool add(Point p) {
    if (m_uniqueIndex.count(p) == 0) {
      m_points.emplace_back(new Point(p));
      m_uniqueIndex.emplace(*m_points.back());
      return true;
    }
    return false;
  }

 private:
  std::set<std::reference_wrapper<Point>> m_uniqueIndex;
  std::vector<std::unique_ptr<Point>> m_points;
};

const char * s(bool b) {
  return b ? "true" : "false";
}

int main(int argc, char * argv[]) {
  Point a(1, 2, 3);
  Point b(2, 1, 0);
  Point c(-1, 0, 1);
  Point a2(1, 2, 3);

  Points points;

  std::cout << "Adding a: " << s(points.add(a)) << std::endl;
  std::cout << "Adding b: " << s(points.add(b)) << std::endl;
  std::cout << "Adding c: " << s(points.add(c)) << std::endl;
  std::cout << "Adding a2: " << s(points.add(a2)) << std::endl;
}