简体   繁体   English

Deque在C ++中的实现

[英]Implementation of Deque in C++

I'm writing an implementation of Deque as a programming exercise and it's not going too well at all. 我正在将Deque的实现编写为编程练习,但进展并不顺利。 I'm missing a few key function that are needed to make the test main program I was given function correctly. 我缺少一些使我获得正确的测试主程序功能所需的关键功能。

Here is my code so far: 到目前为止,这是我的代码:

#include <vector>
#include <iostream>
#include <cassert>

using namespace std;

template <class T> class DequeIterator;

template <class T>
class Deque {
public:
    typedef DequeIterator<T> iterator;

    Deque(): vecOne(), vecTwo() { }
    Deque(unsigned int size, T& initial): vecOne(size/2, initial), vecTwo(size-(size/2), initial) { }
    Deque(Deque<T> & d): vecOne(d.vecOne), vecTwo(d.vecTwo) { }

    T & operator[](unsigned int);
    T & front();//
    T & back();//
    bool empty(){ return vecOne.empty() && vecTwo.empty(); }
    iterator begin() { return iterator(this,0); }
    iterator end() { return iterator(this, size ()); }
    void erase(const iterator &);
    void erase(const iterator &, const iterator &);
    void insert(const iterator &, const T &);
    int size() { return vecOne.size() + vecTwo.size(); }
    void push_front(const T & value) { vecOne.push_back(value); }
    void push_back(const T & value) {vecTwo.push_back(value); }
    void pop_front();
    void pop_back();
protected:
    vector<T> vecOne;
    vector<T> vecTwo;
};

template <class T>//
T & Deque<T>::front()//returns the first element in the deque
{
    if (vecOne.empty())
        return vecTwo.front();
    else
        return vecOne.back();
}

template <class T>//
T & Deque<T>::back()//returns the last element in the deque
{
    if (vecOne.empty())
        return vecTwo.back();
    else
        return vecOne.front();
}

template <class T>//
T & Deque<T>::operator[] (unsigned int index)
{
    int n = vecOne.size();
    if (index < n)
        return vecOne [ (n-1) - index ];
    else
        return vecTwo [ index - n ];
}

template <class T>//
Deque<T>::iterator DequeIterator<T>::operator ++ (int)
{
    Deque<T>::iterator clone(theDeque, index);
    index++;
    return clone;
}


template <class T>//
void Deque<T>::pop_front()
{

}

template <class T>//
void Deque<T>::pop_back()
{

}

template <class T>//
void Deque<T>::erase (const iterator & itr)
{
    int index = itr.index;
    int n = vecOne.size();
    if (index < n)
        vecOne.erase (vecOne.begin() + ((n-1) - index));
    else
        vecTwo.erase (vecTwo.begin() + (n - index));
}

template <class T>//
void Deque<T>::erase (const iterator &, const iterator &)
{

}

template <class T>//
void Deque<T>::insert(const iterator &, const T &)
{

}

template <class T>
class DequeIterator {
    friend class Deque<T>;
    typedef DequeIterator<T> iterator;
public:
    DequeIterator(): theDeque(0), index(0) { }
    DequeIterator(Deque<T> * d, int i): theDeque(d), index(i) { }
    DequeIterator(const iterator & d): theDeque(d.theDeque), index(d.index) { }

    T & operator*() { return (*theDeque)[index]; }
    iterator & operator++(int) { ++index; return *this; }
    iterator operator++();
    iterator operator--(int) { --index; return *this; }
    iterator & operator--();
    bool operator==(const iterator & r) { return theDeque == r.theDeque && index == r.index; }
    bool operator!=(const iterator & r) { return theDeque == r.theDeque && index != r.index; }
    bool operator< (const iterator & r) { return theDeque == r.theDeque && index < r.index; }
    T & operator[](unsigned int i) { return (*theDeque) [index + i]; }
    iterator operator=(const iterator & r) { theDeque = r.theDeque; index = r.index; }
    iterator operator+(int i) { return iterator(theDeque, index + i); }
    iterator operator-(int i) { return iterator(theDeque, index - i); }
protected:
    Deque<T> * theDeque;
    int index;
};
main()
{
    Deque<int> d;

    d.push_back(10);
    d.push_back(20);
    assert(d.front() == 10);
    assert(d.back() == 20);

    d.push_front(1);
    d.push_front(2);
    d.push_front(3);
    assert(d.front() == 3);
    assert(d.back() == 20);

    d.pop_back();
    d.pop_back();
    d.pop_back();
    assert(d.front() == 3);
    assert(d.back() == 2);

    d.push_back(1);
    d.push_back(0);

    Deque<int>::iterator i;
    int counter = 3;
    for (i = d.begin(); i != d.end(); i++)
        assert(*i == counter--);

    for (counter = 0; counter < d.size(); counter++)
        assert(d[counter] == d.size()-counter-1);

    i = d.begin() + 3;
    Deque<int>::iterator j(i), k;
    k = j = i - 2;
    assert(*k == 2);

    for (i = d.begin(); not(i == d.end()); ++i)
        cout << *i << " ";
    cout << endl;

    d.erase(d.begin()+3);
    //d.erase(d.begin(), d.begin()+2);
    assert(d.size() == 1);
    assert(d[0] == 1);

    Deque<int> c(d);
    c.front() = 3;
    assert(c.back() == 3);

    c.push_front(1);
    c.insert(c.begin(), 0);
    c.insert(c.begin()+2, 2);

    for (i = c.begin(); not(i == c.end()); ++i)
        cout << *i << " ";
    cout << endl;

    for (counter = 0; counter < c.size(); counter++)
        assert(c[counter] == counter);

    cout << "SUCCESS\n";
}

I was wondering if someone could tell me my function from line 66 is returning: 我想知道是否有人可以告诉我第66行的函数正在返回:

expected constructor, destructor, or type conversion before 'DequeIterator'

Because I'm not sure what I'm doing wrong in it. 因为我不确定自己在做什么错。 Also, if someone would be kind enough to give me an example of the pop_front() function so that I can use it to create the pop_back() function as well, that would be great. 另外,如果有人愿意给我一个pop_front()函数的示例,以便我也可以使用它来创建pop_back()函数,那将很棒。 Lastly, I have on of the erase functions completed but I am not sure how to go about creating the second one, which basically erases a value within the range of two iterators, it is referenced in line 176. 最后,我已经完成了擦除功能中的一个,但是我不确定如何创建第二个擦除功能,该操作基本上会擦除两个迭代器范围内的值,请参见第176行。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you in advance. 先感谢您。

至于错误,你可能需要一个typenameDeque<T>::iterator在该行。

typename Deque<T>::iterator DequeIterator<T>::operator++(int)

I think it is a great programming exercise to implement deque. 我认为实现双端队列是一个很棒的编程练习。 But a prerequisite is to implement vector and list. 但是前提是要实现矢量和列表。 deque is one of the most complicated std::containers to implement. deque是要实现的最复杂的std :: container之一。 You should start with one of the simpler ones (vector and list). 您应该从较简单的一种(矢量和列表)开始。

Well, you get the error on line 65 because you return an object of a class that hasn't been defined. 好吧,您在第65行出现错误,因为您返回的是尚未定义的类的对象。 You only have the forward declaration (prototype) for class DequeIterator , not the implementation. 您只有类DequeIterator的前向声明(原型),而没有实现。

void pop_back() {
  vecTwo.pop_back();
}

void pop_front() {
  vecOne.pop_back();
}

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

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