Deque через два вектора C ++ - PullRequest
0 голосов
/ 19 июня 2020

Прохожу курс C ++ и укладываюсь в задачу. Я пытался реализовать deque через два вектора на C ++, но моя реализация слишком медленная. Думаю, медлительность вызвана функцией PushFront. Реализация правильная (я так думаю), проблема только в медлительности. Дайте мне несколько советов, чтобы мое решение было быстрее, пожалуйста:)

template<typename T>
class Deque{
public:    
    Deque(){}    

    const bool Empty() const{
        return first.empty() && second.empty();
    }

    const size_t Size() const{
        return first.size() + second.size();
    }

    T& operator[](size_t index){
        if (index < first.size()){
            return first[index];
        }
        else if (index < first.size() + second.size()){
            return second[index - first.size()];
        }
        else{
            throw out_of_range("index out of range");
        }
    }

    const T& operator[](size_t index) const{
        if (index < first.size()){
            return first[index];
        }
        else if (index < first.size() + second.size()){
            return second[index - first.size()];
        }
        else{
            throw out_of_range("index out of range");
        }
    }

    const T& At(size_t index) const{
        if (index < first.size()){
            return first[index];
        }
        else if (index < first.size() + second.size()){
            return second[index - first.size()];
        }
        else{
            throw out_of_range("index out of range");
        }
    }

    T& At(size_t index){
        if (index < first.size()){
            return first[index];
        }
        else if (index < first.size() + second.size()){
            return second[index - first.size()];
        }
        else{
            throw out_of_range("index out of range");
        }
    }

    T& Front(){
        if (first.empty() && second.empty())
            throw out_of_range("out of range");
        else if (first.empty() && !second.empty())
            return second[0];
        else
            return first[0];
    }

    const T& Front() const{
        if (first.empty() && second.empty())
            throw out_of_range("out of range");
        else if (first.empty() && !second.empty())
            return second[0];
        else
            return first[0];
    }


    T& Back(){
        if (second.empty() && first.empty())
            throw out_of_range("out of range");
        else if (second.empty() && !first.empty())
            return first[first.size() - 1];
        else
            return second[second.size() - 1];
    }

    const T& Back() const{
        if (second.empty() && first.empty())
            throw out_of_range("out of range");
        else if (second.empty() && !first.empty())
            return first[first.size() - 1];
        else
            return second[second.size() - 1];
    }

    void PushFront(const T& elem){
        first.insert(first.begin(), elem);
    }

    void PushBack(const T& elem) {
        second.push_back(elem);
    }

private:
    vector<T> first, second;
};
...