-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_view.cpp
More file actions
71 lines (63 loc) · 2.18 KB
/
Copy pathstring_view.cpp
File metadata and controls
71 lines (63 loc) · 2.18 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <string_view>
void printSV(std::string_view str){
std::cout << str << '\n';
}
int main(){
// // int x {5};
// // std::cout << x << '\n';
// // std::string s{ "Hello, world!" }; // s makes a copy of its initializer
// // std::cout << s << '\n';
// std::string_view s {"Hello, world!"};
// printSV(s);
// std::string_view s1 { "Hello, world!" }; // initialize with C-style string literal
// std::cout << s1 << '\n';
//
// std::string s0{ "Hello, world!" };
// std::string_view s2 { s0 }; // initialize with std::string
// std::cout << s2 << '\n';
//
// std::string_view s3 { s2 }; // initialize with std::string_view
// std::cout << s3 << '\n';
// std::string str {s};
// printSV(str);
// std::cout << static_cast<std::string>(s);
// //assignment changes what std::string_view is viewing but not the value it is viewing!
// std::string name {"Jay"};
// std::string_view sv {name};
// std::cout << sv << '\n';
//
// sv = "Ali";
// std::cout << sv << '\n';
//
// std::cout << name << '\n'; // this will print Jay
// using namespace std::string_literals;
// using namespace std::string_view_literals;
//
// std::cout << "foo\n"; // no suffix is a C-styl literal
// std::cout << "goo\n"s; // s suffix is a std::string literal
// std::cout << "moo\n"sv; // sv suffix is a std::string_view literal
// std::string something {"Hello, world!"};
// std::cout <<something << '\n';
// std::string_view something_sv {something};
// std::cout << something_sv << '\n';
// something = "Spartaaaa!";
// std::cout << something << '\n';
// std::cout << something_sv << '\n';
//
std::string_view sv{};
{ // create a nested block
std::string s{ "Hello, world!" }; // create a std::string local to this nested block
sv = s; // sv is now viewing s
} // s is destroyed here, so sv is now viewing an invalid string
std::cout << sv << '\n'; // undefined behavior
std::string_view str_view {"Peach"};
std::cout << str_view << '\n';
str_view.remove_prefix(1);
std::cout << str_view << '\n';
str_view.remove_suffix(2);
std::cout << str_view << '\n';
str_view = "Peach";
std::cout << str_view << '\n';
return 0;
}