C++中双端队列的实现

发布于 2024-10-21 03:11:10 字数 5656 浏览 2 评论 0原文

我正在编写 Deque 的实现作为编程练习,但进展不太顺利。我缺少一些关键功能,这些功能需要使我得到的测试主程序正确运行。

到目前为止,这是我的代码:

#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";
}

我想知道是否有人可以告诉我第 66 行的函数正在返回:

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

因为我不确定我在其中做错了什么。另外,如果有人愿意给我一个 pop_front() 函数的示例,以便我也可以使用它来创建 pop_back() 函数,那就太好了。最后,我已经完成了一个擦除函数,但我不知道如何创建第二个函数,它基本上擦除两个迭代器范围内的值,它在第 176 行中引用。

任何帮助将不胜感激。先感谢您。

I'm writing an implementation of Deque as a programming exercise and it's not going too well at all. 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:

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

Any help would be greatly appreciated. Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

扬花落满肩 2024-10-28 03:11:10

至于错误,您可能需要在该行的 Deque::iterator 之前添加 typename

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

As for the error, you probably need a typename before Deque<T>::iterator on that line.

typename Deque<T>::iterator DequeIterator<T>::operator++(int)
物价感观 2024-10-28 03:11:10

我认为实现双端队列是一个很好的编程练习。但先决条件是实现向量和列表。 deque 是实现起来最复杂的 std::container 之一。您应该从更简单的之一(向量和列表)开始。

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. You should start with one of the simpler ones (vector and list).

仄言 2024-10-28 03:11:10

嗯,您在第 65 行得到了错误,因为您返回了一个尚未定义的类的对象。您只有类 DequeIterator 的前向声明(原型),而不是实现。

Well, you get the error on line 65 because you return an object of a class that hasn't been defined. You only have the forward declaration (prototype) for class DequeIterator, not the implementation.

末蓝 2024-10-28 03:11:10
void pop_back() {
  vecTwo.pop_back();
}

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

void pop_front() {
  vecOne.pop_back();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文