【问题标题】:C++11 std::vector of template class with argument in constructor模板类的 C++11 std::vector 在构造函数中带有参数
【发布时间】:2019-10-04 00:46:32
【问题描述】:

大家好,所有 c++ 专家,

我有一个循环缓冲区的类模板

#ifndef CIRCULAR_BUFFER_H
#define CIRCULAR_BUFFER_H

#include <algorithm>
#include <cstddef>
#include <cassert>
#include <stdexcept>
#include <iostream>

template <typename T>
class CircularBuffer
{
public:
    typedef size_t size_type;
    typedef T& reference;
    typedef const T& const_reference;
    typedef T* pointer;
    typedef const T* const_pointer;

    explicit CircularBuffer(size_type capacity);
    CircularBuffer(const CircularBuffer<T> &rhs);
    CircularBuffer(CircularBuffer<T>&& rhs);
    ~CircularBuffer() { if (_buffer) delete[] _buffer; }

    CircularBuffer<T>& operator=(CircularBuffer<T> rhs);

    size_type size() const { return (_full ? _capacity : _front); }
    size_type capacity() const { return _capacity; }
    bool is_full() const { return _full; }

    const_reference operator[](size_type index) const;
    reference operator[](size_type index);

    void add(T item);
    void resize(size_type new_capacity);

    friend void swap(CircularBuffer<T> &a, CircularBuffer<T> &b)
    {
        std::swap(a._buffer, b._buffer);
        std::swap(a._capacity, b._capacity);
        std::swap(a._front, b._front);
        std::swap(a._full, b._full);
    }

private:
    pointer _buffer;
    size_type _capacity;
    size_type _front;
    bool _full;

    CircularBuffer();
};

template<typename T>
CircularBuffer<T>::CircularBuffer()
    : _buffer(nullptr)
    , _capacity(0)
    , _front(0)
    , _full(false)
{
}

template<typename T>
CircularBuffer<T>::CircularBuffer(size_type capacity)
    : CircularBuffer()
{
    if (capacity < 1) throw std::length_error("Invalid capacity");

    _buffer = new T[capacity];
    _capacity = capacity;
}

template<typename T>
CircularBuffer<T>::CircularBuffer(const CircularBuffer<T> &rhs)
    : _buffer(new T[rhs._capacity])
    , _capacity(rhs._capacity)
    , _front(rhs._front)
    , _full(rhs._full)
{
    std::copy(rhs._buffer, rhs._buffer + _capacity, _buffer);
}

template<typename T>
CircularBuffer<T>::CircularBuffer(CircularBuffer<T>&& rhs)
    : CircularBuffer()
{
    swap(*this, rhs);
}

template<typename T>
typename CircularBuffer<T>::const_reference
CircularBuffer<T>::operator[](size_type index) const
{
    static const std::out_of_range ex("index out of range");
    if (index < 0) throw ex;

    if (_full)
    {
        if (index >= _capacity) throw ex;
        return _buffer[(_front + index) % _capacity];
    }
    else
    {
        if (index >= _front) throw ex;
        return _buffer[index];
    }
}

template<typename T>
typename CircularBuffer<T>::reference
CircularBuffer<T>::operator[](size_type index)
{
    return const_cast<reference>(static_cast<const CircularBuffer<T>&>(*this)[index]);
}

template<typename T>
CircularBuffer<T>&
CircularBuffer<T>::operator=(CircularBuffer<T> rhs)
{
    swap(*this, rhs);
    return *this;
}

template<typename T>
void
CircularBuffer<T>::add(T item)
{
    _buffer[_front++] = item;
    if (_front == _capacity) {
        _front = 0;
        _full = true;
    }
}

template<typename T>
void
CircularBuffer<T>::resize(size_type new_capacity)
{
    if (new_capacity < 1) throw std::length_error("Invalid capacity");
    if (new_capacity == _capacity) return;

    size_type num_items = size();
    size_type offset = 0;
    if (num_items > new_capacity)
    {
        offset = num_items - new_capacity;
        num_items = new_capacity;
    }

    pointer new_buffer = new T[new_capacity];
    for (size_type item_no = 0; item_no < num_items; ++item_no)
    {
        new_buffer[item_no] = (*this)[item_no + offset];
    }

    pointer old_buffer = _buffer;

    _buffer = new_buffer;
    _capacity = new_capacity;
    _front = (num_items % _capacity);
    _full = (num_items == _capacity);

    delete[] old_buffer;
}

#endif // CIRCULAR_BUFFER_H

通常我是这样初始化的

timed_temperature aTimedTemperature;
aTimedTemperature.dt =102019;
aTimedTemperature.temp=37.0;
CircularBuffer<timed_temperature> myCircularBufferedTemps = CircularBuffer<timed_temperature> (PLOT_RING_BUFFER_SIZE);
myCircularBufferedFilteredTemps.add(aTimedTemperature.dt);

所以我尝试像这样创建一个 CircularBuffer 向量

std::vector<CircularBuffer<timed_temperature>> *myTempsVector = new std::vector< CircularBuffer<timed_temperature>(PLOT_RING_BUFFER_SIZE) >;

但显然对于你们所有人☺️,它不起作用!

我尝试了多种方法,但均未成功。 所以我的问题只是如何在我的标题中声明一个成员,它是我的CircularBuffer&lt;timed_temperature&gt;(PLOT_RING_BUFFER_SIZE)的向量

以及如何访问向量的一个元素的 myTempsVector[i].is_full() 方法?

感谢您的帮助。

【问题讨论】:

  • “但显然对于你们所有人☺️,它不起作用!” 不,没有什么明显的。请在此处发布minimal reproducible example 重现问题。
  • 谢谢,我编辑了一个完整的例子
  • 离题——好吧,我不得不说这段代码看起来比这里的大多数新手要好得多。可能您应该考虑的唯一事情是在resize 上,如果resize 请求小于当前大小,则不要释放缓冲区,并且只调整几个指针/变量。
  • 这不是我的代码☺️,我会修改resize
  • @AlbertTinon 好吧,考虑一下我关于优化resize 的建议。这没什么大不了的,只是想让您知道,std::vector(可能还有其他标准容器)就是这样做的。现在我看到了,也许你应该让capacitysize 大得多,而不是将resize 与容量挂钩,而是与size 挂钩。

标签: c++ c++11 templates vector


【解决方案1】:

您混淆了CircularBuffer&lt;timed_temperature&gt; 的构造函数和std::vector&lt;T&gt; 的构造函数。

当你写作时:

std::vector< CircularBuffer<timed_temperature> >(PLOT_RING_BUFFER_SIZE)

它将使用PLOT_RING_BUFFER_SIZE 调用std::vector 的构造函数,尝试使用默认构造函数分配PLOT_RING_BUFFER_SIZE 不同的CircularBuffer&lt;timed_temperature&gt;

简单地定义你的向量:

std::vector<CircularBuffer<timed_temperature>> myTempsVector;

然后添加 (push_back) 您想要的任何新实例,例如:

myTempsVector.push_back(CircularBuffer<timed_temperature> (PLOT_RING_BUFFER_SIZE));

【讨论】:

    【解决方案2】:

    您只需要先创建一个向量...

    std::vector<CircularBuffer<timed_temperature>> myTempsVector;
    

    ...然后将值放入其中:

    myTempsVector.push_back(CircularBuffer<timed_temperature>(PLOT_RING_BUFFER_SIZE));
    

    当您使用它时,您不应该执行手动内存管理和存储指针,除非您确实需要。如果确实需要通过指针存储向量,请考虑使用std::unique_ptr,如果std::unique_ptr 不起作用,请考虑使用std::shared_ptr

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      相关资源
      最近更新 更多