Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which C++ data structure should I use for end-only access and no relocations?

I have an unusual set of requirements for my C++ application. I need a container that:

  • Is generic, like std::vector
  • Can have elements added to the end
  • Can have elements removed from the end
  • Never moves the memory location of elements
  • Does not need to provide general purpose access to elements (I keep pointers for this)
  • Takes ownership over its elements.

Is there a data-structure with these properties?

like image 483
sdgfsdh Avatar asked Dec 12 '25 14:12

sdgfsdh


1 Answers

There is std::deque that matches your requirements:

std::deque (double-ended queue) is an indexed sequence container that allows fast insertion and deletion at both its beginning and its end. In addition, insertion and deletion at either end of a deque never invalidates pointers or references to the rest of the elements.

Also, std::list.

like image 153
Maxim Egorushkin Avatar answered Dec 14 '25 03:12

Maxim Egorushkin