gpt4 book ai didi

c++ - 如何在 for 循环中迭代 `std::stack` 个元素?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:39 25 4
gpt4 key购买 nike

<分区>

我正在编写一个 FIR 滤波器,它应该计算 running average输入序列的。

class RunningAverager
{
public:
RunningAverager(uint64_t FilterOrder)
{
for (uint64_t i=0; i<FilterOrder; i++)
{
Registers.push(0);
}
}
uint64_t GetAverage(uint64_t NewInput)
{
Registers.push(NewInput);
Registers.pop();
return GetAverage();
}
uint64_t GetAverage() const
{
uint64_t Sum = 0;
//for (uint64_t i=0; i<Registers.size(); i++) <-- Works
for (std::stack<uint64_t>::const_reference ref=Registers.begin(); ref!=Registers.end(); ???)
{ // begin() and end() methods do not exist for std::stack
//Sum += Registers[i]; Doesn't work, because the [] operator is not overloaded.
Sum += ref;
}
return Sum / Registers.size();
}
private:
std::stack<uint64_t> Registers;
};

我在迭代 std::stack 对象 Registers 时遇到问题。与其他 STL 容器不同,它不提供迭代器或随机访问运算符。

如何循环和 std::stack 对象?

用法示例:

RunningAverager ra(10);

while(...)
{
FilteredSpeed = ra.GetAverage(ActualSpeed);
}

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