mercredi 27 mai 2015

std::set VS std::vector + std::make_heap , when inserting multiple elements

// my vector declared elsewhere like this : 
// std::vector<MyClass> my_vector;
//
// This function adds one or more elements into the vector every time step
// using push_back
accept_new_arrivals();
// 
// make_heap is called every time step
std::make_heap(my_vector.begin(), my_vector.end(),my_comparator);
/* Do something with elements,
 one at a time only using top element,
 and preserving heap property */
do_stuff();

Or I can write:

// My set declared elsewhere like this
// std::set<MyClass, my_comparator> mySet(my_comparator);
//
// This function adds one or more elements into the set every time step
// using insert
accept_new_arrivals();
/* Do something with elements,
 in an ordered fashion */ 
do_stuff();

Which is a better approach if on average I add more than one elements into my set or vector in any timestep?

Definition of better in the current context: efficient when elements are coming in as a stream.

PS: All new arrivals are assumed to be unique.

Aucun commentaire:

Enregistrer un commentaire