Hello I have created a 2d-vector  I have no problems filing this vector. But the issue I am having is that I want to extract a specific row of this 2D vector (say the 2nd) and store it as a 1d vector. What would be the best way to do this? Can I use the copy command and if so how do I do that?

I probably can use a for loop and loop throw that specific index and push it in a new vector but thats not quite elegant enough. I figured the the copy function should work??

//Create 2d vector
vector< vector<char> > VecGrid;
vector< vector<char> > VecGrid(3, vector<char>(15,'O'));

//Storage Container for row
vector<char> VecRow

copy(VecGrid[3].begin(), VecGrid.end(),VecRow)

//assume vec2d is a 2 d vector
vector< data_type > copyRow( vec2d[0].begin(), vec2d[0].end());

That would copy the row 0 onto copyRow vector

Thats great....

I would then assume that

vector< data_type > copyRow( vec2d[3].begin(), vec2d[3].end());

copies the 4 row???

What if I need to copy a column?

Thanks

ENCHTERP

You don't need to copy anything to get a row. You just need a pointer. The program below reserves space for and initializes 12 integers. Row 2 is integer indexes 8 through 11. "Copying" implies changing the memory at certain locations. That isn't necessary here. The row2 array simply points to the 8th integer (2 times 4 - row number (2) times number of columns (4)).

To grab a column, you WOULD need to create a new array and reserve new integers in memory and do some copying since a column in a 2-D array is NOT contiguous.

#include <iostream>
using namespace std;

const int NUM_ROWS = 3;
const int NUM_COLS = 4;



int* GetRow (int arr[][NUM_COLS], int rowNumber)
{
    return arr[rowNumber];
}


int main ()
{
   int anArray[NUM_ROWS][NUM_COLS];
   for (int i = 0; i < NUM_ROWS; i++)
   {
     for (int j = 0; j < NUM_COLS; j++)
       anArray[i][j] = 10 * i + j;
   }

   int* row2 = GetRow (anArray, 2);

   for (int i = 0; i < NUM_COLS; i++)
     cout << row2[i] << " ";

   cout << endl;
   cin.get ();
   return 0;
}

That would work for an array but not for a 2d vector...The code I am writing relies on the vector container class. On another note....Would using nested for loops be a good way to convert a 2d vector into a 2d array??

>>I would then assume that
>>vector< data_type > copyRow( vec2d[3].begin(), vec2d[3].end());
>>copies the 4 row???
No it copies the 4th row. That is, it copies everything from vec[3][0] to vec[3][ end], where end is the las element in the 4th row.


>> What if I need to copy a column?
Here is an example :

#include<iostream>
#include<vector>

using namespace std;

int main()
{
	
	vector< vector< int > > twoD(5, vector<int> (5,0));
	typedef unsigned int uInt;

	//populate
	for(uInt i = 0; i < twoD.size(); i++)
		for(uInt j = 0; j < twoD[i].size(); j++)
			twoD[i][j] = j + i*twoD.size();

	//Print for testing
	for(uInt i = 0; i < twoD.size(); i++)
	{
		for(uInt j = 0; j < twoD[i].size(); j++)
		{			
			cout<<twoD[i][j]<<" ";
		}
		cout<<endl;
	}

	cout<<"\n\n\n Copying column part\n";

	//create enough space to copy column of the 2D vector
	vector<int> columnCopy(twoD.size(),0);
	
	for(uInt i = 0; i < twoD.size(); i++)
		columnCopy[i] = twoD[i][0];

	for(uInt i = 0; i < columnCopy.size(); i++)
		cout<<columnCopy[i]<<" ";

	return 0;
}

Edited 7 Years Ago by firstPerson: n/a

That would work for an array but not for a 2d vector...The code I am writing relies on the vector container class. On another note....Would using nested for loops be a good way to convert a 2d vector into a 2d array??

Guess I wasn't reading carefully. Indeed, you said it was a vector from the very beginning. Yeah, nested loops will work fine.

// 2-D vector to array (asume both are 5 x 7)

vector <vector <int>> someVector;
// populate vector

int someArray[5][7];

for (int i = 0; i < 5; i++)
{
  for (int j = 0; j < 7; j++)
  {
    someArray[i][j] = someVector[i][j];
  }
}

Edited 7 Years Ago by VernonDozier: n/a

This article has been dead for over six months. Start a new discussion instead.