Define a vector of pointers called pvector
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Define a vector of pointers called pvector

  1. #1
    Join Date
    Jun 2015
    Posts
    114

    Define a vector of pointers called pvector

    Hello,
    Here is the exercise no. 15. Well, just like many other exercises it's not comprehensible well enough. I wrote this for that:

    Code:
    #include <iostream>
    #define pvector Vector
    using namespace std;
    
    template <class T> class My_allocator {
    public:
    
    	T* allocate(int n);  // allocate space for n objects of type T
    	void deallocate(T* p, int n);  // deallocate n objects of type T starting at p
    
    	void construct(T* p, const T& v);  // construct a T with the value v in p
    	void destroy(T* p);  //  destroy the T in p
    };
    
    //-------------------------------------------------
    
    template<class T>
    T* My_allocator<T>::allocate(int n)
    {
    	T* p = (T*)malloc(sizeof(T)*n);
    	return p;
    
    }
    
    //------------------------------
    
    template<class T>
    void My_allocator<T>::deallocate(T* p, int n)
    {
    	free(p);
    }
    
    //------------------------------
    
    template<class T>
    void My_allocator<T>::construct(T* p, const T& v)
    {
    	p = new (p) T(v);
    }
    
    //------------------------------
    
    template<class T>
    void My_allocator<T>::destroy(T* p)
    {
    	p->~T();
    }
    
    //------------------------------------------------
    
    class Range_error : out_of_range {
    public:
    	int index;
    	Range_error(int i) :out_of_range("Range error"), index(i) {}
    };
    
    //--------------------------------------------------
    
    template <class T, class A = My_allocator<T> >
    class pvector {
    	A alloc;
    	int sz, space;
    	T* elem;
    public:
    	pvector() :sz(0), space(0), elem(nullptr) {}
    	pvector(int n) :sz(n), space(n) { elem = alloc.allocate(n); initial(T()); }
    	pvector(int n, const T& v) :sz(n), space(n) { elem = alloc.allocate(n); initial(v); }
    
    	pvector<T>& operator=(const pvector<T>);  //Assignment Operator
    	int size() const { return sz; }
    	int capacity() const { return space; }
    	T get_elem(int i) { return elem[i]; }
    
    	void initial(const T);
    	void resize(int, T);
    	void push_back(const T&);
    	void reserve(int);
    
    	T& operator[](unsigned int n) // rather than return at(i);
    	{
    		if (n < 0 || this->sz <= n) throw Range_error(n);
    		return elem[n];
    	}
    
    	const T& operator[](unsigned int n) const
    	{
    		if (n < 0 || this->sz <= n) throw Range_error(n);
    		return elem[n];
    	}
    };
    
    //---------------------------------------------
    
    template<class T, class A>
    void pvector<T, A>::initial(const T v)
    {
    	for (int i = 0; i < sz; ++i)
    		elem[i] = v;
    }
    
    //--------------------------------------------
    
    template <class T, class A>
    void pvector<T, A>::push_back(const T& val)
    {
    	if (space == 0) reserve(8);
    	else if (sz == space) reserve(2 * space);
    	alloc.construct(&elem[sz], val);
    	++sz;
    }
    
    //----------------------------------
    
    int main() {
    	int i = 5;
    	int* pi = &i;
    	pvector<int*> v;
    	v.push_back(pi);
    	cout << v.size() << ' ' << v.capacity() << ' '
    		 << *v.get_elem(0) << ' ' << v.get_elem(0) << endl;
    
    	system("pause");
    		return 0;
    }
    It shows "1 8 5 an address" in output.
    There are some member functions that aren't through yet.
    My question is, can it be a good code for that exercise (of course after doing the member functions' implementations)?
    Last edited by tomy12; August 28th, 2017 at 03:46 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,724

    Re: Define a vector of pointers called pvector

    I don't see why you need a custom allocator when the instructions say "except (...) its destructor deletes each object". I would have had a single member variable, i.e., a vector of pointers, then forward the operations that you need to the vector's member functions. Care needs to be taken with the destructor, copy constructor, etc, such that the container owns the objects that the pointers in the vector member point to.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Aug 2017
    Posts
    13

    Re: Define a vector of pointers called pvector

    A class to describe a two or three dimensional vector. This datatype stores two or three variables that are commonly used as a position, velocity, and/or acceleration. Technically, position is a point and velocity and acceleration are vectors, but this is often simplified to consider all three as vectors. For example, if you consider a rectangle moving across the screen, at any given instant it has a position, a velocity, and acceleration. Since vectors represent groupings of values, we cannot simply use traditional addition/multiplication/etc. Instead, we'll need to do some "vector" math, which is made easy by the methods inside the PVector class.

  4. #4
    Join Date
    Jun 2015
    Posts
    114

    Re: Define a vector of pointers called pvector

    I don't see why you need a custom allocator when the instructions say "except (...) its destructor deletes each object". I would have had a single member variable, i.e., a vector of pointers, then forward the operations that you need to the vector's member functions.
    I used the My_allocator class because the author taught us to use that class for the class vector (when defining) and since the exercise said "to be like a vector" I used it here too.
    Do you mean "implementing the operations" by "forward the operations", please?

    Care needs to be taken with the destructor, copy constructor, etc, such that the container owns the objects that the pointers in the vector member point to.
    I will add these constructors and destructor and post the code here too.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,724

    Re: Define a vector of pointers called pvector

    Quote Originally Posted by tomy12
    I used the My_allocator class because the author taught us to use that class for the class vector (when defining)
    Are you sure that is not just an example to teach you how to define and use a custom allocator, should the need arise? When you are defining your own container class template, I would expect you to default to the standard allocator, not default to your own allocator, unless there is special reason to do so.

    Quote Originally Posted by tomy12
    Do you mean "implementing the operations" by "forward the operations", please?
    That is, instead of storing sz, space, and elem, you store a single say, std::vector<T*>, and then you can just make use of the std::vector's member functions when implementing your own, so your own member functions typically do nothing more than call the std::vector's member functions and return the return value. Only for some things, e.g., "its destructor deletes each object" do you add additional code to take care of the fact that you're really modelling a vector of pointers.

    Quote Originally Posted by tomy12
    I will add these constructors and destructor and post the code here too.
    Priya456 is off track by talking about a completely different vector, i.e., the notion of a vector in physics that consists of both magnitude and direction. Such a vector is a very different beast from a container of pointers.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)