双重向量超出范围

huangapple go评论71阅读模式
英文:

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 &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;


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 &gt;&gt; n;
    vector&lt;vector&lt;int&gt;&gt; vec(n);

    while(n--) {
        vector&lt;int&gt; aux(3);
        cin &gt;&gt; aux.at(0) &gt;&gt; aux.at(1) &gt;&gt; aux.at(2);
        vec.push_back(aux);

    }
    for ( int i = 0; i &lt; vec.size(); i++ ) {
        for (int j = 0; j &lt; vec[i].size(); j++) {
            cout &lt;&lt; vec.at(i).at(j) &lt;&lt; &quot; &quot;;
            }
        cout &lt;&lt; endl;
    }
    cout &lt;&lt; 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 &#39;std::out_of_range&#39;
  what():  vector::_M_range_check: __n (which is 0) &gt;= this-&gt;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&lt;vector&lt;int&gt;&gt; vec; // initial size zero

while(n--) {
    vector&lt;int&gt; aux(3);
    cin &gt;&gt; aux.at(0) &gt;&gt; aux.at(1) &gt;&gt; aux.at(2);
    vec.push_back(aux); // add new item

}

or this

vector&lt;vector&lt;int&gt;&gt; vec(n); // initial size 3

for (int i = 0; i &lt; vec.size(); ++i) {
    vector&lt;int&gt; aux(3);
    cin &gt;&gt; aux.at(0) &gt;&gt; aux.at(1) &gt;&gt; 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&lt;vector&lt;int&gt;&gt; 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&lt;vector&lt;int&gt;&gt; vec;
vec.reserve(n); // allocate the necessary storage, but the size remains 0 for now
while(n--)
{
    vector&lt;int&gt; aux(3);
    cin &gt;&gt; aux.at(0) &gt;&gt; aux.at(1) &gt;&gt; aux.at(2);
    vec.push_back(std::move(aux)); // std::move avoids a copy here
}

or

vector&lt;vector&lt;int&gt;&gt; vec(n, std::vector&lt;int&gt;(3));

// vec is completely allocated now; just fill existing elements
for (auto&amp; v : vec)
{
    std::cin &gt;&gt; v[0] &gt;&gt; v[1] &gt;&gt; v[2];
}

huangapple
  • 本文由 发表于 2023年5月28日 20:45:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定