Circular Buffer

Good day!
I'm a beginner in C++, and I'm currently trying to implement a (boolean) circular buffer.
I would like to ask about getting the iterator of the begin and end of a circular buffer, and then printing the contents of a buffer.

I've tried looking at the Boost reference page and also relevant forums, and came up with the following code.

boost::circular_buffer<bool> Samples;
boost::circular_buffer<int>::const_iterator itBegin = Samples.begin();
boost::circular_buffer<int>::const_iterator itEnd = Samples.end();

My idea is to get the indices first, then use them to traverse the buffer to print its contents.
Unfortunately, the code does not work, as it seems like I'm getting an error because I used different types (bool and int).
However, I don't think the bool type is appropriate for the iterators.
Could you kindly help me on this? Thank you!
boost::circular_buffer<int>::const_iterator is used to iterate through a boost::circular_buffer<int>.

boost::circular_buffer<bool>::const_iterator is used to iterate through a boost::circular_buffer<bool>.
Hi, thank you for your reply.

The following lines work fine.

boost::circular_buffer<bool> Samples;
boost::circular_buffer<bool>::const_iterator itBegin = Samples.begin();
boost::circular_buffer<bool>::const_iterator itEnd = Samples.end();

However, when I tried to traverse the buffer with the following code, I got an error saying that "cannot convert 'boost::circular_buffer<bool>::const_iterator' to 'int'.
Does that mean the iterators are of bool type? In that case, is there a way to convert them to int type, and use them to print the contents of the buffer?

for (int i = itBegin; i <= itEnd; i++)
cout << Samples[i];
The iterator type is boost::circular_buffer<bool>::const_iterator. To get the element that the iterator is referring to you use the * operator (like with pointers).

1
2
for (boost::circular_buffer<bool>::const_iterator it = Samples.begin(); it != Samples.end(); ++it)
	cout << *it;


Because the iterator type is quite verbose you might want to use auto in these situations to let the compiler deduce the type.

1
2
for (auto it = Samples.begin(); it != Samples.end(); ++it)
	cout << *it;


When iterating though the whole container it is often convenient to use a range-based for loop.

1
2
for (bool sample : Samples)
	cout << sample;
Last edited on
Hi, the codes are working fine. Thank you!

I have the following questions.

- Is there any reason why you use ++it (and not it++)? I read in some forums that pre-increment is more efficient than post-increment. Is this true?

- For the second code, you put "it != Samples.end()" as the for loop condition. Does that mean we're not printing the buffer content corresponding to "Samples.end()"? As opposed to the third code, where I assume we're printing all buffer contents from "Sample.begin()" to "Sample.end()"?
Is there any reason why you use ++it (and not it++)? I read in some forums that pre-increment is more efficient than post-increment. Is this true?

++foo just increments foo and then returns a reference to itself.

foo++ is a bit more complicated. It needs to remember its old value, then increment itself and finally return a copy of the old value.

If you don't use the return value the compiler should be able to optimize the code so that foo++ gives the same code as ++foo. For built-in types like integers and pointers the compiler will have no problem doing this. For more complicated types like iterators I'm not sure, I haven't really investigated. I just use ++foo and don't worry about it, because there is no reason why ++foo would ever be less efficient than foo++.

It's not a huge deal though. The performance gain, if any, is going to be very small compared to everything else. I doubt you will ever be able to tell the difference.

For the second code, you put "it != Samples.end()" as the for loop condition. Does that mean we're not printing the buffer content corresponding to "Samples.end()"? As opposed to the third code, where I assume we're printing all buffer contents from "Sample.begin()" to "Sample.end()"?

The end() iterator is one past the last element. It does not refer to a valid element. This is necessary to allow empty ranges. On an empty container begin() and end() are the same.
Last edited on
Thank you for your detailed explanation. It's now clearer to me, and the issue has been fixed. Thank you again for your help! :)
Topic archived. No new replies allowed.