Difference between revisions of "cpp/iterator/data"
From cppreference.com
(→Synopsis: emphasizes the revision's target header. (?)) |
|||
Line 1: | Line 1: | ||
− | {{cpp/title|data}} | + | {{cpp/title| data}} |
{{cpp/iterator/navbar}} | {{cpp/iterator/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header| | + | {{dcl header | iterator}} |
− | {{dcl header| | + | {{dcl header | array}} |
− | {{dcl header| | + | {{dcl header | deque}} |
− | {{dcl header| | + | {{dcl header | forward_list}} |
− | {{dcl header|list}} | + | {{dcl header | list}} |
− | {{dcl header|map}} | + | {{dcl header | map}} |
− | {{dcl header|regex}} | + | {{dcl header | regex}} |
− | {{dcl header|set | + | {{dcl header | set}} |
− | + | {{dcl header | string}} | |
− | + | {{dcl header | unordered_map}} | |
− | + | {{dcl header | unordered_set}} | |
− | {{dcl header|string | + | {{dcl header | vector}} |
− | + | {{dcl | num=1 | since=c++17 | | |
− | {{dcl header|unordered_map}} | + | template <class C> |
− | {{dcl header|unordered_set}} | + | constexpr auto data(C& c) -> decltype(c.data()); |
− | {{dcl header|vector}} | + | |
− | {{ | + | |
− | template< class C > | + | |
− | constexpr auto data( C& c ) -> decltype(c.data()); | + | |
}} | }} | ||
− | {{ | + | {{dcl | num=2 | since=c++17 | |
− | template< class C > | + | template <class C> |
− | constexpr auto data( const C& c ) -> decltype(c.data()); | + | constexpr auto data(const C& c) -> decltype(c.data()); |
}} | }} | ||
− | {{ | + | {{dcl | num=3 | since=c++17 | |
− | 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; |
}} | }} | ||
− | {{ | + | {{dcl | num=4 | since=c++17 | |
− | 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; |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
− | Returns a pointer to the block of memory containing the elements of the | + | Returns a pointer to the block of memory containing the elements of the container. |
− | @1,2@ returns {{c|c. | + | @1,2@ returns {{c|c.empty()}} |
@3@ returns {{c|array}} | @3@ returns {{c|array}} | ||
@4@ returns {{c|il.begin()}} | @4@ returns {{c|il.begin()}} | ||
Line 44: | Line 40: | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par|c|a container | + | {{par | c | a container with a {{c|data()}} method}} |
− | {{par|array|an array of arbitrary type}} | + | {{par | array | an array of arbitrary type}} |
− | {{par|il|an initializer list}} | + | {{par | il | an initializer list}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | A pointer to the block of memory containing the elements of the | + | A pointer to the block of memory containing the elements of the container. |
===Exceptions=== | ===Exceptions=== | ||
− | @ | + | @3,4@ {{noexcept}} |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
===Possible implementation=== | ===Possible implementation=== | ||
− | {{eq | + | {{eq fun |
− | + | | 1= | |
− | template <class C> | + | template <class C> |
− | constexpr auto data(C& c) -> decltype(c.data()) | + | constexpr auto data(C& c) -> decltype(c.data()); |
{ | { | ||
return c.data(); | return c.data(); | ||
} | } | ||
− | + | | 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()); |
{ | { | ||
return c.data(); | return c.data(); | ||
} | } | ||
− | + | | 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; |
{ | { | ||
return array; | return array; | ||
} | } | ||
− | + | |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; |
{ | { | ||
return il.begin(); | return il.begin(); | ||
Line 89: | Line 80: | ||
===Example=== | ===Example=== | ||
− | {{example | + | {{example}} |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Revision as of 14:48, 12 September 2023
Defined in header <iterator>
|
||
Defined in header <array>
|
||
Defined in header <deque>
|
||
Defined in header <forward_list>
|
||
Defined in header <list>
|
||
Defined in header <map>
|
||
Defined in header <regex>
|
||
Defined in header <set>
|
||
Defined in header <string>
|
||
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 container.
1,2) returns c.empty()
3) returns array
4) returns il.begin()
Contents |
Parameters
c | - | a container with a data() method |
array | - | an array of arbitrary type |
il | - | an initializer list |
Return value
A pointer to the block of memory containing the elements of the container.
Exceptions
3,4)
noexcept specification:
noexcept
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(); } |
Example
This section is incomplete Reason: no example |