Topic 83 of 83
Modern C++
Overview
C++ was completely revolutionized starting with C++11. Knowing these features shows you write clean, modern, performant code rather than legacy 1990s C++.
Syntax
cpp
// C++11: auto, lambdas, range-based for loops, smart pointers, move semantics
auto num = 42;
// C++14: Generic lambdas, return type deduction
auto add = [](auto a, auto b) { return a + b; };
// C++17: Structured bindings, std::optional, std::variant
std::pair<int, double> p = {1, 3.14};
auto [integer, decimal] = p; // Structured binding
// C++20: Concepts, Ranges, Modules, Coroutines
#include <ranges>
// auto result = data | std::views::filter(...) | std::views::transform(...);Common Pitfalls
- Writing C-style code in a C++ compiler. Modern C++ heavily discourages raw arrays, raw pointers, `#define` macros, and manual loops when algorithms can be used.
Interview Tips
- Move Semantics (C++11) is the most critical performance feature. It allows 'stealing' resources from temporary objects (using `std::move`) instead of performing expensive deep copies.
Real-World Example
Using C++17 Structured Bindings for clean iteration.
example
cpp
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> scores = {{"Alice", 100}, {"Bob", 80}};
// C++17 structured binding directly unpacks the pair!
for (const auto& [name, score] : scores) {
cout << name << " got " << score << endl;
}
return 0;
}