Namespaces
Variants

std::list<T,Allocator>:: end, std::list<T,Allocator>:: cend

From cppreference.net

iterator end ( ) ;
(1) (C++11以降 noexcept)
(C++26以降 constexpr)
const_iterator end ( ) const ;
(2) (C++11以降 noexcept)
(C++26以降 constexpr)
const_iterator cend ( ) const noexcept ;
(3) (C++11以降)
(C++26以降 constexpr)

* this の最後の要素の次を指すイテレータを返します。

この返されたイテレータはセンチネルとしてのみ機能します。これが デリファレンス可能 であることは保証されません。

range-begin-end.svg

目次

戻り値

最後の要素の次のイテレータ。

計算量

定数。

注記

libc++ バックポートは cend() を C++98 モードに移植します。

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <list>
int main()
{
    std::list<int> nums{1, 2, 4, 8, 16};
    std::list<std::string> fruits{"orange", "apple", "raspberry"};
    std::list<char> empty;
    // リストを表示
    std::for_each(nums.begin(), nums.end(), [](const int n) { std::cout << n << ' '; });
    std::cout << '\n';
    // リストnums内の全整数を合計し(存在する場合)、結果のみを表示
    std::cout << "Sum of nums: "
              << std::accumulate(nums.begin(), nums.end(), 0) << '\n';
    // リストfruitsの最初のフルーツを表示(存在確認付き)
    if (!fruits.empty())
        std::cout << "First fruit: " << *fruits.begin() << '\n';
    if (empty.begin() == empty.end())
        std::cout << "list 'empty' is indeed empty.\n";
}

出力:

1 2 4 8 16
Sum of nums: 31
First fruit: orange
list 'empty' is indeed empty.

関連項目

先頭を指すイテレータを返す
(公開メンバ関数)
(C++11) (C++14)
コンテナまたは配列の終端を指すイテレータを返す
(関数テンプレート)