【问题标题】:Assigning to a two dimensional vector in c++在c ++中分配给二维向量
【发布时间】:2017-12-26 23:54:31
【问题描述】:
#include<bits/stdc++.h>
using namespace std;
main()
{
    vector<vector<int> > v;
    for(int i = 0;i < 3;i++)
    {
        vector<int> temp;
        for(int j = 0;j < 3;j++)
        {
            temp.push_back(j);
        }
        //cout<<typeid(temp).name()<<endl;
        v[i].push_back(temp);
    }
 }

我正在尝试分配给二维向量。我收到以下错误

No matching function for call to 
std ::vector<int>::push_back(std::vector<int> &)

【问题讨论】:

  • v.push_back(...)
  • v[i] 访问越界。该向量没有条目
  • v.push_back(vector)v[i].push_back(int)
  • “2D 向量”只是一个向量。所以你需要弄清楚如何将元素添加到向量中。就是这样。
  • 提示,如果你使用的是c++11,你可以使用emplace_back

标签: c++ 2d-vector


【解决方案1】:

问题:你的向量v是空的,你不能访问v[i]而不在v中推送任何向量。

解决方案:将声明v[i].push_back(temp);替换为v.push_back(temp);

【讨论】:

    【解决方案2】:

    你可以按照这个过程:

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        vector<vector<int> > v;
        for(int i = 0;i < 3;i++)
        {
            vector<int> temp;
            for(int j = 0;j < 3;j++)
            {
                temp.push_back(j);
    
            }
            //cout<<typeid(temp).name()<<endl;
            v.push_back(temp);
        }
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                cout << v[i][j] << " ";
            }
            cout << endl;
        }
     }
    

    【讨论】:

      【解决方案3】:

      v[0] 为空,你应该使用v.push_back(temp);

      您可以使用at 方法来避免此错误:

      for(int i = 0; i < 3; i++){
         vector <vector <int> > v;
         vector <int> temp;
         v.push_back(temp);
         v.at(COLUMN).push_back(i);
      }
      

      然后您可以通过以下方式访问它:

      v.at(COLUMN).at(ROWS) = value;
      

      【讨论】:

        【解决方案4】:
        v[i].push_back(temp);
        

        应该是

        v.push_back(temp);
        

        vstd::vector&lt;vector&lt;int&gt;&gt; 的类型,v[i]std::vector&lt;int&gt; 的类型

        【讨论】:

          【解决方案5】:

          您应该使用v 而不是v[i]。 (C++11)

          #include <iostream>
          #include <vector>
          
          using namespace std;
          
          int main(int argc, char* argv[])
          {
              vector<vector<int> > v;
              for(int i = 0;i < 3;i++)
              {
                  vector<int> temp;
                  for(int j = 0;j < 3;j++)
                  {
                      temp.push_back(j);
                  }
          
                  v.push_back(temp);
              }
          
              for (auto element: v) {
                  for (auto atom: element) {
                      cout << atom << " ";
                  }
                  cout << "\n";
              }
          
              return 0;
          }
          

          【讨论】:

            【解决方案6】:

            这样想:“如何将T 类型的变量temp 插入到std::vector&lt;T&gt; 的向量中?”你的情况是:

            v.push_back(temp);
            

            T 本身是一个向量并没有什么区别。然后打印出你的向量(向量),你可以使用两个 for 循环:

            for (std::size_t i = 0; i < v.size(); i++){
                for (std::size_t j = 0; j < v[i].size(); j++){
                    std::cout << v[i][j] << ' ';
                }
                std::cout << std::endl;
            }
            

            【讨论】:

              【解决方案7】:

              去做吧……

              #include<bits/stdc++.h>
              using namespace std;
              int main()
              {
                  vector<vector<int> > v;
                  for(int i = 0; i < 3; i++)
                  {
                      vector<int> temp;
                      for(int j = 0; j < 3; j++)
                      {
                          temp.push_back(j);
                      }
                      v.push_back(temp);//use v instead of v[i];
                  }
              }
              

              【讨论】:

                猜你喜欢
                • 2015-06-10
                • 1970-01-01
                • 2016-08-11
                • 1970-01-01
                • 2020-06-29
                • 2023-03-10
                • 2020-03-28
                • 2013-10-29
                • 1970-01-01
                相关资源
                最近更新 更多