Przejdź do głównej zawartości

std::forward_list end() method

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

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

Returns an iterator

to the element past-the-end of the array.

zanotuj

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

Undefined Behavior

Attempting to dereference a past-the-end iterator is undefined behaviour

.

Parameters

(none)

Return value

Iterator to the first element.

Complexity

Constant - **O(1)*8.

Notes

For a container c, the expression *c.begin() is equivalent to c.front().

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 <forward_list>

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

Example

Main.cpp
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <forward_list>

int main()
{
std::forward_list<int> nums {1, 2, 4, 8, 16};
std::forward_list<std::string> fruits {"orange", "apple", "raspberry"};
std::forward_list<char> empty;

// Print forward_list.
std::for_each(nums.begin(), nums.end(), [](const int n) { std::cout << n << ' '; });
std::cout << '\n';

// Sums all integers in the forward_list nums (if any), printing only the result.
std::cout << "Sum of nums: "
<< std::accumulate(nums.begin(), nums.end(), 0) << '\n';

// Prints the first fruit in the forward_list fruits, checking if there is any.
if (!fruits.empty())
std::cout << "First fruit: " << *fruits.begin() << '\n';

if (empty.begin() == empty.end())
std::cout << "forward_list 'empty' is indeed empty.\n";
}
Output
1 2 4 8 16
Sum of nums: 31
First fruit: orange
forward_list 'empty' is indeed empty.
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::forward_list end() method

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

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

Returns an iterator

to the element past-the-end of the array.

zanotuj

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

Undefined Behavior

Attempting to dereference a past-the-end iterator is undefined behaviour

.

Parameters

(none)

Return value

Iterator to the first element.

Complexity

Constant - **O(1)*8.

Notes

For a container c, the expression *c.begin() is equivalent to c.front().

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 <forward_list>

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

Example

Main.cpp
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <forward_list>

int main()
{
std::forward_list<int> nums {1, 2, 4, 8, 16};
std::forward_list<std::string> fruits {"orange", "apple", "raspberry"};
std::forward_list<char> empty;

// Print forward_list.
std::for_each(nums.begin(), nums.end(), [](const int n) { std::cout << n << ' '; });
std::cout << '\n';

// Sums all integers in the forward_list nums (if any), printing only the result.
std::cout << "Sum of nums: "
<< std::accumulate(nums.begin(), nums.end(), 0) << '\n';

// Prints the first fruit in the forward_list fruits, checking if there is any.
if (!fruits.empty())
std::cout << "First fruit: " << *fruits.begin() << '\n';

if (empty.begin() == empty.end())
std::cout << "forward_list 'empty' is indeed empty.\n";
}
Output
1 2 4 8 16
Sum of nums: 31
First fruit: orange
forward_list 'empty' is indeed empty.
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.