简体   繁体   English

std :: vector distance函数如何提供比.size()高的值?

[英]How can it be that the std::vector distance Function gives a higher value than .size()?

I have the following code, where myFields is a std::set of pairs and currentSet is a std::vector of pairs, which has a very strange behavior: After a iteration where the condition is true, the size of currentSet gets increased as I expect from 1 to 2, but very strangely the distance in the for loop from the current iterator does not go from 1 to 2 as well as I expect, but suddenly to 6. This causes my code to crash after the next iteration, because there are not more than 2 elements in the vector, so it will try to read unallocated memory. 我有以下代码,其中myFields是对的std :: set,而currentSet是对的std :: vector,具有非常奇怪的行为:在条件为true的迭代之后, currentSet的大小会随着我期望从1到2,但是非常奇怪的是,for循环中当前迭代器的距离并没有像我期望的那样从1到2,而是突然变为6。这导致我的代码在下一次迭代后崩溃,因为向量中的元素不超过2个,因此它将尝试读取未分配的内存。

vector<pair<int,int>> currentSet;

currentSet.push_back( *(myFields.begin()) );
myFields.erase( myFields.begin() );

for ( auto iterator=currentSet.begin(); iterator != currentSet.end(); ++iterator ) {

    set<pair<int,int>>::iterator topright = myFields.find( pair<int,int>( i, j+1 ) );

    if ( topright != myFields.end() ) {
        cout << "size " << currentSet.size() << endl;    // gives 1
        cout << "dist to end " << distance(iterator, currentSet.end()) << endl;    // gives 1
        UniqueInsertion(currentSet, *topright, myFields );
        cout << "size " << currentSet.size() << endl;    // gives 2
        cout << "dist to end " << distance(iterator, currentSet.end()) << endl;    // gives 6!?


    }
}

Where UniqueInsertion is the following function: 其中, UniqueInsertion是以下函数:

void UniqueInsertion(vector<pair<int,int>> &vect, const pair<int,int> &elem, set<pair<int,int>> &fields) {

  if(find(vect.begin(), vect.end(), elem) == vect.end()) {
      vect.push_back(elem); 
      fields.erase(elem);
  }
}

This problem has only shown up when I changed the Sets from std::set to std::vector, which was necessary because I need back insertion for the loop to work this way. 仅当我将Sets从std :: set更改为std :: vector时才出现此问题,这是必需的,因为我需要向后插入才能使循环以这种方式工作。 I have not the slightest idea what causes this behavior and would be very grateful for explanation to how this happens, and of course what I can do to fix it. 我丝毫不知道是什么原因导致了这种行为,并且非常感谢您解释这种情况的发生方式,当然也可以解决该问题。

Cheers! 干杯!

Because you're use push_back on currentSet : 因为您在currentSet上使用push_back

If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated. 如果新的size()大于Capacity(),则所有迭代器和引用(包括过去的迭代器)都将失效。

In fact, std::vector stores its array contiguously in memory. 实际上, std::vector将其数组连续存储在内存中。

For that, it allocates an array of an arbitrary size (to avoid to reallocate at each push_back() ). 为此,它分配一个任意大小的数组(以避免在每个push_back()处重新分配)。
And when we have performed as many push_back() as the available allocated space, the std::vector reallocates a bigger array to be able to add the new element. 当我们执行了与可用分配空间一样多的push_back()std::vector将重新分配一个更大的数组以能够添加新元素。

The problem is here, as the std::vector stores its elements contiguously in memory, it needs to copy the already existing data in the new location (the bigger array). 问题就在这里,因为std::vector将其元素连续存储在内存中,它需要将已经存在的数据复制到新位置(更大的数组)。 Then, the old array is released. 然后,释放旧阵列。
Therefore, if you have an iterator pointing to an element of the vector, after the reallocation, the iterator becomes invalidated and points to a free memory. 因此,如果您有一个指向向量元素的迭代器,则在重新分配之后,该迭代器将变为无效并指向可用内存。

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

相关问题 std :: bitset如何比std :: vector更快 <bool> ? - How can std::bitset be faster than std::vector<bool>? 如何编写一个接受大小不同的本征矩阵的std :: vector的函数? - How can I write a function that accepts a std::vector of Eigen matrices of different size? 当具有相同值的多个值是 std::vector 的一部分时 std::distance 的计算如何工作 - How calculation of std::distance works when multiple values with same value are part of the std::vector 我怎么知道std :: vector分配的内存大小? - How can I know the allocated memory size of a std::vector? 如何将std :: vector的大小作为int? - How can I get the size of an std::vector as an int? 如何发现std :: vector的大小/长度(以字节为单位)? - How can I discover the size/length(in bytes) of a std::vector? C ++:向量大小错误并且大于元素数量 - C++: vector size is wrong and higher than the number of elements 我怎样才能知道向量的实际最大大小? (不使用 std::vector::max_size) - How can I know the real maximum size of a vector? (Not using std::vector::max_size) 如何获得从包含 std::function 的向量 std::vector 调用的函数的返回 - How can I get the return of a function called from a vector std::vector that contains std::function std :: vector :: size()比手动跟踪大小要慢吗? - Is std::vector::size() slower than keeping track of size manually?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM