添加项目后C find()函数不起作用
C++ find() function does not work after adding items
我想知道为什么" 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;
}
push_back(4)使您的迭代器无效。这就是为什么此代码产生错误的原因。
相关文章:
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- 为什么我的数组双精度函数不起作用?
- std::函数不起作用,但普通的旧函数指针可以 - 为什么?
- C++ Arduino - 随机函数不起作用
- 为什么我的数组或函数不起作用?
- 从类中的另一个文件请求函数不起作用
- 双链表堆栈删除函数不起作用
- 将 cmake 代码段转换为函数 - 不起作用
- 为什么我的从base64解码的函数不起作用?
- 为什么当我们使用等于'='符号比较器函数时,c ++的内置排序函数不起作用?
- swap(int&, int&) 函数不起作用,当交换不使用临时变量时?
- 子类化 STL 容器:范围构造函数不起作用
- 带有此指针的模板类多重继承构造函数不起作用?
- 以下打印树函数不起作用为什么?
- 为什么内存集函数不起作用?
- 调用填充我的主体数组的函数不起作用
- 带有自定义数字的阶乘函数不起作用
- 如何QDialog raise()函数不起作用
- Lambda 表达式闭包函数不起作用
- C++:在没有 lvalue 的情况下调用时复制构造函数不起作用