Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/data"

From cppreference.com
< cpp‎ | iterator
m (s/method/member function)
(+ ranges)
Line 34: Line 34:
 
{{dcl end}}
 
{{dcl end}}
  
Returns a pointer to the block of memory containing the elements of the container.
+
Returns a pointer to the block of memory containing the elements of the range.
  
 
@1,2@ returns {{c|c.data()}}
 
@1,2@ returns {{c|c.data()}}
Line 42: Line 42:
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | c | a container 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 initializer list}}
 
{{par | il | an initializer list}}
Line 48: Line 48:
  
 
===Return value===
 
===Return value===
A pointer to the block of memory containing the elements of the container.
+
A pointer to the block of memory containing the elements of the range.
 +
 
 +
===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}}.
  
 
===Possible implementation===
 
===Possible implementation===
Line 99: Line 105:
 
}}
 
}}
  
{{langlinks|es|ja|zh}}
+
===See also===
 +
{{dsc begin}}
 +
{{dsc inc | cpp/ranges/dsc data}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|es|ja|ru|zh}}

Revision as of 23:50, 3 April 2021

 
 
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(C++20)
(C++20)
(C++20)
Utilities
(C++20)
Iterator adaptors
Range access
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
data
(C++17)
 
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.

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

#include <string>
#include <cstring>
#include <iostream>
 
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

obtains a pointer to the beginning of a contiguous range
(customization point object)[edit]