简体   繁体   English

C ++模板类

[英]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: 我们正在研究VectorList(有点像链表,但带有向量-不要问为什么。。)无论如何我都有这样的东西:

#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? 我需要填写函数..我的问题是,当我拥有* vList ..它是第一个向量元素的指针时,我不理解如何使用STIL?

// 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; 我的想法是在向量​​上使用for循环,并使用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 ... 这是一个头文件,主要是我们声明一个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.. 理解此*vList在这里如何发挥作用的任何帮助都会有很大帮助,我可能可以完成其余的工作。

Also, for isEmpty() , I assume I can use vList.empty() .. but since vList is a pointer.. it doesn't work quite well. 另外,对于isEmpty() ,我假设我可以使用vList.empty() ..但由于vList是一个指针..它不能很好地工作。

== For the constructor/destructor what can I do? ==对于构造函数/析构函数,我该怎么办? I know for destructor I should iterate through the vector and use delete on each element. 我知道对于析构函数,我应该遍历向量并在每个元素上使用delete。 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? 我的问题是当我拥有* vList ..它指向第一个向量元素的指针时,我不理解如何使用STL?

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. 实际上,我发现从C切换到C ++的最好之处在于,借助STL编程,我可以编写完全没有指针的程序。 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 . 因此,在构造函数中,调用new

vList = new std::vector<NODETYPE>;

Anytime we invoke new , we must have a matching delete somewhere. 每当我们调用new ,我们都必须在某处具有匹配的delete Since our new is in our constructor, we need to invoke delete in the destructor: 由于new是在构造函数中,因此我们需要在析构函数中调用delete

delete vList;

You said: 你说:

but since vList is a pointer.. it doesn't work quite well. 但是由于vList是一个指针..它不能很好地工作。

Here is where life gets easy. 这是生活变得轻松的地方。 Generally, if p is a pointer to some type, then (*p) is the object to which p points. 通常,如果p是指向某种类型的指针,则(*p)p指向的对象。 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() . 因此,调用vList的成员很容易: (*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 . 表达式x->y几乎等于(*x).y

To sum up: 总结一下:

Allocate your vList in your constructor with new . new在您的构造函数中分配vList Destroy your vList in your destructor with delete . 使用delete销毁析构函数中的vList Invoke members of vList using either (*vList).function() or vList->function() . 使用(*vList).function()vList->function()调用vList的成员。

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 . Ps由于您有一个非平凡的析构函数,因此需要考虑3的规则

PPs You said something about iterating the vector in your destructor and deleting each of the objetcs you find there. PPs您说了一些有关在析构函数中迭代向量并删除在其中找到的每个objetcs的事情。 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). 如果您的数据类型是指向NODETYPE的指针矢量(与声明的内容相反:指向NODETYPE的指针),则只需要这样做。 Until and unless you become completely comfortable with pointers, I recommend that you never store pointers in STL containers. 除非您完全不喜欢指针,否则建议您不要将指针存储在STL容器中。

You should construct your object in the constructor (if you really need using bare pointers): vList = new vector< NODETYPE >(); 您应该在构造函数中构造对象(如果确实需要使用裸指针): vList = new vector< NODETYPE >(); , free memory in the destructor: delete vList; ,释放析构函数中的可用内存: delete vList; , translate your methods to corresponding methods of the container class. ,将您的方法转换为容器类的相应方法。 For example, insertAtBack would be implemented as vList->push_back(elem); 例如, insertAtBack将实现为vList->push_back(elem);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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