std::data_C++中文网
| 定义于头文件 |
||
| template <class C> |
(1) | (C++17 起) |
| template <class C> |
(2) | (C++17 起) |
| template <class T, std::size_t N> |
(3) | (C++17 起) |
| template <class E> |
(4) | (C++17 起) |
返回指向含有容器元素的内存块的指针。
1,2) 返回 c.data()
3) 返回 array
4) 返回 il.begin()
参数
| c | - | 有 data() 方法的容器 |
| array | - | 任意类型的数组 |
| il | - | initializer_list |
返回值
指向含有容器元素的内存块的指针。
注意
除了包含于 <iterator> ,若包含下列任一头文件,则保证 std::data 可用: <array> 、 <deque> 、 <forward_list> 、 <list> 、 <map> 、 <regex> 、 <set> 、 <span> (C++20 起) 、 <string> 、 <string_view> 、 <unordered_map> 、 <unordered_set> 及 <vector> 。
可能的实现
| 版本一 |
|---|
template <class C> constexpr auto data(C& c) -> decltype(c.data()) { return c.data(); } |
| 版本二 |
template <class C> constexpr auto data(const C& c) -> decltype(c.data()) { return c.data(); } |
| 版本三 |
template <class T, std::size_t N> constexpr T* data(T (&array)[N]) noexcept { return array; } |
| 版本四 |
template <class E> constexpr const E* data(std::initializer_list<E> il) noexcept { return il.begin(); } |