
Learning website for technical articles:https://www.chengxuchu.com
Hello everyone, I am a chef, a programmer who loves cooking and has obtained a chef qualification certificate.
STL accompanies C++ programmers in their daily development, but the most commonly used containers are often <span>vector</span>, <span>map</span>, <span>set</span>, and some of the most common algorithms. Many times, we only stay at the level of “usable” and overlook some very practical but easily ignored features in the standard library. Reflecting on my C++ development experience over the years, I found that mastering some tools earlier could have saved me a lot of detours. Here are a few STL usages that I think are particularly noteworthy.
1. <span>std::string_view</span> — Efficient String View
In the past, when handling strings, I always preferred to pass around <span>std::string</span>, which frequently triggered copies. Later, I discovered that <span>std::string_view</span> is more suitable. It does not manage data, just a view, making it particularly suitable for function parameters, avoiding unnecessary allocations.
#include <iostream>
#include <string>
#include <string_view>
void print_prefix(std::string_view sv) {
std::cout << sv.substr(0, 5) << std::endl;
}
int main() {
std::string s = "Hello, World!";
print_prefix(s); // Directly pass string
print_prefix("Goodbye"); // Directly pass literal
}
This approach makes the interface more flexible and avoids the overhead of temporarily constructing <span>std::string</span>. It is especially useful in scenarios like logging systems and parsing configuration files.
2. <span>std::optional</span> — Clearly Expressing “May Not Exist”
Many people are accustomed to using special values (like <span>-1</span> or null pointers) to indicate function failure or data absence. After C++17, <span>std::optional</span> provides a clearer way to express “there may be a value, or there may not be a value”.
#include <optional>
#include <iostream>
std::optional<int> find_index(int value) {
if (value == 42) return 0;
return std::nullopt;
}
int main() {
auto result = find_index(100);
if (result) {
std::cout << "Found: " << *result << std::endl;
} else {
std::cout << "Not found\n";
}
}
This way, the caller must explicitly handle the “no value” case, significantly improving code readability and safety.
3. <span>std::unordered_map::contains</span> — Concise Lookup Method
In the past, to check if a key exists in an <span>unordered_map</span>, it was always written as:
if (m.find(key) != m.end()) { ... }
After C++20, you can directly use <span>contains</span>:
if (m.contains(key)) {
// More intuitive
}
Although it’s just a small change, the clarity of the code is significantly improved.
4. <span>std::erase_if</span> — The “Batch Cleanup Tool” for Containers
In the past, to delete elements from a container that meet certain conditions, it usually required manually writing <span>remove_if + erase</span>. Now the standard library directly provides <span>std::erase_if</span>, greatly simplifying the code:
#include <vector>
#include <algorithm>
#include <iostream>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5, 6};
std::erase_if(v, [](int x) { return x % 2 == 0; });
for (int x : v) std::cout << x << " "; // Output 1 3 5
}
This small change makes many container operations feel much more natural.
5. <span>std::scoped_lock</span> — Simplifying Multiple Lock Scenarios
When writing multithreaded code, it is often necessary to hold multiple mutexes at the same time. If using <span>std::lock_guard</span>, you have to be careful about deadlock issues. C++17 provides <span>std::scoped_lock</span>, which can lock multiple mutexes simultaneously and guarantees that deadlocks will not occur:
#include <mutex>
std::mutex m1, m2;
void func() {
std::scoped_lock lock(m1, m2); // Locking simultaneously, safe
// Critical section
}
It automatically unlocks when the scope ends, making it more concise than the combination of <span>std::lock</span> + <span>std::lock_guard</span>.
6. <span>std::iota</span> — Quickly Generating Continuous Sequences
Often, it is necessary to generate a continuous array starting from 0, which in the past might have required writing a loop. Using <span>std::iota</span> is more straightforward:
#include <numeric>
#include <vector>
#include <iostream>
int main() {
std::vector<int> v(10);
std::iota(v.begin(), v.end(), 0); // Fill 0..9
for (int x : v) std::cout << x << " ";
}
This function is very convenient in algorithm competitions or when initializing index arrays.
7. <span>std::clamp</span> — Limiting Value Ranges
When writing value range limits, you often see something like:
if (x < 0) x = 0;
else if (x > 100) x = 100;
C++17 provides a more concise <span>std::clamp</span>:
x = std::clamp(x, 0, 100);
These kinds of tools make the code clearer and reduce the likelihood of errors.
The functionalities provided by STL are far more than what we use daily. The ones listed here are just a part of it, reflecting a thought: If the standard library can solve it, do not reinvent the wheel. These tools are often well-tested and optimized, resulting in cleaner code and lower error rates.
If you are also writing C++, take some time to revisit the standard library documentation; you may discover many previously overlooked “gems”.
Recommended reading:
C++ Direct Access to Major Companies
Breaking Down High-Frequency C++ Interview Questions: What Problems Did C++11 Lambda Solve?
Sharing Absurd C++ Interview Questions: Is the vector Object on the Heap or Stack?

Hello, I am a chef! Graduated with a master’s degree in 211, I passed over 30 interviews during the autumn recruitment period and ultimately received more than 10 offers from Baidu, Tencent, Huawei, Shopee, Bilibili, SenseTime, JD, a military research institute, and one of the Big Four banks, mostly at SP or SSP level.
My tags:
- Former Full-Stack Developer at Tencent, now an entrepreneur: Proficient in C++ development, self-rated 80 points.
- Open Source Project Author: One of my open-source repositories has surpassed 10,000+ stars and topped the global trending list for a week.
- Campus Recruitment Offer Collector: During the autumn recruitment period, I received over 10 offers, almost all at SP or SSP level positions.
- Deeply Understand the Confusion and Pain Points of Autumn Recruitment: Therefore, I started the “Xijia Training Camp”, which has been running for 9 months, helping over 150 students significantly improve their C++ skills and successfully obtain their ideal offers.
I particularly understand the confusion and dilemmas everyone faces during autumn recruitment, so I hope to provide a clear direction and some practical methods through my experience.If you also want to stand out in the autumn recruitment, feel free to contact me (vx: chuzi345) to learn about the training camp we are running and join me in preparing for this autumn recruitment challenge!