Last active
March 18, 2021 10:23
-
-
Save eopXD/9e8e6a94f272414430b2c75b771c553b to your computer and use it in GitHub Desktop.
std::set containing std::unique_ptr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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