C++ 从地图中获取项目并添加到向量c++;
我在程序中死记硬背这段代码C++ 从地图中获取项目并添加到向量c++;,c++,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") {
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.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;
}
但当我尝试选择第二项的id时,std::cout
按此方式修改副本构造函数。为什么不使用隐式定义的副本构造函数,而不是定义新的副本构造函数?如果定义一个新的,为什么不使用初始化列表呢?@jogojapan我同意。这将是最简单的事情。通过删除代码来修复某些东西并不完全常见,接受它应该是值得庆祝的=)
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;
}
Node::Node(const Node& orig) {
this->id = orig.id;
this->latitude = orig.latitude;
this->longitude = orig.longitude;
}