开发者

What data structure to use

开发者 https://www.devze.com 2023-03-11 14:31 出处:网络
I\'m looking for the proper data structure for this scenario. I have boost available to use. The code was originally in C#, and I was using a queue there, but I don\'t believe that was an appropriate

I'm looking for the proper data structure for this scenario. I have boost available to use.

The code was originally in C#, and I was using a queue there, but I don't believe that was an appropriate choice, and there isnt a C++ equivalent for C#'s queue as far as I can tell. I'm looking at the following properties in terms of their frequency/importance:

  • Needs to have quick iteration
  • Need to be able to advance the structure quickly (i.e. When i pull an element off the the top, the next element should be the structure head)
  • Will occasionally be cleared then fully repopulated
  • Will occasionally be copied
  • Will not need to be sorted as the elements will be added in the correct order

The number of elements will be known at creation time and will be 50 to 200 elements. The structure will never hold more then this, but may occasionally hold less

I was considering using std::list, but with the need to clear then repopulate occasionally, this doesn't seem to be a good option. When I create a list with a fixed size, then clear it, it loses the prefixed size doesn't it? Is there some way to always keep the list size so it doesn't have to deallocate/allocate memory?

I know boost has a queue data structure, but开发者_开发知识库 it isn't iterable, and I'm not sure if it would have the same problem I had with std::list

Some suggestion on how to fit std::list into my problem or a more appropriate data structure would be helpful.


std::deque seems to meet all of your requirements.

If performance is a real issue for you, you should read GMan's answer to Pre-allocate space for C++ STL queue .


Sounds like a ring buffer should work? There is one in boost: boost::circular_buffer.


Ring buffer (implemented as static array Foo buffer[200] and two counters), as suggested by eevar would be the best. No need for STL/Boost.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号