简体   繁体   中英

C++ Template Class

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 =[

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!

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

PPs 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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM