Strings

std::string and std::string_view

While C-style string literals are fine, C-style bariables behave oddly and are hard to work with and dangerous. They are best avoided. For this, std::string and std::string_view (C++17) have been added.

Becaust std::string is expensive to initialize (or copy), std::string_view has been introduced. It provides read-only access to an existing string without making a copy. So it's preffered to use over std::string when we need a string that won't change. It's important that the object being viewed isn't destroyed and isn't changed, because it causes undefined behaviour.

Best way to se std::string_view is as a read-only function parameter. This way the argument is passed without making a copy, regardless of the type of string.

String input

Since std::cin gets input until whitespace, we can use std::getline to read a full line of text.

std::getline(std::cin >> std::ws, name);

The std::ws is needed so that the '\n' character isn't captured by std::cin.