[英]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.