C++ examples for STL:set
Ordering elements in a set from highest to lowest
#include <cinttypes> #include <functional> #include <iostream> #include <set> #include <string> using namespace std; class SetObject/*w ww . j av a2 s . c om*/ { private: string m_Name; int32_t m_Key{}; public: SetObject(int32_t key, const string& name) : m_Name{ name } , m_Key{ key } { } SetObject(int32_t key) : SetObject(key, "") { } const string& GetName() const { return m_Name; } int32_t GetKey() const { return m_Key; } bool operator<(const SetObject& other) const { return m_Key < other.m_Key; } bool operator>(const SetObject& other) const { return m_Key > other.m_Key; } }; using namespace std; int main(int argv, char* argc[]) { set<SetObject, greater<SetObject>> mySet { { 6, "Six" }, { 3, "Three" }, { 4, "Four" }, { 1, "One" }, { 2, "Two" } }; for (auto&& number : mySet) { cout << number.GetName() << endl; } auto iter = mySet.find(3); if (iter != mySet.end()) { cout << "Found: " << iter->GetName() << endl; } return 0; }