1

Hey.. I'm having trouble with some homework.

We are working on VectorList ( kinda like linked list but with vectors - don't ask why.. ) Anyway I have something like this:

#ifndef VECTORLIST_H 
#define VECTORLIST_H

#include <iostream>
#include <vector>

using namespace std;

template< typename NODETYPE >

class VectorList 
{
public:
  VectorList(); // constructor
  ~VectorList(); // destructor

  void insertAtFront( const NODETYPE & );
  void insertAtBack( const NODETYPE & );
  bool removeFromFront( NODETYPE & );
  bool removeFromBack( NODETYPE & );
  bool isEmpty() const;
  void print() const;

private:
  vector< NODETYPE > *vList;  // list data as a vector

};

I need to fill in the functions.. my problem is that I do not understand how to use STIL when I have *vList.. its a pointer to the first vector element?

// display contents of VectorList
template< typename NODETYPE >
void VectorList< NODETYPE >::print() const
{
  // Fill in the missing code 
} 

My Idea was to use a for loop on the vector and use cout<< vector[i]<< endl; to print the vector out..

Problem is that I get all sorts of errors and seg faults.

I do not understand how to access the vector in the function, and how to access its elements.

This is a header file, and in the main we declare an object of VectorList<NODETYPE> IntVector...

So how can I do this? Any help with understanding of how this *vList plays a role here would help a lot and I'd probably be able to finish the rest..

Also, for isEmpty(), I assume I can use vList.empty().. but since vList is a pointer.. it doesn't work quite well.

== For the constructor/destructor what can I do? I know for destructor I should iterate through the vector and use delete on each element. But shoul

Please explain this to me, I am frustrated =[

Mahesh
  • 34,573
  • 20
  • 89
  • 115
Sanya
  • 33
  • 1
  • 1
  • 3
  • Added `homework` tag but removed `printing` tag, as the system allows only 5 tags. – Mahesh Mar 12 '11 at 23:10
  • 1
    Never employ `using namespace` declarations in header files, that will force the `std` namespace into all your users, and that might cause name collisions in their code. – David Rodríguez - dribeas Mar 13 '11 at 00:06
  • Almost all of your problems come from trying to use a pointer to a vector. If you store the vector directly in your class, most of the other things will just work! – Bo Persson Mar 13 '11 at 08:13

2 Answers2

1

my problem is that I do not understand how to use STL when I have *vList.. its a pointer to the first vector element?

I assume that you are required as part of your homework to use pointer-to-vector instead of a vector itself. As a rule, I never use pointers-to-containers. In fact, the best thing that I discovered in switching from C to C++ was that I could write entire programs with no pointers at all, thanks to STL programming. Unless you are required to use pointer-to-vector, I recommend that you use the vector directly.

Certainly it is easier to use the vector proper than a pointer, but don't worry. Using the pointer isn't too bad.

First, in order to use a pointer-to-something, one must allocate the something. So, in your constructor, invoke new.

vList = new std::vector<NODETYPE>;

Anytime we invoke new, we must have a matching delete somewhere. Since our new is in our constructor, we need to invoke delete in the destructor:

delete vList;

You said:

but since vList is a pointer.. it doesn't work quite well.

Here is where life gets easy. Generally, if p is a pointer to some type, then (*p) is the object to which p points. Here are some examples:

int i = 1;
int *pInt = &i;

i = 4;
(*pInt) = 4;
std::cout << i << " " << (*pInt) << "\n";

std::vector<NODETYPE> v;
std::vector<NODETYPE> *pVector;

v.push_back();
(*pVector).push_back();

it = v.begin();
it = (*pVector).end();

So, invoking members of vList is easy : (*vList).empty().

So, your code might be :

void insertAtFront(const NODETYPE& node) { (*vList).push_front(node); }

There is a short-cut operator -> that makes the above somewhat easier to read:

void insertAtFront(const NODETYPE& node) { vList->push_front(node); }

The expression x->y is more-or-less equivalent (*x).y.

To sum up:

Allocate your vList in your constructor with new. Destroy your vList in your destructor with delete. Invoke members of vList using either (*vList).function() or vList->function().

Good luck, and come back if you have other questions!

P.s. Since you have a non-trivial destructor, you'll need to consider the rule of three.

P.P.s. You said something about iterating the vector in your destructor and deleting each of the objetcs you find there. You would only need to do that if your data type were vector-of-pointers-to-NODETYPE (contrast to what you declared: pointer-to-vector-of-NODETYPE). Until and unless you become completely comfortable with pointers, I recommend that you never store pointers in STL containers.

Community
  • 1
  • 1
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • @Nawaz : Don't give me the -1, give it to the OP's professor. I'm just showing how to do what he was apparently assigned to do. – Robᵩ Mar 13 '11 at 01:49
  • You should atleast tell him that using pointer to vector is a bad idea, when it's not needed. If you do so, I'll upvote it! – Nawaz Mar 13 '11 at 01:50
  • I thought I had, but now I see the warning was pretty weak. I've strengthened the warning in the post. To the OP: I never use pointer-to-container, and I never, ever use container-of-pointers. I recommend that you don't either, until and unless you understand why. – Robᵩ Mar 13 '11 at 02:13
-1

You should construct your object in the constructor (if you really need using bare pointers): vList = new vector< NODETYPE >();, free memory in the destructor: delete vList;, translate your methods to corresponding methods of the container class. For example, insertAtBack would be implemented as vList->push_back(elem);

vissi
  • 2,325
  • 1
  • 19
  • 26
  • As you see, I mentioned it just because it was specified this way in the homework. I wouldn't do such pointer mess either. – vissi Mar 13 '11 at 08:13