vector transpose question

mop65715's Avatar, Join Date: Apr 2006
Newbie Member
The code below transfers the two dimensional array in_arr and produces the output in out_arr. I then intialize a vector to look like in_arr. I'd like to do the same thing with the vector. What woudl the code look like in the transponse function for the vector?
Thanks in advance.


Code:
  
# include <vector> 
# include <algorithm>
using namespace std; // this is bad but only here for test purposes

template <typename T>
void  transpose(std::vector<T>& input,
	         std::vector<T>& output)  // these probably need to be const correct
{
  for ( size_t idx (0); idx < input.size(); ++idx )
  {
    //output [ idx + 1 ] = input [ idx + 1 ];
  }
}


int main()
{
  int in_arr  [ 3 ] [ 3 ];
  int out_arr [ 3 ] [ 3 ];

  // initialize arr;
  int temp ( 4 );
  for ( int idx ( 0 ); idx < 3; ++idx )
    for ( int jdx ( 0 ); jdx < 3; ++jdx )
      in_arr [ idx ] [ jdx ] = temp * jdx;
  // dump in
  for ( int idx ( 0 ); idx < 3; ++idx )
    for ( int jdx ( 0 ); jdx < 3; ++jdx )
      cout << "  " << in_arr [ idx ][ jdx ] << endl;

  cout << endl;

  // transpose
  for ( int idx ( 0 ); idx < 3; ++idx )
    for ( int jdx ( 0 ); jdx < 3; ++jdx )
      out_arr [ jdx ][ idx ] = in_arr [ idx ] [ jdx ];

  // dump
  for ( int idx ( 0 ); idx < 3; ++idx )
    for ( int jdx ( 0 ); jdx < 3; ++jdx )
      cout << "  " << out_arr [ idx ][ jdx ] << endl;

  // now do the same with a vector
  vector<double> in;
  in.push_back ( 0 );
  in.push_back ( 4 );
  in.push_back ( 8 );
  in.push_back ( 0 );
  in.push_back ( 4 );
  in.push_back ( 8 ); 
  in.push_back ( 0 );
  in.push_back ( 4 );
  in.push_back ( 8 );

  vector<double> out;

  transpose<double> ( in, out );
  std::copy ( out.begin(), out.end(), 
              std::ostream_iterator<double> ( std::cout, "\n" ));

}
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by mop65715
What woudl the code look like in the transponse function for the vector?
There will be no difference in the code for the vector as it has almost all the operators overloaded.