Boost logo

Boost Users :

Subject: Re: [Boost-users] [circular_buffer] About overwriting elements
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2010-04-16 10:49:42


AMDG

Daniele Barzotti wrote:
> in the introductory example I see this:
>
> int a = cb[0]; // a == 1
> int b = cb[1]; // b == 2
> int c = cb[2]; // c == 3
>
> // The buffer is full now, pushing subsequent
> // elements will overwrite the front-most elements.
>
> cb.push_back(4); // Overwrite 1 with 4.
> cb.push_back(5); // Overwrite 2 with 5.
>
> // The buffer now contains 3, 4 and 5.
>
> a = cb[0]; // a == 3
> b = cb[1]; // b == 4
> c = cb[2]; // c == 5
>
> and I would ask why the 4 and 5 are in the 2nd and 3th position instead
> of 1st and 2nd respectively.
>
> I thought that it worked in the following way:
>
> 1 2 3 1 2 3 1 2 3
> [A][B][C] -> push_back(D) -> [D][B][C] -> push_back(E) -> [D][E][C]
>
> I need a circular buffer for buffering network data and I need that the
> old data is replaced but doesn't change its position..
>

What actually happens is something like this:

 1 2 3 3 1 2 2 3 1
[A][B][C] -> push_back(D) -> [D][B][C] -> push_back(E) -> [D][E][C]

The indexes are adjusted, but the physical locations of the elements
don't change.

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net