Skip to content

Instantly share code, notes, and snippets.

@eopXD
Last active March 18, 2021 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eopXD/9e8e6a94f272414430b2c75b771c553b to your computer and use it in GitHub Desktop.
Save eopXD/9e8e6a94f272414430b2c75b771c553b to your computer and use it in GitHub Desktop.
std::set containing std::unique_ptr
// C++ 14
#include <iostream>
#include <memory>
#include <set>
// template specialization
template<typename T> struct std::less<std::unique_ptr<T>> {
bool operator()(const std::unique_ptr<T>& a, const std::unique_ptr<T>& b) const {
return *a < *b;
}
};
int main ()
{
std::set<std::unique_ptr<int>> st;
for ( auto element : {1, 3, 1, 5, 13, 8, 2} ) {
st.insert(std::make_unique<int>(element));
}
for ( auto& element : st ) {
std::cout << *element << " ";
} std::cout << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment