C++ examples for STL:set
Adding elements to a set
#include <cinttypes> #include <functional> #include <iostream> #include <set> #include <string> using namespace std; class SetObject/*from ww w .j a v a2 s. c o m*/ { 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; } }; 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; } cout << endl; mySet.emplace(SetObject( 5, "Five" )); for (auto&& number : mySet) { cout << number.GetName() << endl; } cout << endl; auto iter = mySet.find(3); if (iter != mySet.end()) { cout << "Found: " << iter->GetName() << endl; } return 0; }