By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,242 Members | 1,087 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,242 IT Pros & Developers. It's quick & easy.

get 1D row of 2D vector array

P: n/a
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
May 10 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 10 Maj, 09:42, Matthias Pospiech <matthia...@gmx.dewrote:
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.

--
Erik Wikström

May 10 '07 #2

P: n/a
On May 10, 3:42 am, Matthias Pospiech <matthia...@gmx.dewrote:
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::ostream&
operator<<( std::ostream& 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::ostream_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;
}

May 10 '07 #3

P: n/a
On May 10, 9:42 am, Matthias Pospiech <matthia...@gmx.dewrote:
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,

May 11 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.