Learning set, unordered_set, and priority_queue in C++

Published: (January 5, 2026 at 12:42 PM EST)
1 min read
Source: Dev.to

Source: Dev.to

set

Overview

  • Unique + Sorted data
  • Stores unique elements
  • Maintains sorted order
  • Implemented using a self‑balancing BST (Red‑Black Tree)

Example

std::set

#include 
#include 

int main() {
    std::set s = {3, 1, 2, 2};
    for (int x : s) std::cout 
}

std::unordered_set

#include 

int main() {
    std::unordered_set us = {3, 1, 2, 2};
    for (int x : us) std::cout 
}

std::priority_queue (max‑heap)

int main() {
    std::priority_queue pq;
    pq.push(3);
    pq.push(1);
    pq.push(5);
    std::cout 
}

Min‑heap with std::greater

#include 
#include 

std::priority_queue, std::greater> minHeap;

Complexity

  • Push / Pop: O(log n)
  • Top: O(1)
  • Space: O(n)

When to use

  • Need k‑th largest / smallest element
  • Repeated max/min extraction
  • Scheduling & greedy problems
Back to Blog

Related posts

Read more »

Day 0: Starting My DSA Journey

Day 0 – Planning, Mindset & Commitment - Goal: Become strong in problem‑solving & interview‑ready - Starting level: Beginner / Revising basics - Daily commitme...