Creating Views
Build views over strings.
Creating Views is a free C++ Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
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";
}From Pointer and Length
You can build a view from a character pointer and an explicit length, useful for buffers that are not null-terminated.
#include <iostream>
#include <string_view>
int main() {
const char buf[] = {'H','e','l','l','o','!'};
std::string_view sv(buf, 5);
std::cout << sv << "\n"; // Hello
}The sv Literal Suffix
The sv suffix (from std::string_view_literals) creates a view directly and handles embedded null characters correctly.
#include <iostream>
#include <string_view>
using namespace std::string_view_literals;
int main() {
auto sv = "ready"sv;
std::cout << sv.size() << "\n";
}Slicing with substr
substr(pos, count) returns a new view into the same buffer, a free way to look at part of the text.
#include <iostream>
#include <string_view>
int main() {
std::string_view sv = "2026-05-30";
std::cout << sv.substr(0, 4) << "\n"; // 2026
}remove_prefix
remove_prefix(n) shrinks the view from the front by advancing the start pointer, no copy involved.
#include <iostream>
#include <string_view>
int main() {
std::string_view sv = " trimmed";
sv.remove_prefix(3);
std::cout << sv << "\n"; // trimmed
}remove_suffix
remove_suffix(n) shrinks the view from the back by reducing its length.
#include <iostream>
#include <string_view>
int main() {
std::string_view sv = "data.csv";
sv.remove_suffix(4);
std::cout << sv << "\n"; // data
}Empty Views
A default-constructed view is empty; empty() reports true and size() is zero.
#include <iostream>
#include <string_view>
int main() {
std::string_view sv;
std::cout << sv.empty() << " " << sv.size() << "\n";
}Copying a View Is Cheap
Assigning one view to another copies only the pointer and length, both still refer to the same characters.
#include <iostream>
#include <string_view>
int main() {
std::string_view a = "shared";
std::string_view b = a;
b.remove_prefix(3);
std::cout << a << " " << b << "\n"; // shared red
}Converting Back to string
When you need to own the data, construct a std::string from the view; this is the point where a copy happens, intentionally.
#include <iostream>
#include <string>
#include <string_view>
int main() {
std::string_view sv = "copy me";
std::string owned{sv};
std::cout << owned << "\n";
}Choosing a Construction Method
Use literals and implicit conversion for everyday cases, the pointer+length form for raw buffers, and substr/remove_* to reslice without allocating.
Quick Check
Check your understanding of building views.
Recap
You learned how to create views:
- From literals, strings, or pointer+length.
- The
svsuffix builds views directly. substr,remove_prefix,remove_suffixreslice for free.- Construct a
std::stringwhen you need to own the data.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Creating Views” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Creating Views”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
What will I learn in “Creating Views”?
Build views over strings. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “Creating Views” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why string_view
- Creating Views
- Pitfalls and Lifetimes
- String Algorithms