std::list<T,Allocator>::begin, std::list<T,Allocator>::cbegin
提供: cppreference.com
iterator begin(); | (C++11未満) | |
iterator begin()noexcept; | (C++11以上) | |
const_iterator begin()const; | (C++11未満) | |
const_iterator begin()constnoexcept; | (C++11以上) | |
const_iterator cbegin()constnoexcept; | (C++11以上) | |
コンテナの最初の要素を指すイテレータを返します。
コンテナが空の場合は、返されたイテレータは end() と等しくなります。
目次 |
[編集]引数
(なし)
[編集]戻り値
最初の要素を指すイテレータ。
[編集]計算量
一定。
[編集]例
Run this code
#include <iostream>#include <list>#include <string> int main(){std::list<int> ints {1, 2, 4, 8, 16};std::list<std::string> fruits {"orange", "apple", "raspberry"};std::list<char> empty; // Sums all integers in the list ints (if any), printing only the result.int sum =0;for(auto it = ints.cbegin(); it != ints.cend(); it++) sum +=*it;std::cout<<"Sum of ints: "<< sum <<"\n"; // Prints the first fruit in the list fruits, without checking if there is one.std::cout<<"First fruit: "<<*fruits.begin()<<"\n"; if(empty.begin()== empty.end())std::cout<<"list 'empty' is indeed empty.\n";}
出力:
Sum of ints: 31 First fruit: orange list 'empty' is indeed empty.
[編集]関連項目
終端を指すイテレータを返します (パブリックメンバ関数) |