mercredi 25 janvier 2017

How to add struct, vector of struct to map within class

My issue is in the following .h:

#include <iostream>
#include <map>
#include <vector>

struct Node {
    int data;
};

class Graph {

std::map< Node, std::vector<Node> > edge_map;

public:
    Graph();

    void add_neighbor(Node cur_node, Node new_node);

    void remove_neighbor(Node cur_node, Node del_node);

    virtual ~Graph();
};

and in trying to implement the add_neighbor function, but I'm fumbling through the details and need some direction. What I've got so far (not functional) is the following:

void Graph::add_neighbor(Node cur_node, Node new_node) {
    if (edge_map.find(cur_node) == edge_map.end()) {
        edge_map.insert(std::pair<Node, std::vector<Node> >(cur_node, std::vector<Node>()));
        edge_map[cur_node].push_back(new_node);
    }
    else {
        edge_map[cur_node].push_back(new_node);
    }
}

I can copy the explosion that g++ -std=c++11 throws at me, but it's not very helpful. How can I include a map as a class member where the key, value are struct, vector of struct, and then have a function in the .cpp that can add it? I'll also need the remove function, but once I understand how to treat the data in the insertion function I'll know what I need, I think.

Aucun commentaire:

Enregistrer un commentaire