Skip to content

Instantly share code, notes, and snippets.

@eopXD
Last active Mar 18, 2021
Embed
What would you like to do?
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