添加项[重复]后C ++ find()函数不起作用

问题描述 投票:0回答:1

这个问题在这里已有答案:

我想知道为什么“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;
}
c++ algorithm find
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.