Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.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++_Boost_Operator Overloading_Containers_Constants - Fatal编程技术网

C++ 迭代向量并添加迭代器实际指向的对象作为映射中的键

C++ 迭代向量并添加迭代器实际指向的对象作为映射中的键,c++,boost,operator-overloading,containers,constants,C++,Boost,Operator Overloading,Containers,Constants,在一个Graph类中,我创建了顶点类对象的无序映射作为键,浮点作为映射值。在vertex类中,我实现了一个friend函数hash_值,以便将自定义类对象用作键: 和图表的标题: #include "Vertex.h" #include <vector> #include <unordered_map> #include <limits> #include <boost/functional/hash.hpp> #include <boos

在一个Graph类中,我创建了顶点类对象的无序映射作为键,浮点作为映射值。在vertex类中,我实现了一个friend函数hash_值,以便将自定义类对象用作键:

和图表的标题:

#include "Vertex.h"
#include <vector>
#include <unordered_map>
#include <limits>
#include <boost/functional/hash.hpp>
#include <boost/unordered_set.hpp>
#include <boost\unordered_map.hpp>
class Graph{
    private:
        const int VERTICES_MAX = 120;
        const int VERTICES_MIN = 5;

        std::vector<Vertex> vertices_list;
        float cost_matrix[120][120];

        struct Edge { 
        std::string vertex1; std::string vertex2; float weight; };
        std::vector<Edge> edge_list;

        boost::unordered_map<Vertex, float> KEY;

    public:
        Graph(int num_of_vertices);
        float SetEdgeWeight(Vertex* v1, Vertex* v2);
        void SetGraphEdgesWeight();
        void DisplayWeightMatrix(int num_of_vertices);
        void DisplayVerticesData();
        void MatrixToEdgeList();
        void DisplayList();

        void Christofides(Graph& g);
 };
在Graph类的.cpp文件中,我创建了一个函数,该函数迭代向量顶点列表,并添加迭代器实际指向的对象作为无序映射的键

Graph::Graph(int num_of_vertices){
    typedef boost::mt19937 RNGType; ///Mersenne twister generator
    RNGType rng(time(0));

    boost::uniform_int<> zero_to_thousand(0, 1000);
    boost::variate_generator< RNGType, boost::uniform_int<> > point(rng,  zero_to_thousand);
/*-----------------------------------------------------------------------------------*/
    for (int i = 0; i < num_of_vertices; ++i) {
        std::string vertex_label;
        std::cout << "Vertex name: " << std::endl;
        std::cin >> vertex_label;
        Vertex* V = new Vertex(vertex_label, point(), point());
        V->SetVertexNo(i);
        vertices_list.push_back(*V);

        SetGraphEdgesWeight();
        MatrixToEdgeList();
    }
}
float Graph::SetEdgeWeight(Vertex* v1, Vertex* v2) {
    int absolute_x = (v1->GetPositionX() - v2->GetPositionX());
    int absolute_y = (v1->GetPositionY() - v2->GetPositionY());
    float edge_weight = (float)(sqrt(pow(absolute_x,2)+pow(absolute_y,2)));
    return edge_weight;
}
void Graph::SetGraphEdgesWeight() {
    int i = 0;
    for (std::vector<Vertex>::iterator it = vertices_list.begin(); it !=  vertices_list.end(); ++it) {
        Vertex* pointer = &*it;
        int n = 0;
        for (std::vector<Vertex>::iterator it2 = vertices_list.begin(); it2  != vertices_list.end(); ++it2) {
            Vertex* pointee = &*it2;
            //cost_matrix[i][n] = SetEdgeWeight(pointer, pointee);
            if (pointee->IsVisited()==true) {
                cost_matrix[i][n] = cost_matrix[n][i];
            }
            else if(it == it2){
                cost_matrix[i][n] = 0;
            }
            else {
                pointer->SetVisitedTrue();
                cost_matrix[i][n] = SetEdgeWeight(pointer, pointee);
            }
            ++n;
        }
        ++i;
    }
}
void Graph::DisplayWeightMatrix(int num_of_vertices) {
    for (int i = 0; i < num_of_vertices; ++i) {
        for (int j = 0; j < num_of_vertices; ++j) {
            std::cout << "*" << " " << cost_matrix[i][j] << " ";
        }
        std::cout << "*" << std::endl;
    }
}
void Graph::DisplayVerticesData() {
    int i = 1;
    for (std::vector<Vertex>::iterator it = vertices_list.begin(); it !=   vertices_list.end(); ++it) {
        std::cout << "Vertex no: " << i << " " << "x:" << it->GetPositionX()  <<" "<< "y:" << it->GetPositionY() << std::endl;
        ++i;
    }
}
void Graph::MatrixToEdgeList() {
    int i = 0;
    for (std::vector<Vertex>::iterator it = vertices_list.begin(); it !=  vertices_list.end(); ++it) {
        int n = 0;
        for (std::vector<Vertex>::iterator it2 = vertices_list.begin(); it2 != vertices_list.end(); ++it2) {
            //std::vector<Vertex>::iterator it3 = ++it2;

            if (cost_matrix[i][n]==0) {
                 ++n;
                break;
            }
            else {
                struct Edge* e = new struct Edge;
                e->vertex1 = it->GetLabel();
                e->vertex2 = it2->GetLabel();
                e->weight = cost_matrix[i][n];
                edge_list.push_back(*e);
                ++n;
            }
        }
        ++i;
    }
}
void Graph::DisplayList() {
    for (std::vector<Edge>::iterator it = edge_list.begin(); it != edge_list.end(); ++it) {
        std::cout << "Starting vertex: " << it->vertex1 << " " << "Ending vertex: " << it->vertex2 << " " << "Edge's weight: " << it->weight<<"\n";
    }
}
void Graph::Christofides(Graph& g) {
    for (std::vector<Vertex>::iterator it = vertices_list.begin(); it != vertices_list.end(); ++it) {
        Vertex* pointer = &*it;
        g.KEY.insert(std::pair<Vertex,float>(*pointer,   std::numeric_limits<float>::max()));
        //KEY.emplace(*pointer, std::numeric_limits<float>::max());
    }


    /*
    for (auto v : g.vertices_list) {

        //KEY.insert(*v_pointer, std::numeric_limits<float>::max());
        //KEY.insert(std::make_pair(v, std::numeric_limits<float>::max()));
       //KEY[v] = std::numeric_limits<float>::max();
    }
    */
}
问题就在这里。我无法将hash_value函数的参数更改为non-const,因为它需要const¶meter。此外,当我在收到以下错误时必须将其添加到地图中时,也不能使用:

二进制“==”:未找到接受“const Vertex”类型的左侧操作数的运算符,或者没有可接受的转换


我尝试了不同的解决方法来克服这个问题,但我没有足够的经验来做到这一点。欢迎提供任何提示。

对于无序容器,您需要一个哈希函数和一个相等比较。例如,从标准库:

template<
    class Key,
    class T,
    class Hash = std::hash<Key>,
    class KeyEqual = std::equal_to<Key>,
    class Allocator = std::allocator< std::pair<const Key, T> >
> class unordered_map;

请注意,等式函数和哈希函数的含义必须一致:a==b意味着b==a和hasha==hashb,否则您将导致未定义的行为。

请阅读如何提供最小、完整且可验证的示例:感谢您的帮助。我还考虑过重载“==”运算符,但我不知道如何正确地执行它。@MichaelS。它能解决问题吗?因为我可能会编辑问题的标题。
template<
    class Key,
    class T,
    class Hash = std::hash<Key>,
    class KeyEqual = std::equal_to<Key>,
    class Allocator = std::allocator< std::pair<const Key, T> >
> class unordered_map;
   bool operator==(Vertex const& other) const {
        return std::tie(x,y) == std::tie(other.x, other.y);
   }