#include <iostream>
#include <list>
#include <queue>
#include <vector>

struct Foo {
	Foo(int _a) : a(_a) {}
	int a;
};

struct FooComparator {
	bool operator()(Foo *a, Foo *b) { return a->a < b->a; }
};

int main() {
	std::list<Foo> foos;
	//std::vector<Foo> foos; // when used instead, the behaviour will become undefined
	std::priority_queue<Foo *, std::vector<Foo *>, FooComparator> pq;
	
	// Simulate creation and 'containment' of objects, while they are being processed by other structures.
	for(int i=0; i<100; ++i) {
		foos.push_back(Foo((100-i) % 10));
		pq.emplace(&foos.back());
	}
	
	while(not pq.empty()) {
		std::cout << pq.top()->a << " "; // the dereference -> may segfault if foos is not *pointer stable*
		pq.pop();
	}
	
	std::cout << std::endl;
	return 0;
}