英文:
Double vector out of range
问题
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main() {
int n, x, y, z, xres, yres, zres;
cin >> n;
vector<vector<int>> vec(n);
while (n--) {
vector<int> aux(3);
cin >> aux.at(0) >> aux.at(1) >> aux.at(2);
vec.push_back(aux);
}
for (int i = 0; i < vec.size(); i++) {
for (int j = 0; j < vec[i].size(); j++) {
cout << vec.at(i).at(j) << " ";
}
cout << endl;
}
cout << vec.at(0).at(0);
return 0;
}
为什么for
循环有效,但尝试直接访问元素会产生超出范围的错误,错误消息表示向量的大小为0?我认为for
循环也只是将一些数字放在i和j的位置上。输入如下所示:
3
1 2 3
1 2 3
1 2 3
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
Aborted (core dumped)
请注意,你的代码存在一个问题,它在创建vector<vector<int>> vec(n)
后,又使用vec.push_back(aux)
添加了多余的空向量,这可能导致问题。你应该改为使用vec[i] = aux
来填充vec
。这样,你的代码应该像下面这样:
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main() {
int n, x, y, z, xres, yres, zres;
cin >> n;
vector<vector<int>> vec(n);
for (int i = 0; i < n; i++) {
vector<int> aux(3);
cin >> aux.at(0) >> aux.at(1) >> aux.at(2);
vec[i] = aux;
}
for (int i = 0; i < vec.size(); i++) {
for (int j = 0; j < vec[i].size(); j++) {
cout << vec[i][j] << " ";
}
cout << endl;
}
cout << vec[0][0];
return 0;
}
英文:
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main() {
int n, x, y, z, xres, yres, zres;
cin >> n;
vector<vector<int>> vec(n);
while(n--) {
vector<int> aux(3);
cin >> aux.at(0) >> aux.at(1) >> aux.at(2);
vec.push_back(aux);
}
for ( int i = 0; i < vec.size(); i++ ) {
for (int j = 0; j < vec[i].size(); j++) {
cout << vec.at(i).at(j) << " ";
}
cout << endl;
}
cout << vec.at(0).at(0);
return 0;
}
Why do the for loops work but trying to access an element directly produces an out of range error which says that the vector is of size 0? I would think that the for loops also only put some numbers in place o i and j. The input is like this:
3
1 2 3
1 2 3
1 2 3
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
Aborted (core dumped)
答案1
得分: 2
你的向量最初大小为三,然后你再添加了三个项目。
尝试这个:
vector<vector<int>> vec; // 初始大小为零
while (n--) {
vector<int> aux(3);
cin >> aux.at(0) >> aux.at(1) >> aux.at(2);
vec.push_back(aux); // 添加新项目
}
或者这个:
vector<vector<int>> vec(n); // 初始大小为3
for (int i = 0; i < vec.size(); ++i) {
vector<int> aux(3);
cin >> aux.at(0) >> aux.at(1) >> aux.at(2);
vec[i] = aux; // 覆盖现有项目
}
英文:
Your vector initially has a size of three, and then you add three more items to it.
Try this
vector<vector<int>> vec; // initial size zero
while(n--) {
vector<int> aux(3);
cin >> aux.at(0) >> aux.at(1) >> aux.at(2);
vec.push_back(aux); // add new item
}
or this
vector<vector<int>> vec(n); // initial size 3
for (int i = 0; i < vec.size(); ++i) {
vector<int> aux(3);
cin >> aux.at(0) >> aux.at(1) >> aux.at(2);
vec[i] = aux; // overwrite existing item
}
答案2
得分: 2
你创建的初始向量包含 n
个空向量:
vector<vector<int>> vec(n);
稍后你推入了 n
个非空向量。此后,外部向量包含 2 * n
个向量,前 n
个向量是空的。
你应该使用:
vector<vector<int>> vec;
vec.reserve(n); // 分配必要的存储空间,但目前大小为 0
while(n--)
{
vector<int> aux(3);
cin >> aux.at(0) >> aux.at(1) >> aux.at(2);
vec.push_back(std::move(aux)); // std::move 避免了拷贝
}
或者
vector<vector<int>> vec(n, std::vector<int>(3));
// vec 现在完全分配了; 只需填充现有元素
for (auto& v : vec)
{
std::cin >> v[0] >> v[1] >> v[2];
}
英文:
The initial vector you create contains n
empty vectors:
vector<vector<int>> vec(n);
Later you push n
nonempty vectors. After this the outer vector contains 2 * n
vectors and the first n
of these are empty.
You should use
vector<vector<int>> vec;
vec.reserve(n); // allocate the necessary storage, but the size remains 0 for now
while(n--)
{
vector<int> aux(3);
cin >> aux.at(0) >> aux.at(1) >> aux.at(2);
vec.push_back(std::move(aux)); // std::move avoids a copy here
}
or
vector<vector<int>> vec(n, std::vector<int>(3));
// vec is completely allocated now; just fill existing elements
for (auto& v : vec)
{
std::cin >> v[0] >> v[1] >> v[2];
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论