C++ 在地图上循环 - 消失的对象
C++ Looping over a map - disappearing objects
所以,我在代码中发现了一个相当奇怪的错误,我很难理解。我有一个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 的边缘?是因为汽车吗?
由于地图使用自定义结构,因此我尚未实现迭代器。这可能是问题的根源吗?
您创建了许多对象的副本...
std::vector<Cluster> resClusters = this->GenerateClusters();
这里resCluster
是一个副本,对它的更改不会存储在this
中(如果你那里有向量(。
与
for(Cluster cluster : resClusters)
这里cluster
是向量中元素的副本。循环迭代或结束后,对cluster
所做的更改将丢失。
你可能想要引用,至少对于循环:
for(Cluster& cluster : resClusters)
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 将地址分配给本地指针后,公共对象的变量将消失
- 应用投影矩阵后对象消失
- C++ 在地图上循环 - 消失的对象
- C++ GLUT 使对象在视口外消失
- 如何将指针分配给功能中的新对象,而不会使该对象在退出后消失
- 对象在另一个对象的变量中消失
- 字符串神秘地消失了,将字符串从C++带到对象C++
- 当在另一个较小的对象Opengl后面时,“大对象”将消失
- 创建一个新对象并将该对象存储在新类的向量中,属性消失
- openGl对象在特定位置消失
- OpenGL顶点着色器变换,对象消失
- QWebEngine & QWebChannel:传输对象"qt.webChannelTransport"在页面重新加载后消失
- 从共享对象符号表中消失"extern C"函数C++