[英]c++ stl vector iterator insert segmentation fault
std::vector<struct::event>::iterator it;
std::vector<struct::event>::iterator last=myvector.end();
for (it=myvector.begin(); it<=last; it++){
if(mysignal.declination<(*last).declination){
if (mysignal.declination>=(*it).declination && mysignal.declination<(*(it+1)).declination){
myvector.insert(it+1, mysignal);
break;
}
}
if (mysignal.declination>=(*last).declination){
myvector.push_back(mysignal);
break;
}
}
I have a vector called myvector with events that are sorted with the declination. 我有一个名为myvector的向量,其中包含使用偏角排序的事件。 now I want to add mysignal to this vector on the right place.
现在我想在正确的位置添加mysignal到这个向量。 but i always get a seg fault after a few events which refers to: if(mysignal.declination<(*last).declination).
但在一些事件之后我总是得到一个段错误:if(mysignal.declination <(* last).declination)。 I just can't see what is wrong.
我只是看不出有什么问题。
Your loop is wrong, read the docs : 你的循环错了,请阅读文档 :
Returns an iterator to the element following the last element of the container.
返回容器的最后一个元素后面的元素的迭代器。 This element acts as a placeholder;
该元素充当占位符; attempting to access it results in undefined behavior.
尝试访问它会导致未定义的行为。
You can't dereference end()
, it provides a way of knowing that you have overrun the container, so your loop condition should be it != myvector.end()
, and last
is wrong as well. 你不能取消引用
end()
,它提供了一种知道你已经超出容器的方法,所以你的循环条件应该是it != myvector.end()
, last
也是错误的。
end() does not refer to the last element in the container, you need to change your condition as follows. end()不引用容器中的最后一个元素,您需要按如下方式更改条件。
for (it=myvector.begin(); it != last; it++){
You have other broken logic as well that is dereferencing last that you need to fix. 您还有其他破坏的逻辑,这是您需要修复的最后解除引用。
As others have said, C++ iterators define a half-open interval ( '[begin()...end())'
), which is what you should probably be using in most other cases as well. 正如其他人所说,C ++迭代器定义了半开区间(
'[begin()...end())'
),这也是你在其他大多数情况下应该使用的区间。 And although it works with iterators from a vector, in general, iterators do not support <=
(nor <
); 虽然它适用于vector中的迭代器,但通常迭代器不支持
<=
(nor <
); the standard idiom for a loop is: 循环的标准习语是:
for ( auto current = container.begin();
current != container.end();
++ current ) ...
(In the most likely case that you cannot count on C++11, you'll have to write out the full iterator type, rather than use auto
. Although auto
is one of the few things from C++11 that seems to work with VC++11 and with recent versions of g++, so if those are the only targets you're concerned with, and you can be sure of always having very recent versions, you can use it.) (在最可能的情况下,你不能指望C ++ 11,你必须写出完整的迭代器类型,而不是使用
auto
。虽然auto
是C ++ 11中为数不多的东西之一似乎可以工作使用VC ++ 11 和最新版本的g ++,如果这些是您唯一关注的目标,并且您可以确定始终拥有最新版本,则可以使用它。)
Also, if you want to access the last element of the vector in the loop, myvector.back()
will return a reference to it. 此外,如果要在循环中访问向量的最后一个元素,
myvector.back()
将返回对它的引用。 ( myvector.back()
is undefined behavior if the vector is empty, but if the vector is empty, you won't enter the loop.) (如果向量为空,则
myvector.back()
是未定义的行为,但如果向量为空,则不会进入循环。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.