Dave's Brain

Browse - Programming Tips - How do I keep the elements of an STL vector always in order?

Date: 2007dec10, 2010jan24
Language: C/C++
Keywords: sorted, vector

Q.  How do I keep the elements of an STL vector always in order?

A.  I have had good success with Martin Holzherr's sorted_vector
template:

http://www.codeproject.com/KB/stl/sorted_vector.aspx
It's far faster than doing std::sort() after each push_back(). Example use:
#include "sorted_vector.h" typedef codeproject::sorted_vector<int> INTS; void exampleUse() { INTS a; a.insert(3); // Use insert() instead of push_back() a.insert(2); a.insert(1); }
Use STL's binary_search() for lookups.

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.