
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Library - <mdspan>
The <mdspan> header in C++ is used for working with multidimensional views of data. It provides an efficient way to access multidimensional arrays or contiguous blocks of memory with flexible mapping layouts.
This header is part of the containers library and also optimized for performing critical applications, can be used with different data layouts such as (row-major, column-major order).
Including <mdspan> Header
To include the <mdspan> header in your C++ program, you can use the following syntax.
#include <mdspan>
Functions of <mdspan> Header
Below is list of all functions from <mdspan> header.
S.No | Functions & Description |
---|---|
1 |
operator []
This function accesses an element at the specified multidimensional index. |
2 |
size
This function returns the size of the multidimensional index space. |
3 |
empty
This function checks if the size of the index space is zero. |
4 |
stride
This function obtains the stride along the specified dimension. |
5 |
extents
This function obtains the extents object. |
6 |
data_handle
This function obtains the pointer to the underlying 1D sequence. |
7 |
mapping
This function obtains the mapping object that defines how multidimensional indices map to the 1D data. |
8 |
accessor
This function returns the accessor policy object, which manages access to the underlying element. |
Accessing Elements
In the following example we are going to use, operator[] it allows matrix[1][2] accesses the element at the first row and second column.
#include <mdspan> #include <iostream> int main() { std::array<int, 9> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; std::mdspan<int, std::extents<3, 3>> matrix(data.data()); std::cout << "Element at [1][2]: " << matrix[1][2] << std::endl; return 0; }
Output
If we run the above code it will generate the following output
6
Property Checks
The property checks are member functions that provide information about how the multidimensional mapping of the object behaves.
S.No | Functions & Description |
---|---|
1 |
is_unique
This function determines if this mdspan's mapping is unique. |
2 |
is_exhaustive
This function determines if this mdspan's mapping is exhaustive. |
3 |
is_strided
This function determines if this mdspan's mapping is strided( in each dimension, incrementing an index jumps over the same number of underlying elements every time). |
Property Checking
In the following example we are going to use, is_unique() function to check if each multidimensional index in the mdspan maps to a distinct element in the 1D data array.
#include <mdspan> #include <iostream> int main() { std::array<int, 9> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; std::mdspan<int, std::extents<3, 3>> matrix(data.data()); if (matrix.is_unique()) { std::cout << "Mapping is unique." << std::endl; } else { std::cout << "Mapping is not unique." << std::endl; } return 0; }
Output
If we run the above code it will generate the following output
Mapping is unique.
Static Functions
The static functions provides compile-time properties of mdspan and its mapping layout.
S.No | Functions & Description |
---|---|
1 |
is_always_unique
This static function determines if this mdspan's layout mapping is always unique. |
2 |
is_always_exhaustive
This static function determines if this mdspan's layout mapping is always exhaustive. |
3 |
is_always_strided
This static function determines if this mdspan's layout mapping is always strided. |
Checking Compile-Time Property
In the following example we are going to use, is_always_exhaustive to check if the layout mapping always covers the entire extents of the mdspan in a contiguous manner at compile-time.
#include <mdspan> #include <iostream> int main() { if (std::mdspan<int, std::extents<3, 3>>::is_always_exhaustive()) { std::cout << "Mapping is always exhaustive." << std::endl; } else { std::cout << "Mapping is not always exhaustive." << std::endl; } return 0; }
Output
If we run the above code it will generate the following output
Mapping is always exhaustive.