c++ - How to tell the current 'write position' of a Boost Circular Buffer to enable accessing a previously stored value -


i want access value in boost circular buffer is, example, 5 positions 'in past'. so, imagine writing value '7' previous stream of integers:

3, 5, 6, 9, 2, 8, 6

therefore have now:

7, 3, 5, 6, 9, 2, 8, 6

i want '2' since 5 positions in past. how that?

in other words, current 'write index'?

i think might need use boost::circular_buffer<double>::const_iterator i'm not sure.

i'm not sure understand correctly, worries modulo indexing seem overly anxious me. whole purpose of circular buffer abstraction hide index arithmetics caller, if ask me.

i'd thoroughly disappointed in library design if boost let implementation detail leak out¹

here's simple demo seems want:

live on coliru

#include <iostream> #include <boost/circular_buffer.hpp>  int main() {     boost::circular_buffer<int> cb(10); // [tag:cb-so-fixedsize]      (int msg : { 3, 5, 6, 9, 2, 8, 6, 7 }) {         cb.push_back(msg);     }      // should 2     std::cout << "t0-5: " << cb[4] << "\n";     std::cout << "t0-5: " << *std::next(cb.begin(), 4) << "\n";      // should 9     std::cout << "t0-5: " << cb[3] << "\n";     std::cout << "t0-5: " << *std::next(cb.begin(), 3) << "\n";      while (!cb.empty()) {         std::cout << cb.front() << " ";         cb.pop_front();     } } 

prints

t0-5: 2 t0-5: 2 t0-5: 9 t0-5: 9 3 5 6 9 2 8 6 7  

¹ i'm aware implementation detail implied name "circular", hey. many data structures in standard library , alike have confusing names


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -