Search code examples
c++arraysmapsnodesheuristics

C++ Map send pointer element as parameter


I need to print the array from nodes in a list that I made for a heuristic search algorithm.

For the list, I have a C++ map std::map<int, Node>List

WHERE Node is a structure that looks like:

struct Node{
    Node *pParent;
    int array[3][3];  };

I can successfully insert the Node "n" to the list like this:

List.insert(std::make_pair(0, *n));

But I'm having trouble on how to send "Node" as a parameter to the function:

void printArr(Node *tNode){
int row, col;
//prints the 2Darray
    for(row = 0; row < 3; row++) {
        for(col = 0; col < 3; col++) {
            std::cout << tNode->array[row][col] << " ";
 }

I tried this method to send it as parameter but it did not work.

std::map<int, Node> p = List.begin();   
printBoard(p.second);

I received the following warnings:

[Error] conversion from 'std::map<int, Node>::iterator {aka std::_Rb_tree_iterator<std::pair<const int, Node> >}' to non-scalar type 'std::map<int, Node>' requested

[Error] 'class std::map<int, Node>' has no member named 'second'

Also, how should I access the contents of Node "n" which is added to List?


Solution

  • std::map<int, Node> p = List.begin(); doesn't work because begin() returns an iterator but you've declared p to have type std::map.

    You want:

    std::map<int, Node>::iterator p = List.begin(); 
    

    Or simply use auto to not have to care about the type:

    auto p = List.begin();