0

I have several items saved in a list. I would like to add items that have already been processed to a datastructure (this makes sense in my case even though you might wonder why). When processing the next item from the list I first want to make sure if it has been processed before so lets say something like this:

if(element_is_in_datastructure(current_element)) {
   do this
}
else
{
   do that
   add_element_to_datastructure(current_element)
}

My question is, what is the ideal datastructure where checking if the element is in it won't take too long. At the moment I don't have too many elements (max 30) which will be added to the datastructure, but this number might increase and I don't want to lose performance.

Jamo
  • 57
  • 6

1 Answers1

1

You can use a map e.g std::unordered_map to store your elements as keys. Then just check their presence e.g

if(!yourMap.count(element))
{
   // your element is not in the structure
}

This finding takes logarithmic time in the map's size to finish.

Hello Everyone
  • 329
  • 4
  • 11