简体   繁体   English

包含迭代器到向量的无序映射-迭代器不可解除引用的C ++

[英]Unordered map containing an Iterator to a Vector - Iterator not Dereferencable C++

I have an unordered map that stores a string as its key and an iterator to a spot in a vector as its data. 我有一个无序映射,将字符串作为键存储,将迭代器存储到向量中的点作为数据。 Each element in the vector holds a string and an int (number of times the string appears). 向量中的每个元素都包含一个字符串和一个int(字符串出现的次数)。 I have coded an increaseCount(std::string, int) function that is supposed to insert the new string into the unordered map, unless it is already within the container. 我已经编写了一个递增计数(std :: string,int)函数,该函数应该将新字符串插入无序映射,除非它已经在容器中。 If this is the case, the function should find the key in the unordered map, got to the corresponding location in the vector that the iterator points to, and add one to the int parameter of the vector element. 如果是这种情况,该函数应在无序映射中找到键,到达迭代器指向的向量中的对应位置,并向向量元素的int参数添加一个。 However, when executing the second case, the error "Vector iterator not dereferencable" shows up. 但是,在执行第二种情况时,将显示错误“不可迭代向量迭代器”。 Here is what I have coded. 这是我编写的代码。

void ourTrends::increaseCount(std::string s, unsigned int amount){
// check to see if key is already in
if(wordStoreTable.find(s) == wordStoreTable.end()){
    // add the element into the hash table
    std::vector<std::pair<std::string, int>>::iterator it;
    std::pair<std::string, std::vector<std::pair<std::string, int>>::iterator> word (s, it);
    wordStoreTable.insert(word);

    // add element to back of vector
    std::pair<std::string, int> p1 (s, amount);
    sortedVector.push_back(p1);
    //std::swap(sortedVector.front(), sortedVector.back());
    // set the iterator of the hash pair to the end of the current vector size
    it = sortedVector.end();
    --it;
    wordStoreTable.find(s)->second = it;
    isSorted = false;

} else{
    int x = wordStoreTable.find(s)->second->second;
    std::pair<std::string, int> p1 (s, x + amount);
    sortedVector.erase(wordStoreTable.find(s)->second);
    sortedVector.push_back(p1);
    //std::swap(sortedVector.begin(), sortedVector.end());
    std::vector<std::pair<std::string, int>>::iterator it = sortedVector.end();
    --it;
    wordStoreTable.find(s)->second = it;
    std::cout << wordStoreTable.find(s)->first << std::endl;

}

} }

I know that this means the iterator is pointing to an empty location in memory, but I cannot figure out where it loses track of its destination. 我知道这意味着迭代器指向内存中的空位置,但是我无法弄清楚它失去了目的地的位置。

The reason this code doesn't work, is that vector::push_back invalidates the iterators, that is, an iterator you had for a vector of size 3, might not work if you make the vector larger by adding a new element. 该代码不起作用的原因是vector :: push_back使迭代器无效,也就是说,如果通过添加新元素使向量变大,则对于大小为3的向量的迭代器可能无法工作。 From cppreference: If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated. 来自cppreference:如果新的size()大于Capacity(),则所有迭代器和引用(包括过去的迭代器)均无效。 Otherwise only the past-the-end iterator is invalidated. 否则,只有过去的迭代器是无效的。

You certainly could reserve enough space for the vector ahead of time, so that iterators do not invalidate, but as a general rule, you're better off with using numerical indices. 您当然可以提前为向量保留足够的空间,以使迭代器不会无效,但是作为一般规则,最好使用数字索引。

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

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