std::array data() method
- od C++17
- do C++17
// Non-const version
constexpr T* data() noexcept;
// Const version
constexpr const T* data() const noexcept;
// Non-const version
T* data() noexcept;
// Const version
const T* data() const noexcept;
Returns pointer to the underlying array serving as element storage.
The pointer is such that range [ data()
; data() + size()
) is always a valid range,
even if the container is empty (data()
is not dereferenceable in that case).
Parameters
(none)
Return value
Pointer to the underlying element storage.
For a non-empty container c
, the returned pointer compares equal to the address of the first element - c.data() == &c[0]
;
Complexity
Constant.
Notes
If size()
is 0
, data()
may or may not return a null pointer.
Example
#include <array>
#include <iostream>
int main()
{
std::array<char, 6> letters {'a', 'b', 'c', 'd', 'e', 'f'};
if (!letters.empty()) {
std::cout << "The last character is '" << letters.back() << "'.\n";
}
}
The last character is 'f'.
Hover to see the original license.