【发布时间】:2016-10-13 19:19:45
【问题描述】:
如果我的问题不包含所有相关信息,我们深表歉意。请发表评论,我会做出相应的修改。
我在 Win7 上使用带有 MinGW 和 gcc 的 CLion
我一直在尝试使用循环缓冲区并遇到boost::circular_buffer,但对于我的项目规模,我想使用Pete Goodlife 的circular buffer,这似乎是一个可靠的实现,仅在一个.hpp 中。
注意:感谢Boost dependencies and bcp,我知道如何减少对 boost 的依赖。
但是,下面的 Pete 实现示例的行为与预期不同,即 std::adjacent_difference(cbuf.begin(),cbuf.end(),df.begin()); 的结果是 empty。我想了解原因并可能纠正其行为。
遵循 MWE:
#include "circular.h"
#include <iostream>
#include <algorithm>
typedef circular_buffer<int> cbuf_type;
void print_cbuf_contents(cbuf_type &cbuf){
std::cout << "Printing cbuf size("
<<cbuf.size()<<"/"<<cbuf.capacity()<<") contents...\n";
for (size_t n = 0; n < cbuf.size(); ++n)
std::cout << " " << n << ": " << cbuf[n] << "\n";
if (!cbuf.empty()) {
std::cout << " front()=" << cbuf.front()
<< ", back()=" << cbuf.back() << "\n";
} else {
std::cout << " empty\n";
}
}
int main()
{
cbuf_type cbuf(5);
for (int n = 0; n < 3; ++n) cbuf.push_back(n);
print_cbuf_contents(cbuf);
cbuf_type df(5);
std::adjacent_difference(cbuf.begin(),cbuf.end(),df.begin());
print_cbuf_contents(df);
}
打印以下内容:
Printing cbuf size(3/5) contents...
0: 0
1: 1
2: 2
front()=0, back()=2
Printing cbuf size(0/5) contents...
empty
不幸的是,作为 C++ 的新手,我无法弄清楚为什么 df.begin() 迭代器没有被取消引用为左值。
我怀疑罪魁祸首是(或不完全理解)Pete 的circular.h 中第 72 行的circular_buffer_iterator 的成员调用:
elem_type &operator*() { return (*buf_)[pos_]; }
非常感谢任何帮助。
【问题讨论】:
标签: c++ stl circular-buffer