💻
Programming Concept
  • 👨‍🔬About Me
  • 📗C++ STL Concept 2023
    • 📖STL in CPP ( Part I)
    • 📖STL in CPP ( Part II)
    • 📖STL in CPP ( Part III )
    • 📖STL in CPP ( Part IV )
    • 📖STL in CPP ( Part V )
    • 📖STL in CPP ( Part VI )
    • 📖STL in CPP ( Part VII )
  • ✏️Pointers in C / C++
    • 📖Pointers in details
  • 📘Strings in CPP 2023
  • 🕞Thread in C++ 2023
  • 📓Smart Pointer in C++ 2023
  • Basic C++ Topics
    • 💼Type Casting in C++ 2023
  • 💻Advanced C++ Programming
    • 🎓Topics (Syllabus)
    • 🧑‍🎓Inheritance and Polymorphism
    • 🖊️Exception Handling
    • 🏃Runtime Type Information
    • 📔An Overview of Templates
    • 📡Template in C++
  • 💻Embedded Linux
    • 🖥️Embedded Linux OS
      • Build Yocto Project Step By Step
      • How to install apt-get to the Yocto Project image
      • Installing gcc/g++ toolchain in yocto project
      • Installing GDB in yocto project
      • Installing ssh-server-dropbear for embedded linux in yocto project
      • Add Python To A Build
      • Add Boost To A Build
      • Adding Valgrind To Build
      • How To Add A Custom App To A Yocto Build
      • Raspberry Pi
    • 📶Communication Protocols
      • ✏️Working With I2C using c++
      • 📔SPI Implementation with source code in C++
      • 📓Linux Serial Ports Using C/C++
      • 🖱️UART
      • 🖨️Universal GPIO Access
  • 🧑‍💻QT QML
    • 📘QT QML Concept 2023
      • Why Qt Framework and QML
      • Sqlite3 for qtquick application
      • How To Install sqlite3 in windows 11 || Windows 10
      • Signals and Slots in Qt QML
      • Working with Signals and Slots in QML
      • Sorting QML ListModels
      • ListView In QML With Section Delegate
      • Registration of custom enum in QML
      • Registering a QML Type as a Singleton object
      • Using enumerations in QML without C ++
      • Registering a Singleton object to use “Static” methods in QML
      • Connecting JavaScript files to other JavaScript files in a Qt / QML project
      • Positioning in Qt QML with anchors
      • TextInput validators in Qt Qml
      • Qt Qml Abstract Item Model using C++
      • Qt QML List Model
      • How to Register your C++ Class as a QML Type
      • Jabra Speaker Connect Qt Project
      • Qt | One article summarizing QObject
  • ▶️QT Concept
    • ℹ️Icon button
    • 💻Register QML Singletone in CP
Powered by GitBook
On this page

Was this helpful?

  1. C++ STL Concept 2023

STL in CPP ( Part IV )

STANDARD TEMPLATE LIBRARY II - SET - 2023

Standard Template Library (STL) II - Associative Containers: Set

Sets and multisets sort their elements automatically. Sets do not allow duplicates while multisets allow duplicates.

#include <iostream>
#include <set>

using namespace std;

struct Compare
{
    bool operator()(const int &a;, const int &b;) const
    {
        return a % 10 < b % 10;
    }
};

int main()
{
    const int a[] = {8, 2, 24, 9, 7, 3, 4, 6, 15, 2};
    const int count = sizeof(a) / sizeof(a[0]);
    for(int i = 0; i < count; i++)
	cout << a[i] << " ";
    cout << endl;

    set<int> s(a, a + count); 
    set<int>::iterator it;
    for(it = s.begin(); it != s.end(); ++it)
	cout << *it << " ";
    cout << endl;

    set<int, Compare>::iterator sit;
    set<int, Compare> new_s(s.begin(), s.end());
    for(sit = new_s.begin(); sit != new_s.end(); ++sit)
	cout << *sit << " ";
    return 0;
}

Output:

8 2 24 9 7 3 4 6 15 2
2 3 4 6 7 8 9 15 24
2 3 4 15 6 7 8 9

As we see from the output of the second line, set srots its element automatically and removes any duplicates. The third line was sorted with the last digits.

PreviousSTL in CPP ( Part III )NextSTL in CPP ( Part V )

Last updated 1 year ago

Was this helpful?

📗
📖
Page cover image