gpt4 book ai didi

c++ - 为什么 std::remove 不删除数组的最后一个元素

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:11:18 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
STL remove doesn’t work as expected?

抱歉,我是 C++11 和迭代器的新手。这应该删除数组中的所有数字 3,但不会删除最后一个。为什么?

#include <algorithm>
#include <array>
#include <iostream>

int main() {

std::array<int, 8> a{{9, 3, 4, 5, 33, 5, 6, 3}};

int N(3);

std::remove(a.begin(), a.end(), N);

for (int i : a) {
std::cout << i << '\n';
}

}

我得到的输出是:

{ 9, 4, 5, 33, 5, 6, 6, 3 }
^
|
// the last 3 is still there

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com