0

I have been using vectors only lately and found them extremely handy when compared normal array declarations. however, I have dealt with only 1D operations as following:-

      vector<int>X;
      X.push_back(value/variable);

How Do I perform similar operations for a 2D vector equivalent and dynamically update values, row by row? Please help me here and my apologies if the question is rudimentary. Also it would be really helpful if you can show me a link where all operations of vectors are provided as tutorials. I tried few sites, but they are bits all over. Please help me.

2 Answers 2

3

One more way to work with 2D-arrays is the using of one vector with 2D-addressing in your own class.

Here an example:

#include <iostream>
#include <vector>

class array_2d
{
    public:
        array_2d( 
            unsigned int x, 
            unsigned int y ) 
        : 
            m_size_x( x ), 
            m_size_y( y ),
            // resize vector to size x*y, all elements are 0.
            m_data( x*y, 0 )
        {}

        int
        get( unsigned int x, unsigned int y ) const
        {
            return m_data[ x + y * m_size_y ];
        }

        void
        set( unsigned int x, unsigned int y, int data )
        {
            m_data[ x + y * m_size_y ] = data;
        }

    private:
        unsigned int m_size_x;
        unsigned int m_size_y;

        std::vector <int> m_data;
};

int main()
{
    // 2D array 2x3.
    array_2d m( 2, 3 );

    // Set 2 cells into 1 and 3.
    m.set( 1, 1, 1 );
    m.set( 2, 0, 3 );

    for( unsigned int i = 0; i < 3; ++i )
    {
        for( unsigned int j = 0; j < 2; ++j )
            std::cout << m.get( i, j ) << " ";

        std::cout << std::endl;
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I personally like this one more than my vector<vector> solution because it is more efficient. But you will have to code a little more for this one, and in addition to coordinate conversion done here maybe also add operator () or maybe [] so you can access/modify elements by doing something like array2d(1,3) = 7 and if(array2d(1,3)>5) Still a +1 though.
1

You need something along the lines of vector <vector <int> > with inside vector representing a full row. Or a full column (just pick a row major or a column major and stick with it). Or, just use boost::multi_array (http://www.boost.org/doc/libs/1_53_0/libs/multi_array/doc/user.html)

Here is an example:

//#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

int main(){

vector <vector <int> > my2d;

vector<int> row;

row.push_back(1); row.push_back(2); row.push_back(3);

my2d.push_back(row);

++row[1];

my2d.push_back(row);

--row[0];

my2d.push_back(row);

for(size_t r=0;r<my2d.size();r++)
{
    for(size_t c=0;c<my2d[0].size();c++)
    {
        printf("%d ",my2d[r][c]);
    }
    printf("\n");
}

getchar();

return 0;
}

3 Comments

Thanks for your reply. I prefer using a vector for now, as I have slightly got used to it. Can you please tell me how can I use push_back here to update elements one by one along column or row? as in syntax wise? like (0,1), then (0,2) and so on. Thanks in advance.
If I want 3 columns and any no. of rows, how Do I do that.?
Added an example. I have 3 rows and 3 columns. I add (1 2 3) row first, then add (1 3 3) then add (0 3 3) then print the whole thing out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.