from array to vector class

Discussion in 'C++' started by aortizb, Sep 28, 2008.

  1. aortizb

    aortizb New Member

    Joined:
    Sep 12, 2008
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Hello, I am trying to rewrite the two snippets below using the vector class. Can anyone guide me in how to do it? For example, can I return a pointer to a vector object? is that the right way to proceed? If someone wants to translate the below snippets would be great. Both snippets do the same thing but I want to see the differences that may arise using vector class with the two approaches below. Thanks.

    Snippet 1:
    Code:
    int main()
    {
      int size, i;
      double* p;
    
      size = 100;
    
      p = new double[size];
      for (i = 0; i < size; i++)
        p[i] = 0.0;
    
      changeArray(p, size);
    
      for (i = 0; i < size; i++)
        cout << p[i] << endl;
    
      delete [] p;
    
      return 0;
    }
    
    Snippet 2:
    Code:
    int main()
    {
      int size, i;
      double* p;
    
      size = 100;
    
      p = changeArray(size); 
      for (i = 0; i < size; i++)
        cout << p[i] << endl;
    
      delete [] p;
    
      return 0;
    }
    
    changeArray() is as follows:

    For snippet 1:
    Code:
    void changeArray(double* arr, int length)
    {
      for (i = 0; i < length; i++)
        arr[i] = (i + 1) * (i + 1);
    }
    
    For snippet 2:
    Code:
    double* changeArray(int length)
    {
      double* temp;
    
      temp = new double[length];
      for (i = 0; i < length; i++)
        temp[i] = (i + 1) * (i + 1);
    
      return temp;
    }
    
     
  2. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    typedef vector< double > VecType;
    
    VecType * createVector( int size )
    {
      VecType *ret = new VecType( size ); // elements init'd to 0
    
      for (int i = 0; i < ret->size(); i++)
        (*ret)[i] = (i + 1) * (i + 1);
    
      return ret;
    }
    
    void changeVector( VecType & v )
    {
        for( int i = 0; i < v.size(); ++i )
          v[i] = (i + 1) * (i + 1);
    }
    
    void showVector( VecType & v )
    {
        for( int i = 0; i < v.size(); ++i )
            cout << v[i] << ' ';
        cout << endl;
    }
    
    int main()
    {
        const int size = 10;
        VecType p( size ); // elements init'd to 0
    
        showVector( p );
        changeVector( p );
        showVector( p );
    
        VecType *q = createVector( size );
        showVector( *q );
        delete q;
    
        return 0;
    }
    
     
  3. aortizb

    aortizb New Member

    Joined:
    Sep 12, 2008
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for your reply. I have tested another two way to do what you did with vectors above. The first is returning a vector object by value which should be expensive for large vectors. The second is passing a vector object by reference in a function that returns a void value. The third option would be what you coded above (i.e., returning a pointer to a vector object). The second and third option seems to be equally fast. But I am unsure if these are the fastest way I can work. Do you know if there is yet a faster way to do it? My concern is because I am working with large scale problems (i.e., large vectors).

    Thanks.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice