std::string_view sample
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 <string>
#include <iostream>
#include <string_view>
// we need to overload 'new' to see what's
// hapenning under the hood...
void* operator new(std::size_t n)
{
std::cout << "new " << n << " bytes\n";
return malloc(n);
}
int main()
{
// the original string, one allocation:
std::string str {"Hello Amazing Programming World" };
// another allocation for the substring, separate 'copy'
auto subStr = str.substr(str.find("Programming"));
std::cout << subStr << "\n";
// no allocation for the sub range:
std::string_view strView { str };
auto subView = strView.substr(str.find("Programming"));
std::cout << subView << "\n";
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX