449,242 Members | 1,087 Online 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
3 Replies

 P: n/a On 10 Maj, 09:42, Matthias Pospiech

 P: n/a On May 10, 3:42 am, Matthias Pospiech 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 = {{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 #include #include 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

### This discussion thread is closed

Replies have been disabled for this discussion. 