Przejdź do głównej zawartości

std::array end()/cend() method

// Non-const version
constexpr iterator end() noexcept;

// Const version
constexpr const_iterator end() const noexcept;
constexpr const_iterator cend() const noexcept;

Returns an iterator

to the element past the end of the array. If the array is empty, the returned iterator will be equal to begin().

Attempting to dereference a past-the-end iterator results in an undefined behaviour.

Parameters

(none)

Return value

Iterator to the past-the-end element.

Complexity

Constant.

Notes

For a container c, the expression *std::prev(c.end()) is equivalent to c.back().

Why past the end?

important

This section requires improvement. You can help by editing this doc page.

Difference between end and cend

For a const container c, end and cend are the same - c.end() == c.cend()

For non-const container of type c they return different iterators:

#include <array>

int main()
{
std::array<int, 5> arr = {1, 2, 3, 4, 5};
auto it = arr.end(); // Type: std::array<int, 5>::iterator
*std::prev(it) = 5; // ✔ Ok
}

Example

Main.cpp
#include <array>
#include <iostream>
#include <algorithm>
#include <iomanip>

int main()
{
std::cout << std::boolalpha;

std::array<int, 0> empty;
std::cout << "1) "
<< (empty.begin() == empty.end()) << ' ' // true
<< (empty.cbegin() == empty.cend()) << '\n'; // true
// *(empty.begin()) = 42; // => undefined behaviour at run-time


std::array<int, 4> numbers{5, 2, 3, 4};
std::cout << "2) "
<< (numbers.begin() == numbers.end()) << ' ' // false
<< (numbers.cbegin() == numbers.cend()) << '\n' // false
<< "3) "
<< *(numbers.begin()) << ' ' // 5
<< *(numbers.cbegin()) << '\n'; // 5

*numbers.begin() = 1;
std::cout << "4) " << *(numbers.begin()) << '\n'; // 1
// *(numbers.cbegin()) = 42; // compile-time error:
// read-only variable is not assignable

// print out all elements
std::cout << "5) ";
std::for_each(numbers.cbegin(), numbers.cend(), [](int x) {
std::cout << x << ' ';
});
std::cout << '\n';


constexpr std::array constants{'A', 'B', 'C'};
static_assert(constants.begin() != constants.end()); // OK
static_assert(constants.cbegin() != constants.cend()); // OK
static_assert(*constants.begin() == 'A'); // OK
static_assert(*constants.cbegin() == 'A'); // OK
// ❌ Compile-time error: read-only variable is not assignable
*constants.begin() = 'Z';
}
Possible output
1) true true
2) false false
3) 5 5
4) 1
5) 1 2 3 4
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::array end()/cend() method

// Non-const version
constexpr iterator end() noexcept;

// Const version
constexpr const_iterator end() const noexcept;
constexpr const_iterator cend() const noexcept;

Returns an iterator

to the element past the end of the array. If the array is empty, the returned iterator will be equal to begin().

Attempting to dereference a past-the-end iterator results in an undefined behaviour.

Parameters

(none)

Return value

Iterator to the past-the-end element.

Complexity

Constant.

Notes

For a container c, the expression *std::prev(c.end()) is equivalent to c.back().

Why past the end?

important

This section requires improvement. You can help by editing this doc page.

Difference between end and cend

For a const container c, end and cend are the same - c.end() == c.cend()

For non-const container of type c they return different iterators:

#include <array>

int main()
{
std::array<int, 5> arr = {1, 2, 3, 4, 5};
auto it = arr.end(); // Type: std::array<int, 5>::iterator
*std::prev(it) = 5; // ✔ Ok
}

Example

Main.cpp
#include <array>
#include <iostream>
#include <algorithm>
#include <iomanip>

int main()
{
std::cout << std::boolalpha;

std::array<int, 0> empty;
std::cout << "1) "
<< (empty.begin() == empty.end()) << ' ' // true
<< (empty.cbegin() == empty.cend()) << '\n'; // true
// *(empty.begin()) = 42; // => undefined behaviour at run-time


std::array<int, 4> numbers{5, 2, 3, 4};
std::cout << "2) "
<< (numbers.begin() == numbers.end()) << ' ' // false
<< (numbers.cbegin() == numbers.cend()) << '\n' // false
<< "3) "
<< *(numbers.begin()) << ' ' // 5
<< *(numbers.cbegin()) << '\n'; // 5

*numbers.begin() = 1;
std::cout << "4) " << *(numbers.begin()) << '\n'; // 1
// *(numbers.cbegin()) = 42; // compile-time error:
// read-only variable is not assignable

// print out all elements
std::cout << "5) ";
std::for_each(numbers.cbegin(), numbers.cend(), [](int x) {
std::cout << x << ' ';
});
std::cout << '\n';


constexpr std::array constants{'A', 'B', 'C'};
static_assert(constants.begin() != constants.end()); // OK
static_assert(constants.cbegin() != constants.cend()); // OK
static_assert(*constants.begin() == 'A'); // OK
static_assert(*constants.cbegin() == 'A'); // OK
// ❌ Compile-time error: read-only variable is not assignable
*constants.begin() = 'Z';
}
Possible output
1) true true
2) false false
3) 5 5
4) 1
5) 1 2 3 4
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.