Which has faster access Binary Search of std::vector or std::set ???

  • Follow


Which has faster access Binary Search of std::vector or 
std::set ???
I would gess that the former might be slightly faster and 
take less memory. 


0
Reply Peter 2/27/2010 6:36:48 PM

Peter Olcott wrote:
> Which has faster access Binary Search of std::vector or 
> std::set ???
> I would gess that the former might be slightly faster and 
> take less memory. 

The std::vector for sure. It is also optimal in space. However, you 
choose your data structure depending on what features you need. 
Currently you are only requiring to find an element from a static set. 
Add the requirement of dynamically adding or removing elements and you 
will be better using std::set.

-- 
http://kaba.hilvi.org
0
Reply Kaba 2/27/2010 11:50:49 PM


On Feb 27, 12:36=A0pm, "Peter Olcott" <NoS...@OCR4Screen.com> wrote:
> Which has faster access Binary Search of std::vector or
> std::set ???
> I would gess that the former might be slightly faster and
> take less memory.

Binary search works on sorted data.  The basic std::vector doesn't
impose any sorting order on its contents, so you would have to sort
them first (or insert them in a sorted manner).  std::set forces its
contents to be sorted, so it is always ready for binary search.
Perhaps this is what Kaba was pointing out.
0
Reply John 3/1/2010 11:05:28 PM

2 Replies
548 Views

(page loaded in 0.066 seconds)

Similiar Articles:













7/20/2012 5:26:28 AM


Reply: