Urgent.News

What's breaking now, across thousands of outlets.

Tech

The Fellowship of the STL: C++ Data Structures Every Competitive Programmer Needs

The Quest Begins (The "Why") I still remember my first ICPC‑style contest. I was cruising through a problem that needed a dynamic median, and I reached for a priority_queue like a trusty sword. I popped the top, pushed the next value, and felt like I’d solved it—until the judge returned Wrong Answer . After hours of staring at the output, I realized I’d been throwing away half the data every time…

The Quest Begins (The Why)

In the author's early competitive programming days, a dynamic median problem proved elusive. They relied on a priority_queue, but each pop destroyed half the data. After hours of frustration, they discovered the heap's hidden container, leading to a pivotal moment in their journey.

The Revelation (The Insight)

1. Priority Queue's Secret Container

The author explains that priority_queue's standard interface hides its underlying container. By inheriting from it, you gain access to iterate, clear, or re-heapify manually. This insight is crucial for problems requiring heap manipulation beyond popping the top element.

2. Unordered Map's Reserve & Load-Factor Control

Unordered_map offers O(1) average performance for insertions and lookups. However, its efficiency can degrade if rehashing occurs too frequently. The author reveals that pre-allocating buckets with reserve(n) and setting max_load_factor(z) can prevent unnecessary rehashes, saving precious time in contests.

3. Vector's Capacity Tricks – Shrink-to-Fit & Swap Idiom

Unlike priority_queue and unordered_map, vector's clear() does not release allocated memory. This can lead to wasted memory when reusing the vector across multiple test cases. The author introduces two techniques to truly shrink a vector: shrink_to_fit() and the swap trick. Both methods ensure minimal memory usage, a crucial consideration in competitive programming environments with strict memory limits.

Wielding the Power (Code & Examples)

The author provides code snippets demonstrating the practical application of these STL features. These examples showcase how to implement an inspectable priority_queue, optimize unordered_map performance, and manage vector capacity efficiently in competitive programming scenarios.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

35 free developer tools that run entirely in your browser — no signup, no uploads

Two things always bothered me about the usual "paste your JSON here" websites: half of them quietly upload your data to a server, and the other half drown you in ads.

  • JSON Formatter formats, validates, and minifies JSON locally in browser
  • JWT Decoder decodes authentication tokens without sharing sensitive data
  • Regex Tester provides live highlighting and capture groups for regex patterns

Building a Browser Pitch Detector (and Reusing It for a Guitar Tuner)

01 · Why this is harder than it looks Every musician has used a phone tuner app. Building one in the browser sounds straightforward: grab the mic, run FFT, show the note. Ship it.

  • Browser microphone features interfere with pitch detection
  • Three-layer pipeline includes mic input, audio graph, engine
  • React UI handles pitch display with throttled updates

How Tailscale's Route Table Gets Wiped by mwan3 on OpenWrt

My trusty Netcore N60 Pro runs on OpenWrt 25.12 with both mwan3 and tailscale installed. Tailscale would work fine, then randomly stop being reachable.

  • Tailscale route table wiped by mwan3 on OpenWrt
  • MWAN3INTERFACEMAX value causes table 52 to be wiped
  • Solution involves adjusting mmxmask value

More from Saturday 10 October →