Re: Fixed size vector
Andrea Crotti <andrea.crotti.0@gmail.com> writes:
I would need something in theory quite simple, a vector of a certain
size, which, when I add something and it's full, simply deletes the last
entry.
If for example I have
MyContainer x(3);
x.push(1);
x.push(2);
x.push(3);
x.push(4);
it should contain now
<4, 3, 2>
I don't see any of the STL structures doing exactly what I would like,
any suggestions on how to implement it?
The whole story (which is maybe more helpful) is that I have a vector of
vectors of a certain maximal size
<1, 2, 3>
<2, 2, 3>
....
and I have to compute a vector which is the average of all of them.
Now what I did is a vector of vectors and using a map to count up the
values for after doing the average
--8<---------------cut here---------------start------------->8---
// if it's a landmark one 0 should always be so
std::map<coord_t, int> land_hops;
for (int i=0; i < history_size; ++i) {
for (int j=0; j < address[i].getSize(); ++j) {
coord_t hop_count = address[i][j];
// if it's not reachable or == 0 then it should not be inside the average
if (hop_count > 0) {
if (land_hops.count(j) == 0) {
land_hops[j] = hop_count;
} else {
land_hops[j] += hop_count;
}
}
}
--8<---------------cut here---------------end--------------->8---
but it's really ugly and not very smart probably, better ideas??