Get Items from map and add to a vector c++

I rote this code in my program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
JSONNode::const_iterator iter = root.begin();
    for (; iter!=root.end(); ++iter)
    {
        const JSONNode& arrayNode = *iter;
        std::string type = arrayNode["type"].as_string();
        if(type == "node")
        {
            std::string id = arrayNode["id"].as_string();
            double lat = arrayNode["lat"].as_float();
            double lon = arrayNode["lon"].as_float();
            Node node;
            node.SetId(id);
            node.SetLatitude(lat);
            node.SetLongitude(lon);
            nodesMap.insert( std::pair<std::string, Node>(id, node) );
        }
        else if(type == "way")
        {
            std::string wayId = arrayNode["id"].as_string();
            wayNode.SetId(wayId);
            std::vector<Node> collection;
            const JSONNode& wayNodes = arrayNode["nodes"];
            const JSONNode& nodes = wayNodes.as_array();
            JSONNode::const_iterator WayIter = nodes.begin();
            for (; WayIter!=nodes.end(); ++WayIter)
            {
                const JSONNode& arrayNode = *WayIter;
                std::string id = arrayNode.as_string();
                if(nodesMap.find(id) != nodesMap.end())
                {
                    collection.push_back(nodesMap.find(id)->second);
                    nodesMap.erase(id);  
                }
            }
            wayNode.SetNodesCollection(collection);
            std::cout<<"Item Id ->>>>>>>>>>>>>" << collection[2].GetId() << std::endl;
        }
    }


Node.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Node {
private:
    std::string id;
    double latitude;
    double longitude;
public:
    Node();
    Node(const Node& orig);
    Node(std::string id, double lat, double lon);
    virtual ~Node();
    void SetLongitude(double longitude);
    double const & GetLongitude() const;
    void SetLatitude(double latitude);
    double const & GetLatitude() const;
    void SetId(std::string id);
    std::string const & GetId() const;
};


Node.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Node::Node() {
}
Node::Node(const Node& orig) {
}
Node::~Node() {
}
Node::Node(std::string id, double lat, double lon){
    this->id = id;
    this->latitude = lat;
    this->longitude = lon;
}
void Node::SetLongitude(double longitude) {
    this->longitude = longitude;
}
double const & Node::GetLongitude() const {
    return longitude;
}
void Node::SetLatitude(double latitude) {
    this->latitude = latitude;
}
double const & Node::GetLatitude() const {
    return latitude;
}
void Node::SetId(std::string id) {
    this->id = id;
}
std::string const & Node::GetId() const {
    return id;
}


but when i try to ptint id of second item std::cout<<"Item Id ->>>>>>>>>>>>>" << collection[2].GetId() << std::endl;
it gets a blank value. but size of the collection is 82, get the correct value for the collection size.

i need some help to sort out this.
thanks in advance!
When you access nodeMap in the "Way" case, why not use the iterator? You can use it to get to the node data and you need it for erase.

I don't have enough information to work out your specific problem. Have you walked thru this in a debugger or printed intermediate states (with cout)?
Topic archived. No new replies allowed.