簡體   English   中英

從地圖獲取項目並添加到矢量c ++

[英]Get Items from map and add to a vector c++

我在我的程序中使用此代碼

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

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

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

但是當我嘗試ptint id第二項std :: cout <<“Item Id - >>>>>>>>>>>>> << << collection [2] .GetId()<< std :: endl ; 它得到一個空白值。 但是集合的大小是82,獲取集合大小的正確值。

我需要一些幫助來解決這個問題。 提前致謝!

Node::Node(const Node& orig) {
    this->id = orig.id;
    this->latitude = orig.latitude;
    this->longitude = orig.longitude;
}

像這樣修改你的拷貝構造函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM