Skip to content

← Containers, Strings, Algorithms step 2 of 5

Medium Primitives

Reading text you do not own

std::string owns its characters. Constructing a string from a character range copies those characters. Calling substr on an existing string also produces an owning string for the selected characters. That may allocate heap memory; short strings can use storage inside the object.

std::string_view lets you read existing characters without copying them. It describes a contiguous range using a pointer and a length. Copying a view does not copy the text, and view.substr(...) creates another view into it.

std::string text = "red,green,blue";
std::string_view record = text;
std::string_view first = record.substr(0, 3);  // refers to "red" in text

Passing text to a function

A function that reads text during a call can take a view by value:

void handle(std::string_view text);

The caller can pass a string, a string literal, or another view. The view is small and cheap to copy. The caller must keep the characters valid for as long as the function uses them.

Keeping the characters alive

A view does not extend the lifetime of its source:

std::string_view bad() {
    std::string local = "temporary";
    return local;  // invalid after local is destroyed
}

std::string_view dangling = std::string("temporary");
// The temporary string is destroyed at the end of the statement.

Destroying the source or reallocating its character buffer invalidates the view. For example, appending to a string can reallocate even though the string object remains alive. Changing a character in place is visible through the view.

Storing or returning a view can be safe when the referenced characters remain valid for every later use, such as a view into a string literal. For this task, both input buffers remain valid throughout the call.

A view does not guarantee a terminating zero

A view may cover only part of a string. In the example above, first.data() points at "red,green,blue", not a separate zero-terminated "red" string. Functions such as strlen cannot see the view's length.

Use an API that accepts a pointer and a length when possible. If an API needs a zero-terminated string, create an owning copy with std::string copy(view) and pass copy.c_str() while that copy is alive. Embedded zero characters still end a C string early.

Your task

int count_fields(std::string_view record, std::string_view wanted);

Return the number of comma-separated fields in record that exactly equal wanted, without heap allocations. Compare characters as given: do not trim whitespace or interpret quotes. Leading, trailing and consecutive commas produce empty fields. An empty record contains one empty field.

Record Wanted Result
red,blue,red red 2
,red, empty string 2
empty string empty string 1

The starter returns the right counts but creates unnecessary owning strings. Replace those copies with views. The harness reports the counts and the number of allocations during the calls; the allocation count must be zero. Some test fields are long enough to expose allocations hidden by short-string storage.