存储std :: deque的迭代器 [英] Storing Iterators of std::deque
问题描述
我试图在一个向量中存储一个deque的迭代器,并希望将它们保存在向量中,即使我已经从deque中删除或插入一些元素。这是可能吗?
我有以下代码:
typedef struct {
int id;
int seedId;
double similarity;
} NODE_SEED_SIM;
typedef std :: deque< NODE_SEED_SIM> NodesQueue;
typedef std :: deque< NODE_SEED_SIM> :: iterator ITRTR;
typedef std :: vector< const ITRTR> PointerVec;
void growSegments(CSG_PointCloud * pResult,IntMatrix * WdIndices,NodesQueue * NodesList,IntMatrix * Segments){
ITRTR nodeslistStart =(* NodesList).begin
int pointCount =(* WdIndices).size();
int nodeslistSize =(* NodesList).size();
IntVector书(pointCount);
PointerVec pointerList(pointCount); //向量ITRTRs
for(int i = 0; i
book [(* NodesList)[i] .id] = 1 ;
pointerList [(* NodesList)[i] .id] = nodeslistStart + i; // REF:2
}
while(nodeslistSize> 0){
int i = 0;
int returnCode = 0;
int nodeId =(* NodesList)[i] .id;
int seedId =(* NodesList)[i] .seedId;
int n_nbrOfNode =(* WdIndices)[nodeId] .size();
(* Segments)[seedId] .push_back(nodeId);
(* NodesList).erase((* NodesList).begin()); // REF:3;这个擦除不会弄乱pointerList
nodeslistSize - ;
点节点;
/ *
获取节点的属性
* /
for(int j = 0; j
int nborId =(* WdIndices)[nodeId] [j];
if(nborId == seedId)
continue;
点邻居;
/ *
获取邻域的属性
* /
double node_nbor_sim = computeSimilarity(neighbor,node);
if(book [nborId] == 1){
ITRTR curr_node = pointerList [nborId]; // REF:1
if(curr_node - > similarity< node_nbor_sim){
curr_node - > similarity = node_nbor_sim;
NODE_SEED_SIM temp = * curr_node;
(* NodesList).erase(curr_node); // REF:4;这个擦除完全混乱了pointerList
returnCode = insertSortNodesList(& temp,NodesList,-1);
}
}
}
}
}
NodesList中的节点在其中保存全局ID。然而,它们存储在NodesList中,而不是根据这个全局ID,而是按照它们的相似性的降序。所以后来当我想从NodesList对应一个全局ID(代码中的nborID)[REF:1],我通过pointerList,其中我已经存储了deque的迭代器,但根据全局ID的节点[REF:2]。我的pointerList在第一个擦除命令[REF:3]之后保持为真,但在下一次擦除时被弄乱[REF:4]。
这里有什么问题?
感谢
我试图在一个向量中存储deque的迭代器,想要保留他们内的矢量,即使我已经删除或插入一些元素从或到deque。这是否可能?
从
I am trying to store iterators of a deque in a vector and want to preserve them inside the vector even when I have erased or inserted some elements from or into the deque. Is this possible?
I have the following code:
typedef struct {
int id;
int seedId;
double similarity;
} NODE_SEED_SIM;
typedef std::deque<NODE_SEED_SIM> NodesQueue;
typedef std::deque<NODE_SEED_SIM>::iterator ITRTR;
typedef std::vector<const ITRTR> PointerVec;
void growSegments (CSG_PointCloud *pResult, IntMatrix *WdIndices, NodesQueue *NodesList, IntMatrix *Segments) {
ITRTR nodeslistStart = (*NodesList).begin();
int pointCount = (*WdIndices).size();
int nodeslistSize = (*NodesList).size();
IntVector book(pointCount);
PointerVec pointerList (pointCount); // Vector of ITRTRs
for (int i = 0; i < nodeslistSize; i++) {
book [ (*NodesList)[i].id ] = 1;
pointerList [ (*NodesList)[i].id ] = nodeslistStart + i; // REF: 2
}
while (nodeslistSize > 0) {
int i = 0;
int returnCode = 0;
int nodeId = (*NodesList)[i].id;
int seedId = (*NodesList)[i].seedId;
int n_nbrOfNode = (*WdIndices)[ nodeId ].size();
(*Segments)[ seedId ].push_back ( nodeId );
(*NodesList).erase ( (*NodesList).begin() ); // REF: 3; This erase DOES NOT mess the pointerList
nodeslistSize --;
Point node;
/*
GET ATTRIBUTES OF NODE
*/
for (int j = 0; j < n_nbrOfNode; j++) {
int nborId = (*WdIndices)[nodeId][j];
if (nborId == seedId)
continue;
Point neighbor;
/*
GET ATTRIBUTES OF NEIGHBOUR
*/
double node_nbor_sim = computeSimilarity (neighbor, node);
if (book[nborId] == 1) {
ITRTR curr_node = pointerList[nborId]; // REF: 1
if ( curr_node -> similarity < node_nbor_sim) {
curr_node -> similarity = node_nbor_sim;
NODE_SEED_SIM temp = *curr_node;
(*NodesList).erase (curr_node); // REF: 4; This erase completely messes up the pointerList
returnCode = insertSortNodesList (&temp, NodesList, -1);
}
}
}
}
}
The nodes in the NodesList hold a global ID inside them. However they are stored in NodesList, not according to this global ID but in descending order of their "similarity". So later when I want to get the node from NodesList corresponding to a global ID (nborID in code)[REF: 1] I do it via the "pointerList" where I have previously stored the iterators of the deque but according to the global IDs of the nodes [REF: 2]. My pointerList stays true after the first erase command [REF: 3], but gets messed up in the next erase [REF: 4].
What is wrong here? Thanks
I am trying to store iterators of a deque in a vector and want to preserve them inside the vector even when I have erased or inserted some elements from or into the deque. Is this possible?
As from the documentation it says
Sorry I'm posting this as an image here, but the formatting is too tedious to replicate in markup!
So the short answer is: NO! I'm afraid you cannot safely store iterators pointing to certain elements stored in a std::deque
, while it's changed elsewhere.
Some other relevant Q&A:
- Problem with invalidation of STL iterators when calling erase
- C++ deque: when iterators are invalidated
这篇关于存储std :: deque的迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!