简体   繁体   English

存储std :: deque的迭代器

[英]Storing Iterators of std::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. NodesList中的节点在其中包含一个全局ID。 However they are stored in NodesList, not according to this global ID but in descending order of their "similarity". 但是,它们不是根据此全局ID而是按其“相似性”的降序存储在NodesList中。 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]. 因此,稍后我想从NodesList中获取与全局ID(代码中的nborID)相对应的节点时,[REF:1]我通过“ pointerList”完成了该操作,在此之前我已存储了双端队列的迭代器,但要根据全局ID节点[REF:2]。 My pointerList stays true after the first erase command [REF: 3], but gets messed up in the next erase [REF: 4]. 在第一个擦除命令[REF:3]之后,我的pointerList保持为true,但是在下一个擦除[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. 恐怕您无法安全地存储指向存储在std::deque中的某些元素的迭代器,而在其他地方已对其进行了更改。

Some other relevant Q&A: 其他一些相关的问答:

If you want to create a vector of iterators then this is what you want to do: 如果要创建iteratorsvector ,则需要执行以下操作:

#include<iostream>
#include <deque>
#include <vector>
using namespace std;
int main()
{
    deque<int> dq { 1, 2, 3, 4, 5, 6, 7, 8 };
    deque<int>::iterator it;

    vector<deque<int>::iterator> vit;
    for (it = dq.begin(); it != dq.end(); it++)
    {
        vit.push_back(it);
        cout << *it;
    }

    //note that `it` is a pointer so if you modify the `block` it points in your deque then the value will change.
    //If you delete it then you will have a segmenant fault.
    //--- Don't Do: while (!dq.empty()) { dq.pop_back(); } ---//

    for (size_t i = 0; i < vit.size(); i++)
    {
        cout << *vit[i];
    }

    system("pause");
    return 0;
}

However, if you want to preserve that value of that iterator after it has been changed/deleted you may want to create a copy of each iterator and store the copy rather than the actual iterator 但是,如果要在更改/删除该iterator后保留该iterator值,则可能要创建每个iterator的副本并存储该副本,而不是实际的iterator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM