为什么我将矢量下标出范围超出范围
Why am I getting vector subscript out of range?
这是我代码的一部分,现在是其余部分的唯一活动部分,因为其余部分被评论出来,我正在尝试创建一个堆栈的堆栈向量然后创建一个新的堆栈。这是我第一次真正使用堆栈,所以我不知道我是正确声明了矢量还是其他。
,但更重要的是,每当我尝试通过for循环将项目推到堆栈上时,我会收到一个错误:vector subscript超出范围行:1234,我不确定我应该如何推到堆栈或有一种使用trains.push_back()的方法。(每次我尝试push_back,我都会遇到错误)。
#include <stack>
#include <iostream>
using namespace std;
int main()
{
vector<stack<int>> plates;
int numPlates,plateColor;
int x = 0;
cout << "Enter number of Plates" << endl;
cin >> numPlates;
for (int i = 0; i < numPlates; i++) {
if ((x + 1) % 10 == 0) // once plates are stacked ten high, start new stack
x++;
cin >> plateColor;
plates[x].push(plateColor);
}
您需要创建堆栈,填充它,然后使用emplace_back将其添加到向量中。这样的事情应该有效:
for ( int i = 0; i < numPlates; i += 10 )
{
stack<int> temp = stack<int>();
for ( int j = 0; j < 10 && j + i < numPlates; j++ );
{
int plateColor = 0;
cin >> plateColor;
temp.push( plateColor );
}
plates.emplace_back(temp);
}
当x为0时,或每当x增量时,您需要将新堆栈推入板上[x]
相关文章:
- 尝试通过多个向量访问变量时,向量下标超出范围
- 矢量下标超出SFML游戏中的范围
- 运行时错误:矢量下标超出范围:正在检查空集
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- 编译期间矢量下标超出范围
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 对矢量来说是全新的.矢量下标超出范围
- C++十进制到十六进制(字符串下标超出范围)
- 创建结构体向量,表达式:向量下标超出范围
- 读取矢量时矢量下标超出范围
- 断言失败错误,C++中的矢量下标超出范围问题
- 如何在 Linux 上强制执行矢量下标超出范围的调试断言
- 表达式:矢量下标超出范围
- 矢量下标超出范围,带有嵌套的 for 循环
- 向量下标超出向量向量的范围
- gcc8.2如何启用警告:数组下标在数组范围之上[-warray-bounds]
- 向量下标出的范围错误.即使向量的索引大于访问数据的索引,也会发生误差
- std::vector<std::vector<int>>:调试断言失败。C++矢量下标超出范围保留内存
- 重命名两个目录中的文件名,如果它们之间的某些字符匹配 - 矢量下标超出范围
- 行进立方体重建 - 向量下标超出范围