tarmizi
tarmizi

Reputation: 173

Stacking/concatenate 2D vectors in C++

I am trying to stack/vertical concatenate 2D vectors. For 1D vectors, I have something like this:

#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;
}

So the resulting 2D vector (matrix):

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

is a stacked/vertically concatenated version of vectors a and b. Now, I have a and b that are 2D vectors instead of 1D vectors:

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

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

How would I go about stacking them into one resulting matrix of size 4 x 3:

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

Because, a simple push_back() will not do.

Upvotes: 1

Views: 4001

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310940

Do you mean the following?

#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;
    }        
}    

The program output is

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

Also you can use push_back. For example

#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;
    }        
}    

The output will be the same as above

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

Or you can write the following way

#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 

That is there are many approaches to do the task.

Upvotes: 1

Related Questions