All Contiguous-Container and View Relationships

All Contiguous-Container and View Relationships

A compact scan page for how vector, array, string, string_view, span, and mdspan relate through contiguous storage and borrowing semantics.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

All Contiguous-Container and View Relationships

Owning contiguous containers

Non-owning contiguous views

Typical relationships

Practical rules

Small worked example

#include <span>
#include <vector>

void scale(std::span<int> values) {
	for (int& value : values) {
		value *= 2;
	}
}

int main() {
	std::vector<int> values{1, 2, 3};
	scale(values);
	return values[2];
}

The important point is that std::span borrows the vector's storage. That makes the API lightweight, but the vector must outlive the span and must not reallocate while borrowed views are still in use.

View selection guide

Example in practice

#include <span>
#include <vector>

void normalize(std::span<int> values) {
    for (int& value : values) {
        value *= 2;
    }
}

int main() {
    std::vector<int> values{1, 2, 3};
    normalize(values);
    return values[1];
}