【问题标题】:Stacking/concatenate 2D vectors in C++在 C++ 中堆叠/连接二维向量
【发布时间】:2015-11-06 17:49:21
【问题描述】:

我正在尝试堆叠/垂直连接二维向量。对于一维向量,我有这样的东西:

#include<iostream>
#include<vector>

using namespace std;

int main()
{
    vector< vector<int> > res;//(2,vector<int>(3,0.0));

    vector<int>a = {1,1,1};
    vector<int>b = {6,6,6};

    res.push_back(a);
    res.push_back(b);

    for(int i = 0; i < res.size(); i++)
    {

        for(int j = 0; j < res[0].size(); j++)
        {
            cout << res[i][j] << ",";
        }

        cout << endl;
    }

    return 0;
}

所以得到的二维向量(矩阵):

1, 1, 1,
6, 6, 6,

是向量 a 和 b 的堆叠/垂直连接版本。现在,我有 a 和 b 是 2D 向量而不是 1D 向量:

 vector< vector<int> >a = {{1,2,3},
                            {2,2,2}};

  vector< vector<int> >b = {{4,5,6},
                            {6,6,6}};

我将如何将它们堆叠成一个大小为 4 x 3 的结果矩阵:

1, 2, 3,
2, 2, 2,
4, 5, 6,
6, 6, 6,

因为,一个简单的 push_back() 是不行的。

【问题讨论】:

    标签: c++ vector stack 2d concatenation


    【解决方案1】:

    你的意思是以下吗?

    #include <iostream>
    #include <vector>
    
    int main()
    {
        std::vector<std::vector<int>> a = { { 1, 2, 3 }, { 2, 2, 2 } };
        std::vector<std::vector<int>> b = { { 4, 5, 6 }, { 6, 6, 6 } };
        std::vector<std::vector<int>> res;
    
        res = a;
        res.insert( res.end(), b.begin(), b.end() );
    
        for ( const auto &row : res )
        {
            for ( int x : row ) std::cout << x << ' ';
            std::cout << std::endl;
        }        
    }    
    

    程序输出是

    1 2 3 
    2 2 2 
    4 5 6 
    6 6 6 
    

    您也可以使用push_back。例如

    #include <iostream>
    #include <vector>
    #include <functional>
    
    int main()
    {
        std::vector<std::vector<int>> a = { { 1, 2, 3 }, { 2, 2, 2 } };
        std::vector<std::vector<int>> b = { { 4, 5, 6 }, { 6, 6, 6 } };
        std::vector<std::vector<int>> res;
        res.reserve( a.size() + b.size() );
    
        for ( auto &r : { std::cref( a ), std::cref( b ) } )
        {
            for ( const auto &row : r.get() ) res.push_back( row );
        }        
    
        for ( const auto &row : res )
        {
            for ( int x : row ) std::cout << x << ' ';
            std::cout << std::endl;
        }        
    }    
    

    输出和上面一样

    1 2 3 
    2 2 2 
    4 5 6 
    6 6 6 
    

    也可以这样写

    #include <iostream>
    #include <vector>
    #include <functional>
    
    int main()
    {
        std::vector<std::vector<int>> a = { { 1, 2, 3 }, { 2, 2, 2 } };
        std::vector<std::vector<int>> b = { { 4, 5, 6 }, { 6, 6, 6 } };
        std::vector<std::vector<int>> res;
        res.reserve( a.size() + b.size() );
    
        for ( auto &r : { std::cref( a ), std::cref( b ) } )
        {
            res.insert( res.end(), r.get().begin(), r.get().end() );    
        }        
    
        for ( const auto &row : res )
        {
            for ( int x : row ) std::cout << x << ' ';
            std::cout << std::endl;
        }        
    }    
    
    1 2 3 
    2 2 2 
    4 5 6 
    6 6 6 
    

    也就是说,有很多方法可以完成这项任务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 2019-09-23
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 2011-08-11
      相关资源
      最近更新 更多