C++C++ Explainer
C++ · 1 · Foundations1 / 36 · 3%

Hello, Modern C++

iostream, namespaces, auto — the modern C++ starter kit.

shortcuts: ← prev · → next · M mark
1

Hello, World

std::cout streams text; endl flushes.

Example
hello.cpp
#include <iostream>

int main() {
    std::cout << "Hello, World!\n";
    return 0;
}
Output
Hello, World!
2

namespaces

std:: prefixes standard library. Prefer explicit std:: over using namespace std;.

Example
example
#include <string>
#include <iostream>

int main() {
    std::string name = "Ada";
    std::cout << "Hi, " << name << '\n';
}
3

auto & Range for

Let the compiler infer the type — cleaner and safer than repeating it.

Example
example
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v{1, 2, 3, 4};
    for (auto x : v) std::cout << x << ' ';
}
Output
1 2 3 4
4

Build

Compile with a modern standard flag.

Syntax
shell
g++ -std=c++20 -Wall -Wextra -O2 hello.cpp -o hello
./hello