get a row from a 2-D array

R

RS

Hi,

I want to get a row from a 2-D array.
For example:
There is a 2-D array 3 x 4. I want to get a row, which is size of 4.

2-D array 1-D array
a b
####
#### -> #####
####

How can I do it in C++?

RS
 
C

Chris Theis

RS said:
Hi,

I want to get a row from a 2-D array.
For example:
There is a 2-D array 3 x 4. I want to get a row, which is size of 4.

2-D array 1-D array
a b
####
#### -> #####
####

How can I do it in C++?

You can either use the "ordinary" 2D Array like int MyArray[3][4] and
iterate over the elements via the indices, or use nested vectors to obtain a
whole row at once.

e.g.:
vector<vector<int> > MyArray;

fill your array....

MyArray[2] will now return the vector containing the third row of elements.


HTH
Chris
 
S

Stewart Gordon

RS said:
Hi,

I want to get a row from a 2-D array.
For example:
There is a 2-D array 3 x 4. I want to get a row, which is size of 4.

2-D array 1-D array
a b
####
#### -> #####
####

How can I do it in C++?

Suppose your array is declared

int qwert[3][4];

then just do

int *row1 = qwert[1];

then you can access the elements as row1[0] to row1[3].

Stewart.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,966
Members
47,516
Latest member
ChrisHibbs

Latest Threads

Top