Skip to content

Commit ae42383

Browse files
authored
Update 003_stl_std_hash_std_u16string.cpp
1 parent dbcf8a0 commit ae42383

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,24 @@
1-
1
1+
#include <iostream>
2+
#include <string_view>
3+
#include <unordered_set>
4+
using namespace std::literals;
5+
6+
int main() {
7+
std::cout << "\"A\" #: " << std::hash<std::string_view>{}("A"sv) << '\n';
8+
std::cout << "L\"B\" #: " << std::hash<std::wstring_view>{}(L"B"sv) << '\n';
9+
std::cout << "u8\"C\" #: " << std::hash<std::u8string_view>{}(u8"C"sv) << '\n';
10+
std::cout << "u\"D\" #: " << std::hash<std::u16string_view>{}(u"D"sv) << '\n';
11+
std::cout << "U\"E\" #: " << std::hash<std::u32string_view>{}(U"E"sv) << '\n';
12+
13+
// std::hash for string_view family makes it possible to keep these view-types
14+
// in unordered_* associative containers, such as unordered_set. But ensure
15+
// the lifespan of referenced strings is no less than lifespan of the container,
16+
// i.e. no dangling references occurred.
17+
18+
std::unordered_set stars{"Rigel"sv, "Capella"sv, "Vega"sv, "Arcturus"sv};
19+
20+
for (std::string_view const& s : stars) {
21+
std::cout << s << ' ';
22+
}
23+
std::cout << '\n';
24+
}

0 commit comments

Comments
 (0)