Difference between revisions of "cpp/iterator/data"
From cppreference.com
(Updated the header list.) |
|||
(One intermediate revision by one user not shown) | |||
Line 2: | Line 2: | ||
{{cpp/iterator/navbar}} | {{cpp/iterator/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{ | + | {{cpp/iterator/range access headers}} |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
{{dcla|num=1|since=c++17| | {{dcla|num=1|since=c++17| | ||
template< class C > | template< class C > | ||
Line 38: | Line 23: | ||
Returns a pointer to the block of memory containing the elements of the range. | Returns a pointer to the block of memory containing the elements of the range. | ||
− | @1,2@ | + | @1,2@ Returns {{c|c.data()}}. |
− | @3@ | + | @3@ Returns {{c|array}}. |
− | @4@ | + | @4@ Returns {{c|il.begin()}}. |
===Parameters=== | ===Parameters=== | ||
Line 46: | Line 31: | ||
{{par|c|a container or view with a {{c|data()}} member function}} | {{par|c|a container or view with a {{c|data()}} member function}} | ||
{{par|array|an array of arbitrary type}} | {{par|array|an array of arbitrary type}} | ||
− | {{par|il|an | + | {{par|il|an {{lc|std::initializer_list}}}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | + | @1,2@ {{c|c.data()}} | |
+ | @3@ {{c|array}} | ||
+ | @4@ {{c|il.begin()}} | ||
===Exceptions=== | ===Exceptions=== | ||
Line 63: | Line 50: | ||
{{eq impl | {{eq impl | ||
|ver1=1|1= | |ver1=1|1= | ||
− | template <class C> | + | template<class C> |
constexpr auto data(C& c) -> decltype(c.data()) | constexpr auto data(C& c) -> decltype(c.data()) | ||
{ | { | ||
Line 69: | Line 56: | ||
} | } | ||
|ver2=2|2= | |ver2=2|2= | ||
− | template <class C> | + | template<class C> |
constexpr auto data(const C& c) -> decltype(c.data()) | constexpr auto data(const C& c) -> decltype(c.data()) | ||
{ | { | ||
Line 75: | Line 62: | ||
} | } | ||
|ver3=3|3= | |ver3=3|3= | ||
− | template <class T, std::size_t N> | + | template<class T, std::size_t N> |
constexpr T* data(T (&array)[N]) noexcept | constexpr T* data(T (&array)[N]) noexcept | ||
{ | { | ||
Line 81: | Line 68: | ||
} | } | ||
|ver4=4|4= | |ver4=4|4= | ||
− | template <class E> | + | template<class E> |
constexpr const E* data(std::initializer_list<E> il) noexcept | constexpr const E* data(std::initializer_list<E> il) noexcept | ||
{ | { | ||
Line 101: | Line 88: | ||
char a[20]; // storage for a C-style string | char a[20]; // storage for a C-style string | ||
std::strcpy(a, std::data(s)); | std::strcpy(a, std::data(s)); | ||
− | + | // [s.data(), s.data() + s.size()] is guaranteed to be an NTBS since C++11 | |
std::cout << a; | std::cout << a; |
Latest revision as of 18:28, 20 February 2024
Defined in header <array>
|
||
Defined in header <deque>
|
||
Defined in header <flat_map>
|
||
Defined in header <flat_set>
|
||
Defined in header <forward_list>
|
||
Defined in header <inplace_vector>
|
||
Defined in header <iterator>
|
||
Defined in header <list>
|
||
Defined in header <map>
|
||
Defined in header <regex>
|
||
Defined in header <set>
|
||
Defined in header <span>
|
||
Defined in header <string>
|
||
Defined in header <string_view>
|
||
Defined in header <unordered_map>
|
||
Defined in header <unordered_set>
|
||
Defined in header <vector>
|
||
template< class C > constexpr auto data( C& c ) -> decltype(c.data()); |
(1) | (since C++17) |
template< class C > constexpr auto data( const C& c ) -> decltype(c.data()); |
(2) | (since C++17) |
template< class T, std::size_t N > constexpr T* data( T (&array)[N] ) noexcept; |
(3) | (since C++17) |
template< class E > constexpr const E* data( std::initializer_list<E> il ) noexcept; |
(4) | (since C++17) |
Returns a pointer to the block of memory containing the elements of the range.
1,2) Returns c.data().
3) Returns array.
4) Returns il.begin().
Contents |
[edit] Parameters
c | - | a container or view with a data() member function |
array | - | an array of arbitrary type |
il | - | an std::initializer_list |
[edit] Return value
1,2) c.data()
3) array
4) il.begin()
[edit] Exceptions
1) May throw implementation-defined exceptions.
[edit] Notes
The overload for std::initializer_list is necessary because it does not have a member function data
.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_nonmember_container_access |
201411L | (C++17) | std::size(), std::data() , and std::empty()
|
[edit] Possible implementation
First version |
---|
template<class C> constexpr auto data(C& c) -> decltype(c.data()) { return c.data(); } |
Second version |
template<class C> constexpr auto data(const C& c) -> decltype(c.data()) { return c.data(); } |
Third version |
template<class T, std::size_t N> constexpr T* data(T (&array)[N]) noexcept { return array; } |
Fourth version |
template<class E> constexpr const E* data(std::initializer_list<E> il) noexcept { return il.begin(); } |
[edit] Example
Run this code
#include <cstring> #include <iostream> #include <string> int main() { std::string s{"Hello world!\n"}; char a[20]; // storage for a C-style string std::strcpy(a, std::data(s)); // [s.data(), s.data() + s.size()] is guaranteed to be an NTBS since C++11 std::cout << a; }
Output:
Hello world!
[edit] See also
(C++20) |
obtains a pointer to the beginning of a contiguous range (customization point object) |
(C++20) |
obtains a pointer to the beginning of a read-only contiguous range (customization point object) |