gpt4 book ai didi

添加项目后,C++ find() 函数不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:41:54 25 4
gpt4 key购买 nike

<分区>

我想知道为什么“a.push_back(4)”会出现运行时错误。没有“a.push_back(4)”不会产生运行时错误。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void main()
{
vector<int> a(5);
a.push_back(1);
a.push_back(2);
a.push_back(3);

vector<int>::iterator begIt = begin(a);
vector<int>::iterator endIt = end(a);

a.push_back(4); // Once it is removed, this program will work well.

auto begIt2 = begin(a);
auto endIt2 = end(a);

auto findIt = find(begIt, endIt, 4);
if (findIt == endIt)
cout << "not found";
else
cout << *findIt;
}

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