0Pricing
C++ Academy · Lesson

Creating Views

Build views over strings.

Creating Views is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 sv suffix builds views directly.
  • substr, remove_prefix, remove_suffix reslice for free.
  • Construct a std::string when you need to own the data.

Frequently asked questions

Is the “Creating Views” lesson free?

Yes — the full text of “Creating Views” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Why string_view
  2. Creating Views
  3. Pitfalls and Lifetimes
  4. String Algorithms
← Back to C++ Academy