get 1D row of 2D vector array

M

Matthias Pospiech

I have an 2D vector array, but a function that can calculate only with
1D arrays. Now I need to pass every row of the 2D array to that function.

So how can I pass a part (row or column) of a 2D vector array to a
function as a pointer - the result shall be in the 2D array automatically.

With C-Arrays A[N][N] I know the solution by passing

function(A + x*N);


Matthias
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

I have an 2D vector array, but a function that can calculate only with
1D arrays. Now I need to pass every row of the 2D array to that function.

So how can I pass a part (row or column) of a 2D vector array to a
function as a pointer - the result shall be in the 2D array automatically.

Depends on how it is stored, in general you can't get both a a row and
a column since the elements are stored either row-wise of col-wise,
which means that if they are stored row-wise and you want to get a row
then the Nth element is at position (start + N*rowlength). Notice
however that this is just the case where the elements are stored in
consecutive memory, other storage facilities are also possible (and
preferable for some applications).

Or, to sum it up, since you have not told us how the 2D array is
stored we can not answer your question.
 
S

Salt_Peter

I have an 2D vector array, but a function that can calculate only with
1D arrays. Now I need to pass every row of the 2D array to that function.

So how can I pass a part (row or column) of a 2D vector array to a
function as a pointer - the result shall be in the 2D array automatically.

With C-Arrays A[N][N] I know the solution by passing

function(A + x*N);

Matthias


Pass the whole array by reference:

#include <iostream>

template< typename T,
const size_t Row,
const size_t Col >
void function(T (& array)[Row][Col])
{
for(size_t r = 0; r < Row; ++r)
{
for(size_t c = 0; c < Col; ++c)
{
std::cout << array[r][c];
std::cout << ", ";
}
std::cout << std::endl;
}
}

int main()
{
int A[2][2] = {{1,2},{3,4}};
function( A );
}

/*
1, 2,
3, 4,
*/

A much simpler solution is use a vector of vectors since you can
initialize values on construction.

#include <iostream>
#include <vector>
#include <algorithm> // for std::copy
#include <iterator> // for ostream_iterator

// global op<< overload
template< typename T >
std::eek:stream&
operator<<( std::eek:stream& os,
std::vector< std::vector< T > >& r_vvt)
{
typedef typename std::vector< std::vector< T > >::iterator VIter;
for(VIter viter = r_vvt.begin(); viter != r_vvt.end(); ++viter)
{
std::copy( (*viter).begin(),
(*viter).end(),
std::eek:stream_iterator< T >(os, ", ") );
os << std::endl;
}
return os;
}

int main()
{
std::vector< std::vector< int > > vvn(10, std::vector< int >(10,
99));
std::cout << vvn << std::endl;
}
 
U

utab

I have an 2D vector array, but a function that can calculate only with
1D arrays. Now I need to pass every row of the 2D array to that function.

So how can I pass a part (row or column) of a 2D vector array to a
function as a pointer - the result shall be in the 2D array automatically.

Maybe not directly related to your question, but if you are on a Linux
machine, taking a look at uBlas library under Boost may be more
helpful.

Best regards,
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top