C++11 std::vector 的模板类,构造函数中有参数

阿尔伯特·蒂农

各位 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<timed_temperature>(PLOT_RING_BUFFER_SIZE)

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

感谢您的帮助。

杰弗里

您在CircularBuffer<timed_temperature>的构造函数和std::vector<T>构造函数之间感到困惑

当你写:

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

它将使用 调用 std::vector 的构造函数PLOT_RING_BUFFER_SIZE,尝试使用默认构造函数分配PLOT_RING_BUFFER_SIZE不同CircularBuffer<timed_temperature>的构造函数。

简单地定义你的向量:

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用带有 Boost.Python 的 std::map 或 std::vector 参数的构造函数包装 C++ 类?

为什么C ++ 11从std :: vector的fill构造函数原型中删除默认值?

具有可变构造函数的类模板;将参数存储到std :: vector <std :: shared_ptr <>

使用新的C ++ 11 std :: async加速std:.vector填充

使用现代C ++时,带有私有构造函数的类的std :: vector无法编译

C ++ 11如何创建模板函数以将原语和std :: vector转换为具有最少专业化的std :: string?

具有私有构造函数的std :: map中的C ++ 11类作为Value

C ++ 11使用std :: thread运行模板化类函数

c ++ std::vector 作为类方法的参数传递的函数

C ++函数的可选std :: vector参数

C ++ 11:从模板函数构建std :: tuple

C ++模板函数,可以采用Eigen :: vector或std :: vector

启用C ++ 11时std :: vector性能回归

C ++复制构造函数vs移动std :: vector的构造函数

具有可变参数std :: function的C ++ 11函数签名

如何使用 std::vector 动态实例化具有参数化构造函数的类?

c ++ 17在编译时将具有已删除副本构造函数的类添加到std :: vector

带有 std::enable_if 和 std::decay 的 C++ 类构造函数模板

C ++ enable_if模板类方法(如果它是std :: vector <std :: vector <Type >>)

C ++ 11多线程合并排序,错误为“没有构造函数'std :: thread'的实例与参数列表匹配”

C ++ 11:带有std :: move()的'decltype类实例声明'不调用'move构造函数'。为什么?

std::vector 的 std:array 的 SFINAE 不能在 C++11 中编译

如何在c ++ 11中初始化std :: vector <std :: string>的静态constexpr成员?

C++ - 构造函数中的 std::vector 初始化

C ++-为std :: vector <double>创建新的构造函数吗?

std :: vector的C ++钳位函数

如何从 C++ 中的模板类访问 std::vector 数据

使用类std :: vector的构造函数在C数组上进行Constexpr包装

C++:函数模板指针的 std::vector