【发布时间】:2014-02-03 22:12:15
【问题描述】:
我正在尝试使用 std::max_element 找到向量的最大值。当我运行程序时,我得到一个分段错误,我认为这与 std::end 越过向量的末尾有关?我尝试将其更改为 std::end(tempdata)-1) 但无济于事。
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;
看了this answer 我不明白为什么我的不工作。
【问题讨论】:
-
哪条线段错误?
std::max_element()调用(如问题文本所示)或*max取消引用(如问题标题所示)?tempdata是非空的吗? -
您是否在空向量上搜索最大元素?
-
@Angew 这是段错误的 *max 行。