string_view & std::format in C++

From the C++ cheat sheet · Strings · verified Jul 2026

string_view & std::format

Cheap views and modern formatting.

cpp
// Quick Reference
#include <string_view>
#include <format>
void log(std::string_view msg);      // no copy
std::string s = std::format("{} = {}", "x", 42);
💡 Take std::string_view for read-only string params - it avoids copies and binds to literals.
⚡ std::format (C++20) is the type-safe successor to printf and stringstream.
📌 Never return a string_view into a temporary - it dangles once the owner dies.
🟢 std::print (C++23) formats directly to stdout; std::format returns a std::string.
string-viewformat

More C++ tasks

Back to the full C++ cheat sheet