1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

vector transpose question

Discussion in 'C++' started by mop65715, Apr 14, 2006.

  1. mop65715

    mop65715 New Member

    Joined:
    Apr 14, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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" ));
    
    } 
    
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    There will be no difference in the code for the vector as it has almost all the operators overloaded.
     

Share This Page