0Pricing
C++ Academy · Lesson

Why string_view

Avoid string copies.

Why string_view is a free C++ Academy lesson on CoddyKit — lesson 1 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.

The Cost of Copies

Passing std::string by value copies the whole buffer. For read-only access that copy is wasted work. std::string_view is a lightweight, non-owning view, just a pointer and a length. Include <string_view>.

#include <iostream>
#include <string_view>
int main() {
    std::string_view sv = "hello";
    std::cout << sv << " len=" << sv.size() << "\n";
}

What a View Holds

A string_view stores only a pointer to existing characters and a length. It does not own or copy the data.

#include <iostream>
#include <string>
#include <string_view>
int main() {
    std::string s = "world";
    std::string_view sv = s; // views s, no copy
    std::cout << sv << "\n";
}

Read-Only Parameters

Accepting std::string_view means your function works with std::string, string literals, and char arrays, without copying any of them.

#include <iostream>
#include <string_view>
void greet(std::string_view name) {
    std::cout << "Hi, " << name << "\n";
}
int main() {
    greet("Ada");
    greet(std::string("Bob"));
}

No Allocation

Creating a view never allocates memory, so it is cheap to copy and pass around by value.

#include <iostream>
#include <string_view>
int main() {
    std::string_view a = "constant text";
    std::string_view b = a; // trivial copy
    std::cout << (a == b) << "\n";
}

Cheap Substrings

substr on a view returns another view into the same memory, no characters are copied.

#include <iostream>
#include <string_view>
int main() {
    std::string_view sv = "abcdef";
    std::cout << sv.substr(2, 3) << "\n"; // cde
}

Comparing Performance Intent

For functions that only inspect text, string_view avoids both heap allocation and copying, which matters in hot loops and parsing.

Length and Indexing

A view supports the familiar interface: size(), empty(), indexing, and range-based iteration.

#include <iostream>
#include <string_view>
int main() {
    std::string_view sv = "C++";
    for (char c : sv) std::cout << c << "-";
    std::cout << "\n" << sv.size() << "\n";
}

Views Are Read-Only

You cannot modify characters through a string_view; it only grants read access. To mutate, you need the owning string.

From Literal to View

String literals convert to views directly, which makes passing constant text extremely cheap.

#include <iostream>
#include <string_view>
using namespace std::string_view_literals;
int main() {
    auto sv = "literal"sv;
    std::cout << sv.size() << "\n";
}

Not a Replacement for string

Use std::string when you need to own, store, or modify text. Use std::string_view for temporary, read-only parameters.

Lifetime Caveat

Because a view does not own its data, the underlying characters must outlive the view. Misusing this causes dangling views, covered in a later lesson.

Quick Check

Check your understanding of string_view motivation.

Recap

You learned why string_view exists:

  • It is a non-owning pointer + length view.
  • Accepting it avoids copies and allocations for read-only text.
  • Works with strings, literals, and char arrays.
  • The viewed data must outlive the view.

Frequently asked questions

Is the “Why string_view” lesson free?

Yes — the full text of “Why string_view” 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 “Why string_view”?

Avoid string copies. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Why string_view” 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