gpt4 book ai didi

c++ - 固定大小的容器,其中元素已排序并可以提供指向 C++ 中数据的原始指针

转载 作者:行者123 更新时间:2023-11-30 01:34:09 25 4
gpt4 key购买 nike

是否有一个大小可以限制的 STL 容器,插入元素使其保持排序并可以提供指向 C++ 数据的原始指针,或者它可以通过组装来自 STL 和 C++ 的一些东西来构建吗?

事实上,我正在接收实时数据(纪元 + 数据),并且我注意到它们并非“总是”以纪元的递增顺序发送。

我只保存 1024 个数据点以使用绘图 API 绘制它们,因此,我需要一个指向数据的双原始指针(x => 纪元,y => 数据)。

我写了一个类来填充 1024 个 double 组的时间和值。在接收到第1023个数据点后,缓冲区被移位以接收下一个数据点。

在下面的代码中添加排序,可能会使它过于复杂,那么有没有更好的编码方式?

struct TemporalData
{
TemporalData(const unsigned capacity) :
m_timestamps(new double[capacity]),
m_bsl(new double[capacity]),
m_capacity(capacity),
m_size(0),
m_lastPos(capacity - 1)
{

}

TemporalData(TemporalData&& moved) :
m_capacity(moved.m_capacity),
m_lastPos(moved.m_lastPos)
{
m_size = moved.m_size;

m_timestamps = moved.m_timestamps;
moved.m_timestamps = nullptr;

m_bsl = moved.m_bsl;
moved.m_bsl = nullptr;
}

TemporalData(const TemporalData& copied) :
m_capacity(copied.m_capacity),
m_lastPos(copied.m_lastPos)
{
m_size = copied.m_size;
m_timestamps = new double[m_capacity];
m_bsl = new double[m_capacity];

std::copy(copied.m_timestamps, copied.m_timestamps + m_size, m_timestamps);
std::copy(copied.m_bsl, copied.m_bsl + m_size, m_bsl);
}

TemporalData& operator=(const TemporalData& copied) = delete;
TemporalData& operator=(TemporalData&& moved) = delete;

inline void add(const double timestamp, const double bsl)
{
if (m_size >= m_capacity)
{
std::move(m_timestamps + 1, m_timestamps + 1 + m_lastPos, m_timestamps);
std::move(m_bsl + 1, m_bsl + 1 + m_lastPos, m_bsl);

m_timestamps[m_lastPos] = timestamp;
m_bsl[m_lastPos] = bsl;
}
else
{
m_timestamps[m_size] = timestamp;
m_bsl[m_size] = bsl;
++m_size;
}
}

inline void removeDataBefore(const double ptTime)
{
auto itRangeToEraseEnd = std::lower_bound(m_timestamps,
m_timestamps + m_size,
ptTime);
auto timesToEraseCount = itRangeToEraseEnd - m_timestamps;
if (timesToEraseCount > 0)
{
// shift
std::move(m_timestamps + timesToEraseCount, m_timestamps + m_size, m_timestamps);
std::move(m_bsl + timesToEraseCount, m_bsl + m_size, m_bsl);

m_size -= timesToEraseCount;
}
}

inline void clear() { m_size = 0; }

inline double* x() const { return m_timestamps; }
inline double* y() const { return m_bsl; }
inline unsigned size() const { return m_size; }
inline unsigned capacity() const { return m_capacity; }

~TemporalData()
{
delete [] m_timestamps;
delete [] m_bsl;
}
private:
double* m_timestamps; // x axis
double* m_bsl; // y axis
const unsigned m_capacity;
unsigned m_size;
const unsigned m_lastPos;
};

最佳答案

Is there an STL container which size can be limited, where inserting elements keep it sorted and can provide a raw pointer to the data in C++ or can it be built by assembling some stuff from the STL and C++ ?

不可以,但是您可以通过例如std::lower_bound .如果容器可以被随机访问,则插入的时间复杂度为O(log(N))。

After receiving the 1023th data point, the buffer is shifted to receive the next data points.

这听起来像是一个循环缓冲区。但是,如果你想保持元素排序,它就不再是一个循环缓冲区了;除非您谈论的是循环缓冲区之上的排序 View 。

关于c++ - 固定大小的容器,其中元素已排序并可以提供指向 C++ 中数据的原始指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56840323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com