Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 643
Jonathan Mee Avatar asked Feb 14 '18 13:02

Jonathan Mee


1 Answers

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.

like image 191
Toby Speight Avatar answered Oct 22 '22 08:10

Toby Speight