Jonathan Mee
Jonathan Mee

Reputation: 38919

Is there a string_view equivalent for other container types?

A string_view is:

An object that can refer to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero.

A typical implementation holds only two members: a pointer to constant CharT and a size.

This allows for robust but lightweight inspection of a string. It's perfect for recursive functions that would otherwise be forced to work with char*s or string::iterators to pare down a string.

My question is what about other containers? Why provide this only for string? What about other contiguous containers, such as vector, map, queue, etc?

Upvotes: 8

Views: 578

Answers (1)

Toby Speight
Toby Speight

Reputation: 30831

A std::string_view is to a pair of iterators as a std::string is to a standard container of char.

In other words, we can use pairs of iterators (or in future, a standard range object) to represent views into standard containers.

The string-view provides extra, string-like functions that mostly parallel the extra, string-like functions that are part of std::string. For other containers, the equivalent operations are generally constructed from standard <algorithm> functions.

Upvotes: 1

Related Questions