vector下标出C 中的奇怪行为
vector subscript out of range error strange behavior in C++
我开始学习C ,所以我问题的答案对您来说可能很明显,但我真的很困惑。我预计该程序在访问范围内索引时会崩溃,但是该程序运行良好。这是代码。
#include <iostream>
#include <typeinfo>
#include <string>
#include <vector>
using namespace std;
int main() {
int x;
vector<int> v1;
while (cin >> x) {
v1.push_back(x);
}
cout << "out of bound: " << v1[10] << endl;
return 0;
}
这是输出:
1 2 3
out of bound: 0
v1中有3个元素,这意味着没有v1 [10],只有v1 [0]到v1 [2]。
行为并不奇怪。这就是应该发生的事情,因为std :: vector不会执行大小检查" []"。
所以,您基本上是从V1 [10]。
相关文章:
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- SFINAE 为什么我没有检测到 std::vector 的下标运算符?
- std::vector<std::vector<int>>:调试断言失败。C++矢量下标超出范围保留内存
- vector下标出C 中的奇怪行为
- 我的程序正在抛出一个错误,声称Vector下标超出了范围.(C )
- 调试断言失败:下标超出 std::vector 的范围
- 使用下标运算符进行Integer Vector元素比较失败,但使用at函数成功
- 重载类下标运算符以访问成员std::vector对象的元素
- Vector下标超出范围错误
- 在搜索字符串时,Vector下标超出了范围
- 运行时错误:vector下标超出范围
- 调试断言失败:c++ vector下标超出范围
- Vector下标超出c++的范围,无法找到错误的来源
- 对于原始数组和std::vector,明显没有对下标进行过一次结束的说明.这个问题已经得到了决定性的解决吗?
- c++中的vector下标超出了非空vector的范围
- Std::unordered_map vector下标超出范围
- error: Vector下标超出范围错误
- Vector下标添加元素失败
- 运行时vector断言失败表达式:vector下标超出范围
- 在vector的下标处插入对象