Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   vector transpose question (http://www.go4expert.com/forums/vector-transpose-question-t729/)

mop65715 15Apr2006 03:13

vector transpose question
 
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" ));

}


shabbir 17Apr2006 10:51

Re: vector transpose question
 
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.


All times are GMT +5.5. The time now is 16:56.