分段故障尝试解引用迭代器 [英] Segmentation fault trying to dereference an iterator
问题描述
我想使用std :: max_element找到一个向量的最大值。当我运行程序时,我得到一个分段错误,我相信是用std :: end超过向量的结束?我尝试将其更改为std :: end(tempdata)-1),但无效。
I'm trying to find the maximum value of a vector using std::max_element. When I run the program I get a segmentation fault that I believe is to do with std::end going over the end of the vector? I've tried changing it to std::end(tempdata)-1) but to no avail.
auto max = std::max_element(std::begin(tempdata), std::end(tempdata));
std::ofstream maxcurrent("maxcurrent.txt", std::ios::app);
maxcurrent << v << std::setw(15) << *max << std::endl;
查看了这个回答我不明白为什么我的工作不起作用。
Having looked at this answer I don't see why mine isn't working.
推荐答案
code> max 是 std :: end(tempdata)
,如果你的 tempdata
为空。
It won't work when max
is std::end(tempdata)
which can occurs if your tempdata
is empty.
迭代器到范围[first,last]中最大的元素。如果范围中的几个元素等同于最大元素,则将迭代器返回到第一个这样的元素。如果范围为空,则返回last。
Iterator to the greatest element in the range [first, last). If several elements in the range are equivalent to the greatest element, returns the iterator to the first such element. Returns last if the range is empty.
source
这篇关于分段故障尝试解引用迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!