Sign in to follow this  

boost / stl algorithm replacement?

Recommended Posts

Is there a neater way to do this with the stl algorithms (and boost) instead of the for loops? I can't really think of one (I thought maybe unique or unique_copy, or something might work, but couldn't see how). It doesn't really matter, but I'm just curious to see if this could be made rather more elegant. Use of boost bind / lambda is fine.

// Component / widget class.
struct TestClass {
	TestClass(bool resizeable, int size):
			resizeable(resizeable), size(size) {}
	bool getResizeable() const {
		return resizeable;
	int getSize() const {
		return size;

	// placeholder for proper overlap comparison
	bool overlaps(int s) const {
		if (s == size) {
			return true;
		return false;
	bool resizeable;

	// placeholder for proper size
	int size;

	// ...
	typedef std::vector<boost::shared_ptr<TestClass> > items_type;
	items_type items;
	// fill container ...
	// Start with a container filled with widgets. Some widgets are automatically layed out (getResizeable == true). 
	// Others have their size specified.

	// Partition between resizeable (auto-layout) items and size specified items.
	items_type::size_type endAutoLayout = std::partition(items.begin(), items.end(), boost::mem_fn(&TestClass::getResizeable)) - items.begin();
	// Go through the size specified items. 
	// If two of them overlap, move one of the items to the start of the partition and increment endAutoLayout, so it'll be layed out automatically too.
	int count = 0;
	for (items_type::size_type i = endAutoLayout; i != items.size()-1; ++i) {
		int size = items[i]->getSize();
		for (items_type::size_type j = i+1; j != items.size(); ++j) {
			if (items[i]->overlaps(items[j]->getSize())) {
				items.insert(items.begin() + endAutoLayout, *(items.begin()+j));
				items.erase(items.begin() + j + 1);
	endAutoLayout += count;
	// ...

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this