Creating Views
Build views over strings.
From a String Literal
The simplest view comes from a string literal. The length is computed automatically.
#include <iostream>
#include <string_view>
int main() {
std::string_view sv = "automatic length";
std::cout << sv.size() << "\n";
}From a std::string
A std::string converts to a view implicitly, letting you observe its contents without copying.
#include <iostream>
#include <string>
#include <string_view>
int main() {
std::string s = "owned text";
std::string_view sv = s;
std::cout << sv << "\n";
}All lessons in this course
- Why string_view
- Creating Views
- Pitfalls and Lifetimes
- String Algorithms