Difference between revisions of "cpp/iterator/data"
From cppreference.com
Line 1: | Line 1: | ||
− | {{cpp/title| data}} | + | {{cpp/title|data}} |
{{cpp/iterator/navbar}} | {{cpp/iterator/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header | | + | {{dcl header|array}} |
− | {{dcl header | | + | {{dcl header|deque}} |
− | {{dcl header | | + | {{dcl header|forward_list}} |
− | {{dcl header | | + | {{dcl header|iterator}} |
− | {{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 sep}} |
− | {{dcl header | unordered_map}} | + | {{dcl header|span|notes={{mark since c++20}}}} |
− | {{dcl header | unordered_set}} | + | {{dcl sep}} |
− | {{dcl header | vector}} | + | {{dcl header|string}} |
− | {{ | + | {{dcl header|string_view}} |
− | template <class C> | + | {{dcl header|unordered_map}} |
− | constexpr auto data(C& c) -> decltype(c.data()); | + | {{dcl header|unordered_set}} |
+ | {{dcl header|vector}} | ||
+ | {{dcla|num=1|since=c++17| | ||
+ | template< class C > | ||
+ | constexpr auto data( C& c ) -> decltype(c.data()); | ||
}} | }} | ||
− | {{ | + | {{dcla|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()); |
}} | }} | ||
− | {{ | + | {{dcla|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; |
}} | }} | ||
− | {{ | + | {{dcla|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 range. |
− | @1,2@ returns {{c|c. | + | @1,2@ returns {{c|c.data()}} |
@3@ returns {{c|array}} | @3@ returns {{c|array}} | ||
@4@ returns {{c|il.begin()}} | @4@ returns {{c|il.begin()}} | ||
Line 40: | Line 44: | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | c | a container with a {{c|data()}} | + | {{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 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 range. |
===Exceptions=== | ===Exceptions=== | ||
− | @ | + | @1@ {{cpp/impldef exception item}} |
+ | |||
+ | ===Notes=== | ||
+ | The overload for {{lc|std::initializer_list}} is necessary because it does not have a member function {{tt|data}}. | ||
+ | |||
+ | {{feature test macro|__cpp_lib_nonmember_container_access|{{lc|std::size()}}, {{tt|std::data()}}, and {{lc|std::empty()}}|value=201411L|std=C++17}} | ||
===Possible implementation=== | ===Possible implementation=== | ||
− | {{eq | + | {{eq impl |
− | + | |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()) |
{ | { | ||
return c.data(); | return c.data(); | ||
} | } | ||
− | + | |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()) |
{ | { | ||
return c.data(); | return c.data(); | ||
} | } | ||
− | + | |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 |
{ | { | ||
return array; | return array; | ||
} | } | ||
− | + | |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 |
{ | { | ||
return il.begin(); | return il.begin(); | ||
Line 80: | Line 89: | ||
===Example=== | ===Example=== | ||
− | {{example}} | + | {{example |
+ | |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! | ||
+ | }} | ||
+ | |||
+ | ===See also=== | ||
+ | {{dsc begin}} | ||
+ | {{dsc inc|cpp/ranges/dsc data}} | ||
+ | {{dsc inc|cpp/ranges/dsc cdata}} | ||
+ | {{dsc end}} | ||
+ | |||
+ | {{langlinks|es|ja|ru|zh}} |
Revision as of 15:25, 12 September 2023
Defined in header <array>
|
||
Defined in header <deque>
|
||
Defined in header <forward_list>
|
||
Defined in header <iterator>
|
||
Defined in header <list>
|
||
Defined in header <map>
|
||
Defined in header <regex>
|
||
Defined in header <set>
|
||
Defined in header <span>
|
(since C++20) |
|
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 |
Parameters
c | - | a container or view with a data() member function |
array | - | an array of arbitrary type |
il | - | an initializer list |
Return value
A pointer to the block of memory containing the elements of the range.
Exceptions
1) May throw implementation-defined exceptions.
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()
|
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
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!
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) |