双重向量超出范围

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

Double vector out of range

问题

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. using std::string;
  8. using std::vector;
  9. int main() {
  10. int n, x, y, z, xres, yres, zres;
  11. cin >> n;
  12. vector<vector<int>> vec(n);
  13. while (n--) {
  14. vector<int> aux(3);
  15. cin >> aux.at(0) >> aux.at(1) >> aux.at(2);
  16. vec.push_back(aux);
  17. }
  18. for (int i = 0; i < vec.size(); i++) {
  19. for (int j = 0; j < vec[i].size(); j++) {
  20. cout << vec.at(i).at(j) << " ";
  21. }
  22. cout << endl;
  23. }
  24. cout << vec.at(0).at(0);
  25. return 0;
  26. }

为什么for循环有效,但尝试直接访问元素会产生超出范围的错误,错误消息表示向量的大小为0?我认为for循环也只是将一些数字放在i和j的位置上。输入如下所示:

  1. 3
  2. 1 2 3
  3. 1 2 3
  4. 1 2 3
  1. terminate called after throwing an instance of 'std::out_of_range'
  2. what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
  3. Aborted (core dumped)

请注意,你的代码存在一个问题,它在创建vector<vector<int>> vec(n)后,又使用vec.push_back(aux)添加了多余的空向量,这可能导致问题。你应该改为使用vec[i] = aux来填充vec。这样,你的代码应该像下面这样:

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. using std::string;
  8. using std::vector;
  9. int main() {
  10. int n, x, y, z, xres, yres, zres;
  11. cin >> n;
  12. vector<vector<int>> vec(n);
  13. for (int i = 0; i < n; i++) {
  14. vector<int> aux(3);
  15. cin >> aux.at(0) >> aux.at(1) >> aux.at(2);
  16. vec[i] = aux;
  17. }
  18. for (int i = 0; i < vec.size(); i++) {
  19. for (int j = 0; j < vec[i].size(); j++) {
  20. cout << vec[i][j] << " ";
  21. }
  22. cout << endl;
  23. }
  24. cout << vec[0][0];
  25. return 0;
  26. }
英文:
  1. #include &lt;iostream&gt;
  2. #include &lt;string&gt;
  3. #include &lt;vector&gt;
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. using std::string;
  8. using std::vector;
  9. int main() {
  10. int n, x, y, z, xres, yres, zres;
  11. cin &gt;&gt; n;
  12. vector&lt;vector&lt;int&gt;&gt; vec(n);
  13. while(n--) {
  14. vector&lt;int&gt; aux(3);
  15. cin &gt;&gt; aux.at(0) &gt;&gt; aux.at(1) &gt;&gt; aux.at(2);
  16. vec.push_back(aux);
  17. }
  18. for ( int i = 0; i &lt; vec.size(); i++ ) {
  19. for (int j = 0; j &lt; vec[i].size(); j++) {
  20. cout &lt;&lt; vec.at(i).at(j) &lt;&lt; &quot; &quot;;
  21. }
  22. cout &lt;&lt; endl;
  23. }
  24. cout &lt;&lt; vec.at(0).at(0);
  25. return 0;
  26. }

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:

  1. 3
  2. 1 2 3
  3. 1 2 3
  4. 1 2 3
  1. terminate called after throwing an instance of &#39;std::out_of_range&#39;
  2. what(): vector::_M_range_check: __n (which is 0) &gt;= this-&gt;size() (which is 0)
  3. Aborted (core dumped)

答案1

得分: 2

你的向量最初大小为三,然后你再添加了三个项目。

尝试这个:

  1. vector<vector<int>> vec; // 初始大小为零
  2. while (n--) {
  3. vector<int> aux(3);
  4. cin >> aux.at(0) >> aux.at(1) >> aux.at(2);
  5. vec.push_back(aux); // 添加新项目
  6. }

或者这个:

  1. vector<vector<int>> vec(n); // 初始大小为3
  2. for (int i = 0; i < vec.size(); ++i) {
  3. vector<int> aux(3);
  4. cin >> aux.at(0) >> aux.at(1) >> aux.at(2);
  5. vec[i] = aux; // 覆盖现有项目
  6. }
英文:

Your vector initially has a size of three, and then you add three more items to it.

Try this

  1. vector&lt;vector&lt;int&gt;&gt; vec; // initial size zero
  2. while(n--) {
  3. vector&lt;int&gt; aux(3);
  4. cin &gt;&gt; aux.at(0) &gt;&gt; aux.at(1) &gt;&gt; aux.at(2);
  5. vec.push_back(aux); // add new item
  6. }

or this

  1. vector&lt;vector&lt;int&gt;&gt; vec(n); // initial size 3
  2. for (int i = 0; i &lt; vec.size(); ++i) {
  3. vector&lt;int&gt; aux(3);
  4. cin &gt;&gt; aux.at(0) &gt;&gt; aux.at(1) &gt;&gt; aux.at(2);
  5. vec[i] = aux; // overwrite existing item
  6. }

答案2

得分: 2

你创建的初始向量包含 n 个空向量:

  1. vector<vector<int>> vec(n);

稍后你推入了 n 个非空向量。此后,外部向量包含 2 * n 个向量,前 n 个向量是空的。

你应该使用:

  1. vector<vector<int>> vec;
  2. vec.reserve(n); // 分配必要的存储空间,但目前大小为 0
  3. while(n--)
  4. {
  5. vector<int> aux(3);
  6. cin >> aux.at(0) >> aux.at(1) >> aux.at(2);
  7. vec.push_back(std::move(aux)); // std::move 避免了拷贝
  8. }

或者

  1. vector<vector<int>> vec(n, std::vector<int>(3));
  2. // vec 现在完全分配了; 只需填充现有元素
  3. for (auto& v : vec)
  4. {
  5. std::cin >> v[0] >> v[1] >> v[2];
  6. }
英文:

The initial vector you create contains n empty vectors:

  1. 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

  1. vector&lt;vector&lt;int&gt;&gt; vec;
  2. vec.reserve(n); // allocate the necessary storage, but the size remains 0 for now
  3. while(n--)
  4. {
  5. vector&lt;int&gt; aux(3);
  6. cin &gt;&gt; aux.at(0) &gt;&gt; aux.at(1) &gt;&gt; aux.at(2);
  7. vec.push_back(std::move(aux)); // std::move avoids a copy here
  8. }

or

  1. vector&lt;vector&lt;int&gt;&gt; vec(n, std::vector&lt;int&gt;(3));
  2. // vec is completely allocated now; just fill existing elements
  3. for (auto&amp; v : vec)
  4. {
  5. std::cin &gt;&gt; v[0] &gt;&gt; v[1] &gt;&gt; v[2];
  6. }

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:

确定