|
Prev: Scrollbar Thumb Size
Next: Arrays
From: Gerhard Wolf on 7 Dec 2007 04:09 Hi, currently i deal with the C++ Standard Template Library and i wonder how i can realize a container with n elements that drops the oldest when the n+1 values is added. Is there already a container type or do i have to realize this? A vector pop_back that removes the "first" not the last element of a vector would be fine!
From: Ian Collins on 7 Dec 2007 04:31 Gerhard Wolf wrote: > Hi, > > currently i deal with the C++ Standard Template Library and i wonder how > i can realize a container with n elements that drops the oldest when the > n+1 values is added. Is there already a container type or do i have to > realize this? > > A vector pop_back that removes the "first" not the last element of a > vector would be fine! Use a std::list and pop_front()? -- Ian Collins.
From: Gerhard Wolf on 7 Dec 2007 04:39 Ian Collins schrieb: > Gerhard Wolf wrote: >> Hi, >> >> currently i deal with the C++ Standard Template Library and i wonder how >> i can realize a container with n elements that drops the oldest when the >> n+1 values is added. Is there already a container type or do i have to >> realize this? >> >> A vector pop_back that removes the "first" not the last element of a >> vector would be fine! > > Use a std::list and pop_front()? > Ok thanks i will have a look at the list container. I just found out that i can use a vector and do somthing like the_vector.push_back(value); if (the_vector.size() > x) the_vector.erase(the_vector.begin());
From: "Sebastian "psy" Messerschmidt" on 7 Dec 2007 05:10 Gerhard Wolf schrieb: > Ian Collins schrieb: >> Gerhard Wolf wrote: >>> Hi, >>> >>> currently i deal with the C++ Standard Template Library and i wonder how >>> i can realize a container with n elements that drops the oldest when the >>> n+1 values is added. Is there already a container type or do i have to >>> realize this? >>> >>> A vector pop_back that removes the "first" not the last element of a >>> vector would be fine! >> >> Use a std::list and pop_front()? >> > Ok thanks i will have a look at the list container. I just found out > that i can use a vector and do somthing like > > the_vector.push_back(value); > if (the_vector.size() > x) the_vector.erase(the_vector.begin()); You probably don't want to do that, since this might result in reallocating memory for the whole vector (IMHO). std::list will allocate memory for each entry without copying. Maybe you should provide the scope of what you are trying to achieve - Performance, hence the container you are should use heavily depends on which kind of operations are performed often on your data structure. (E.g. random-access to an element of an vector is O(1) while for a list this would be O(n). hth psy
From: Daniel T. on 7 Dec 2007 06:12
In article <5rsiloF169s6lU1(a)mid.individual.net>, Gerhard Wolf <quisquiliae(a)gmx.de> wrote: > Ian Collins schrieb: > > Gerhard Wolf wrote: > >> Hi, > >> > >> currently i deal with the C++ Standard Template Library and i wonder how > >> i can realize a container with n elements that drops the oldest when the > >> n+1 values is added. Is there already a container type or do i have to > >> realize this? > >> > >> A vector pop_back that removes the "first" not the last element of a > >> vector would be fine! > > > > Use a std::list and pop_front()? > > > Ok thanks i will have a look at the list container. I just found out > that i can use a vector and do somthing like > > the_vector.push_back(value); > if (the_vector.size() > x) the_vector.erase(the_vector.begin()); Use a deque instead: the_deque.push_back( value ); if ( the_deque.size() > x ) the_deque.pop_front(); |