使用 std::sort 和自定义“向量”容器

问题描述 投票:0回答:1

我正在尝试将

std:sort
与自定义 C++ 矢量一起使用,正如您所看到的,迭代器是使用基于范围的 for 循环来实现和测试的,以打印矢量内容。但是
std::sort
给出了构建错误:

'_Sort_unchecked': no matching overloaded function found

我的代码中是否缺少任何能够使用

std::sort
的要求?

这是完整的代码:

#include <iostream>
#include<exception>
#include<algorithm>

template <typename T>
class CustomVector {

private:

    T* m_data;
    int m_size ;
    int m_capacity ;

public:

    class Iterator
    {
        using value_type = T;
        using difference_type = std::ptrdiff_t;
        using pointer = T*;
        using reference = T&;
        using iterator_category = std::random_access_iterator_tag;

    public:
        Iterator() = default;
        Iterator(T* pData) : m_ptr(pData) {}

        reference       operator*() { return *m_ptr; }
        const reference& operator*()            const { return *m_ptr; }
        pointer         operator->() { return m_ptr; }
        const pointer   operator->()            const { return m_ptr; }
        reference       operator[](int offset) { return m_ptr[offset]; }
        const reference operator[](int offset)  const { return m_ptr[offset]; }

        Iterator& operator++() { ++m_ptr; return *this; }
        Iterator& operator--() { --m_ptr; return *this; }
        Iterator operator++(int) { Iterator it(*this); ++m_ptr; return *this; }
        Iterator operator--(int) { Iterator it(*this); --m_ptr; return *this; }

        Iterator& operator+=(int offset) { m_ptr += offset; return *this; }
        Iterator& operator-=(int offset) { m_ptr -= offset; return *this; }

        Iterator operator+(int offset) const { Iterator it(*this); return it += offset; }
        Iterator operator-(int offset) const { Iterator it(*this); return it -= offset; }

        difference_type operator-(const Iterator& other) const { return m_ptr - other.m_ptr; }

        bool operator<  (const Iterator& other) const { return m_ptr < other.m_ptr; }
        bool operator<= (const Iterator& other) const { return m_ptr <= other.m_ptr; }
        bool operator>  (const Iterator& other) const { return m_ptr > other.m_ptr; }
        bool operator>= (const Iterator& other) const { return m_ptr >= other.m_ptr; }
        bool operator== (const Iterator& other) const { return m_ptr == other.m_ptr; }
        bool operator!= (const Iterator& other) const { return m_ptr != other.m_ptr; }

    private:
        T* m_ptr{ nullptr };
    };


    Iterator begin() { return Iterator(m_data); }
    Iterator end() { return Iterator(m_data + m_size); }

    CustomVector() :m_size(0), m_capacity(2), m_data(new T[m_capacity]) { std::cout << "constructor called" << std::endl; }
    CustomVector(const int & size)  {

        m_size = size;
        m_capacity = 2 * size;
        m_data = new T[m_capacity];
        std::cout << "constructor called" << std::endl; 
    }

    CustomVector(const CustomVector& other) {

        m_size = other.m_size;
        m_capacity = other.m_capacity;
        m_data = new T[m_capacity];

       for (int i = 0; i < m_size; i++) {
            m_data[i] = other.m_data[i];
       }
    }

    ~CustomVector() { delete[] m_data; std::cout << "destructor called" << std::endl; }

    int size() { return m_size; }
    int capacity() { return m_capacity; }

    void reserve(int newcapacity) {

        T* newData = new T[newcapacity];

            for (int i = 0; i < m_size; i++) {
                newData[i] = m_data[i];
            }

        if(m_data != nullptr)delete[]m_data;

        m_data = newData;
        m_capacity = newcapacity;
    }

    void push_back(const T& val) {

        if (m_size < m_capacity) {
            reserve(m_capacity * 2);
        }

        m_data[m_size++] = val;
    }

    void print_data() { 

       Iterator iter(m_data);
        for (iter = this->begin(); iter != this->end(); iter++) {

            std::cout << "data =" << *iter << std::endl;
        }
    }

    void pop_back() {

        if (m_size > 0) {

            (reinterpret_cast<T*>(m_data)[m_size - 1]).~T();
            m_size--;
        }
    }

    void clear() {

        if (m_size > 0) {

            delete[]m_data;
            m_data = nullptr;
            m_size = 0;
            m_capacity = 2;
        }
    }

    bool empty() {
        return (m_size == 0);
    }

    const T& operator [] (int index) const {
        if (index > m_size || index < 0) {
            throw std::out_of_range("index is out of range (Custom vector class)");
        }
        
        return  m_data[index];
    }

    CustomVector<T>& operator = (const CustomVector<T>& V) {

        m_size = V.m_size;
        m_capacity = V.m_capacity;
        m_data = new T[m_size];

        for (int i = 0; i < m_size; i++) {
            m_data[i] = V.m_data[i];
        }
    }

    CustomVector<T>& operator>(const CustomVector<T>& other) {
        
        return *this;
    }

    CustomVector<T>& operator<(const CustomVector<T>& other) {
        
        return *this;
    }

};


int main() {

    try {

        CustomVector<double>somevector;
        somevector.push_back(16666);
        somevector.push_back(55);
        somevector.push_back(100);
        somevector.push_back(300000);

        for (auto& val : somevector) {
            std::cout << "val = " << val << std::endl;
        }

        //double min = *std::min_element(somevector.begin(), somevector.end()); // build error
        //double max = *std::max_element(somevector.begin(), somevector.end()); // build error 
        std::sort(somevector.begin(), somevector.end() );  // build error

        std::fill(somevector.begin(), somevector.end(), 100); // working

    }
    catch (std::exception& e) {
        std::cout << "exception : " << e.what() << std::endl;
    }


    return 0;
}
c++ stl
1个回答
4
投票

您的

Iterator
类型中的 using 声明是
private
,因此
std::iterator_traits<CustomVector<T>::Iterator>
无法找到它们,反过来也不会定义它们。 如果你做到了这些
public
,它就有效了。

class Iterator
    {
    public:
        using value_type = T;
        using difference_type = std::ptrdiff_t;
        using pointer = T*;
        using reference = T&;
        using iterator_category = std::random_access_iterator_tag;

        Iterator() = default;
        // ...
© www.soinside.com 2019 - 2024. All rights reserved.