Create a set that orders its elements from smallest to largest. - C++ STL

C++ examples for STL:set

Description

Create a set that orders its elements from smallest to largest.

Demo Code

#include <cinttypes>
#include <iostream>
#include <set>
#include <string>

using namespace std;

class SetObject/*from w w w  . ja v  a 2 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> 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;
}

Result


Related Tutorials