简体   繁体   English

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

[英]C++11 std::vector of template class with argument in constructor

Hello all c++ experts, 您好所有C ++专家,

I have a class template for a Circular buffer 我有一个循环缓冲区的类模板

#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

Usuallly I init it like this 通常我是这样初始化的

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);

So I tried to create a vector of CircularBuffer like this 所以我试图像这样创建一个CircularBuffer向量

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

But obviously for all of you ☺️, it does not work ! 但是显然对所有人☺️,它不起作用!

I tried multiple things without success. 我尝试了很多事情都没有成功。 So my question is only how can I declare in my header a member wich is a vector of my CircularBuffer<timed_temperature>(PLOT_RING_BUFFER_SIZE) 所以我的问题只是如何在标头中声明一个成员,该成员是我的CircularBuffer<timed_temperature>(PLOT_RING_BUFFER_SIZE)的向量

And how I access the myTempsVector[i].is_full() method of one element of the vector? 以及如何访问矢量的一个元素的myTempsVector [i] .is_full()方法?

Thank you for your help. 谢谢您的帮助。

You're confusing between CircularBuffer<timed_temperature> 's constructor and std::vector<T> constructor. 您在CircularBuffer<timed_temperature>的构造函数和std::vector<T>构造函数之间感到困惑。

When you write: 当你写:

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

It'll call std::vector's constructor with PLOT_RING_BUFFER_SIZE , trying to allocate PLOT_RING_BUFFER_SIZE different CircularBuffer<timed_temperature> using the default constructor. 它将使用PLOT_RING_BUFFER_SIZE调用std :: vector的构造PLOT_RING_BUFFER_SIZE ,尝试使用默认构造函数分配PLOT_RING_BUFFER_SIZE不同的CircularBuffer<timed_temperature>

Simply define your vector as such: 只需这样定义向量:

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

Then add ( push_back ) any new instance you want, eg : 然后添加( push_back )您想要的任何新实例,例如:

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

You simply need to create a vector first... 您只需要首先创建一个向量...

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

...and then put a value into it: ...然后将值放入其中:

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

While you're at it, you should not be performing manual memory management and storing a pointer unless you really need to . 进行此操作时,除非确实需要,否则不应执行手动内存管理和存储指针。 If you do need to store a vector through a pointer, consider using std::unique_ptr , or std::shared_ptr if std::unique_ptr doesn't work. 如果确实需要通过指针存储向量,请考虑使用std::unique_ptr ,如果std::unique_ptr不起作用,则考虑使用std::shared_ptr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C ++ 11的用户定义的类模板中继承std :: vector模板? - How to inherit std::vector template in user-defined class template in C++11? 具有多个数组的类的构造函数初始化器列表(C ++ 11可以,boost和std :: vector不可以) - Constructor initilizer list for a class with multiple arrays (C++11 is ok, boost and std::vector are not) C ++ 11和std :: vector构造函数中的值初始化对象 - Value-Initialized Objects in C++11 and std::vector constructor 为什么std :: vector的构造函数接口是用C ++ 11改变的? - Why was the constructor interface of std::vector changed with C++11? std向量顶部的C ++ 11包装器类 - A C++11 wrapper class on top of std vector C ++ 11构造函数参数:std :: move和value或std :: forward和rvalue参考 - C++11 constructor argument: std::move and value or std::forward and rvalue reference 并发环境中的C ++ 11 std :: vector - C++11 std::vector in concurrent environment C ++ 11将向量传递给构造函数 - C++11 pass vector to constructor C++11:std::result_of&lt;&gt; 模板参数与 std::function&lt;&gt; - C++11: std::result_of<> template argument vs std::function<> C++11 中的隐式构造函数参数转换 - Implicit constructor argument conversion in C++11
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM