C ++遍历地图-消失的对象

时间:2019-12-02 12:59:32

标签: c++ std

因此,我在代码中发现了一个相当奇怪的错误,我很难理解。我有一个std :: map,其中存储了一些信息。第一次循环播放时,一切似乎都很好,但是第二次,数据丢失了。

首先,我使用像这样的结构和类:

struct Eng::Pixel{
        unsigned int x;
        unsigned int y;
    };

class Eng::Edge{
public:
enum EdgeType { INTER, INTRA};
EdgeType type;
}

class Eng::Cluster{
public:
std::map<Pixel, std::vector<Edge>> trans;
}

基本上,集群包含一个像素图。此贴图中的每个像素都包含称为边缘的过渡点。每个像素可以具有多个边缘-边缘的类型可以是inter(0)或intra(1)。请注意,某些命名空间可能会丢失,因为我正在尝试尽可能简化问题。

当我遍历代码时:

std::vector<Cluster> resClusters = this->GenerateClusters();

for(Cluster cluster : resClusters) //For a given cluster in clusters
        {
            this->CreateIntraEdges(cluster); //Create our intra edges. Succeeds.
            std::cout << "Cluster: " << cluster << std::endl; //Prints the bounds of the cluster.
            std::cout << "Cluster has " << cluster.trans.size() << " transition pixels." << std::endl; //Prints the keys of the map jsut fine. 
            for (const auto& p : cluster.trans ) //For each member of the map
            {
                for(Edge ed : p.second) //For each edge from std::vector<Edge>
                {
                    std::cout << ed.type << " Start: " << ed.s << " End: "<< ed.e << std::endl;
                }
            }
        }

这可以正常打印:

Cluster has 6 transition pixels.
0 Start: {2,14} End: {2,15}
1 Start: {2,14} End: {5,14}
1 Start: {2,14} End: {14,0}
1 Start: {2,14} End: {14,6}
1 Start: {2,14} End: {14,8}
1 Start: {2,14} End: {14,14}
0 Start: {5,14} End: {5,15}
...

但是,在没有this-> CreateIntraEdges的情况下再次运行循环,我们得到以下信息:

Second run! 
Cluster: Min: {0,0} Max: {14,14}
Cluster has 6 transition pixels.
0 Start: {2,14} End: {2,15}
0 Start: {5,14} End: {5,15}
0 Start: {14,0} End: {15,0}
0 Start: {14,6} End: {15,6}
0 Start: {14,8} End: {15,8}
0 Start: {14,14} End: {15,14}
0 Start: {14,14} End: {14,15}
...

现在,如果您还想看到CreateIntraEdge,请继续:

    void CreateIntraEdges(Cluster& c)
    {
        for (const auto& p1 : c.trans ) //For each object in the map.
        {
            for (const auto& p2 : c.trans ) //For each object in the map
            {
                if(p1.first == p2.first)
                {
                    continue;
                }
                auto [path, weight] = this->GetPath(c, p1.first, p2.first); //See if the two edges can connect.
                if(path.size() > 0)
                {
                    Edge newedge;
                    newedge.Set(p1.first, p2.first, weight, Edge::INTRA);
                    c.trans[p1.first].push_back(newedge); //Push it back onto the std::vector<Edge> for the pixel p1.first
                }
            }
        }
    }

为什么循环不能再看到类型为1的边了?是因为汽车吗?

由于地图使用自定义结构,因此我尚未实现迭代器。这可能是问题的根源吗?

1 个答案:

答案 0 :(得分:5)

您创建许多对象的副本 ...

std::vector<Cluster> resClusters = this->GenerateClusters();

resCluster是副本,对其所做的更改将不会存储在this中(如果那里有矢量)。

相同
for(Cluster cluster : resClusters)

此处cluster是向量中元素的副本。循环迭代或结束后,对cluster的更改将丢失。

您可能想要引用,至少在循环中需要

for(Cluster& cluster : resClusters)