C++ strings and string_view

C-style strings

C style strings should be avoided except when interfacing with C libraries. C string library functions provide no bounds checking and memory allocation support. They are represented as an array of characters. Last character of the string is the null character \0, so that code that uses the string knows where it ends. The space needed for a string is always one more than the number of readable characters.

String Literals

Strings written with quotes around them are string literals. They are stored in a read-only part of memory. Because they are stored in readonly sections, attempting to modify string literals is undefined behavior. Example:
char *str = "world";
str[0] = 'y'; //undefined behavior
If the code respected the standard and assigned the string literal to const char*, the compiler will catch attempts to modify string literals:
const char * str = "world";
str[0] = 'k'; //compiler will flag this as error. 
To mutate strings, assign them to character arrays. In this example, the compiler creates an array large enough to hold the string and copies the string to the array. The literal is not put in read only memory.
char str[] = "hello world";
str[0] = 'y';

Raw string literals

C++ offers raw string literals which are literals that span multiple lines of code, and don't require escaping of embedded double quotes, and don't treat escape sequences with any special logic. Example:
const char* str = R"(Brave New World)";
const char* str = R"(Line1: Brave 
Line2: New World)";
To use ) or ( in raw string literals, use unique delimiter sequences like the following:
const char* str = R"-(Embedded ) parens)-";

std::string

string has + operator overloaded to mean concatenation. So the following produces hello world:
string one("hello");
string two("world");
string final;
final = one + " " + two;
Furthermore, ==!=<, etc are all overloaded so they use the actual string characters. For compatibility one can call c_str() to return a C style string, but this should be the last operation and should never be done an stack allocated string. Because string literals are inferred as const char*, one can use s to interpret a string literal as std::string:
auto myStr = "hello brave world"s;

Conversions

The std namespace has several functions to convert numbers into strings. Examples : string to_string(int vaL) and string to_string(double val). To convert from strings to numbers, use functions such as int stoi(const string& s, size_t *idx = 0, int base=10). Example:
const string toConvert = " 123ABC";
size_t index = 0;
int value = stoi(toConvert,&index); //index will be the index of 'A'.

std::string_view

string_view is a readonly string but without the overhead of a const string&. It doesn't copy strings. To concatenate a string_view with a string, use the data() member function:
string str = "Hard";
string_view sview = " Rock";
auto result = str + sview.data();
To pass string_views into functions, pass them by value. For string_view literals, use "sv":
auto sview = "Sample string view"sv;
string_view extractLastDelimitedElement(string_view sv, char delimiter)
{
    return sv.substr(sv.rfind(delimiter));
}

References:

Gregoire, M. (2018). Professional C++. Indiana, IN: John Wiley & Sons.

Comments

Popular posts from this blog

QTreeView and QTableView dynamic changes