Introduction to std::string_view/std::span

std::string_view

std::string_view is imported in C++17.

It stores the address of a string and its length. In some cases, it can avoid the overhead of copying strings such as in the parameter of function void foo(const std::string&).

Note that it can only be used for reading, not writing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <string_view>

using namespace std::literals;

void print_string_view(std::string_view sv) {
std::cout << sv << std::endl;
}

int main() {
print_string_view("Hello\0 World");
print_string_view("Hello\0 World"s);
print_string_view("Hello\0 World"sv);

return 0;
}

Output:

1
2
3
Hello
Hello World
Hello World

std::span

std::span is imported in C++20.

It’s similar to std::string_view, and it describes a continuous sequence of memory. The most important thing is that it can be used instead of an array as a function argument, because when we use an array as a parameter (non-reference), it will degrade to a pointer, so we have to pass the length of the array additionally, which is a hassle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <span>

#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))

void print_span(std::span<int> array) {
for (auto item : array) {
std::cout << item << " ";
}
std::cout << std::endl;
}

void print_array(int* array, int length) {
for (int i = 0; i < length; ++i) {
std::cout << array[i] << " ";
}
std::cout << std::endl;
}

int main() {
int array[] = {1, 2, 3, 4, 5};
print_array(array, ARRAY_SIZE(array));
print_span(array);

return 0;
}

References


Introduction to std::string_view/std::span
http://wasprime.github.io/Dev/C++/STL/Introduction-to-std-string-view-std-span/
Author
wasPrime
Posted on
May 2, 2023
Updated on
May 3, 2023
Licensed under