【问题标题】:Initialization of a vector of vectors?向量向量的初始化?
【发布时间】:2011-03-04 22:12:48
【问题描述】:

有没有办法以与初始化矩阵相同的、快速的方式初始化向量向量?

typedef int type;

type matrix[2][2]=
{
{1,0},{0,1}
};

vector<vector<type> > vectorMatrix;  //???

【问题讨论】:

    标签: c++ stl vector matrix


    【解决方案1】:

    对于单个向量,您可以使用以下内容:

    typedef int type;
    type elements[] = {0,1,2,3,4,5,6,7,8,9};
    vector<int> vec(elements, elements + sizeof(elements) / sizeof(type) );
    

    基于此,您可以使用以下内容:

    type matrix[2][2]=
    {
       {1,0},{0,1}
    };
    
    vector<int> row_0_vec(matrix[0], matrix[0] + sizeof(matrix[0]) / sizeof(type) );
    
    vector<int> row_1_vec(matrix[1], matrix[1] + sizeof(matrix[1]) / sizeof(type) );
    
    vector<vector<type> > vectorMatrix;
    vectorMatrix.push_back(row_0_vec);
    vectorMatrix.push_back(row_1_vec);
    

    c++0x 中,您可以像初始化数组一样初始化标准容器。

    【讨论】:

      【解决方案2】:
      std::vector<std::vector<int>> vector_of_vectors;
      

      那么如果你想添加,你可以使用这个程序:

      vector_of_vectors.resize(#rows); //just changed the number of rows in the vector
      vector_of_vectors[row#].push_back(someInt); //this adds a column
      

      或者你可以这样做:

      std::vector<int> myRow;
      myRow.push_back(someInt);
      vector_of_vectors.push_back(myRow);
      

      所以,在你的情况下,你应该可以说:

      vector_of_vectors.resize(2);
      vector_of_vectors[0].resize(2);
      vector_of_vectors[1].resize(2);
      for(int i=0; i < 2; i++)
       for(int j=0; j < 2; j++)
         vector_of_vectors[i][j] = yourInt;
      

      【讨论】:

        【解决方案3】:

        在 C++0x 中,我认为您可以使用与 matrix 相同的语法。

        在 C++03 中,您必须编写一些繁琐的代码来填充它。 Boost.Assign 可能能够稍微简化它,使用类似以下未经测试的代码:

        #include <boost/assign/std/vector.hpp>
        
        vector<vector<type> > v;
        v += list_of(1)(0), list_of(0)(1);
        

        甚至

        vector<vector<type> > v = list_of(list_of(1)(0))(list_of(0)(1));
        

        【讨论】:

        • 我试过了,但它似乎不起作用。我有一个vector&lt;vector&lt;int&gt;,我想将变量的第一个元素初始化为一个,这样我就有一个只有一个元素向量初始化为一个的向量。提前致谢。
        • @tuxworker:在 C++11 中,这是 vector&lt;vector&lt;int&gt;&gt; v {{1}};。使用 C++03 中的 Boost.Assign,类似于 vector&lt;vector&lt;int&gt; &gt; v = list_of(list_of(1));。如果这不起作用,请提出一个新问题,说明您正在尝试什么并描述问题所在。
        • 确实我尝试了vector&lt;vector&lt;int&gt; &gt; v(10); v+= list_of(list_of(1));vector&lt;vector&lt;int&gt; &gt; v(10); v = list_of(list_of(1));,但我遇到了编译错误。我认为另外使用重复会更好,但我不知道如何。我会问一个新问题。谢谢
        【解决方案4】:

        如果矩阵被完全填满-

        vector< vector<int> > TwoDVec(ROWS, vector<int>(COLS));
        //Returns a matrix of dimensions ROWS*COLS with all elements as 0
        //Initialize as -
        TwoDVec[0][0] = 0;
        TwoDVec[0][1] = 1;
        ..
        .
        

        更新:我发现有更好的方法here

        否则,如果每行中有可变数量的元素(不是矩阵)-

        vector< vector<int> > TwoDVec(ROWS);
        for(int i=0; i<ROWS; i++){
            while(there_are_elements_in_row[i]){           //pseudocode
                TwoDVec[i].push_back(element);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2011-05-18
          • 1970-01-01
          • 2018-09-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-20
          • 1970-01-01
          相关资源
          最近更新 更多