Dave's Brain

Browse - programming tips - stl sorted vector

Date: 2007dec10, 2010jan24
Language: C/C++

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);
	}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, 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.
Advertisements: