Non-Owning std::string_view (C++17)

🟡 Intermediate

📖 Definition

Introduced in C++17, std::string_view (in <string_view>) provides a non-owning read-only pointer + length reference to any sequence of characters (C-string, string literal, or std::string), eliminating temporary dynamic string allocations.

💡 Code Example

#include <iostream>
#include <string_view>

void printSV(std::string_view sv) {
    std::cout << "View: " << sv << " (Len: " << sv.length() << ")\n";
}

int main() {
    printSV("Literal String"); // Zero allocation
    std::string str = "Dynamic String";
    printSV(str); // Zero allocation
    return 0;
}