【发布时间】:2017-01-24 22:31:21
【问题描述】:
我是 C++ 新手,我的教授要求我们构建一个 Deque 类以及其他几个类,例如 Stack 和 Queue。
以下是我到目前为止所做的。
我的问题是:如何构造/制作一个函数并使其工作?例如,你怎么能在这里创建一个 push_front(int) 函数? (其实不用模板,用int就可以了。)
只有一个例子就足以让我弄清楚下一步该做什么。
非常感谢帮助。
#ifndef CLASSES_H
#define CLASSES_H
#include <vector>
#include <iostream>
class Deque{
public:
void push_front(int); //Insertion from left
void push_back(int x){m_Vector.push_back(x);};//Insertion from right
void pop_front();//Remove from left
void pop_back(){m_Vector.pop_back();};//Remove from right
int getL()const {return m_left;}
int getR()const {return m_right;}
protected:
//Protected Data members
int m_left;
int m_right;
unsigned int size;
unsigned int length;
vector<int> m_Vector;
//structure
};
// Deque member functions definitions
class Queue:private Deque{
public:
void insertL(int a){push_front(a);};
int removeR(){return pop_back();};
};
class Stack:private Deque{
public:
void push(int x){push_front(x);};
int pop(){return pop_front();};
bool full(){return m_left == m_right;}
protected:
using Deque::m_left;
using Deque::m_right;
};
#endif /* CLASSES_H */
更新:
我只是做了一个这样的小功能,
void Deque::push_front(int x){
m_Vector.insert(m_Vector.begin(),x);
}
我把它放在 Deque 类之后。有人能告诉我我做得对吗?
第二次更新:
我的 pop_front 是这样完成的。
void Deque::pop_front(){
m_Vector.front() = std::move(m_Vector.back());
m_Vector.pop_back();
}
【问题讨论】:
-
是否指示您使用
vector作为基础数据存储? -
是的,我在 google 上了解了一些有关矢量的基本信息。
标签: c++ function class vector deque