std::list::begin, std::list::cbegin
Материал из cppreference.com
iterator begin(); |
(до C++11) | |
iterator begin() noexcept; |
(начиная с C++11) | |
const_iterator begin() const; |
(до C++11) | |
const_iterator begin() const noexcept; |
(начиная с C++11) | |
const_iterator cbegin() const noexcept; |
(начиная с C++11) | |
Возвращает итератор на первый элемент list.
Если list - пуст, возвращаемый итератор будет равен end()
Параметры
(нет)
Возвращаемое значение
Итератор на первый элемент.
Сложность
Константная.
Example
Запустить этот код
#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 {"апельсин", "яблоко", "малина"};
std::list<char> empty;
// Напечатаем list.
std::for_each(nums.begin(), nums.end(), [](const int n) { std::cout << n << ' '; });
std::cout << '\n';
// Сумма всех чисел в list nums (если такие есть), печатаем результат.
std::cout << "Сумма чисел: "
<< std::accumulate(nums.begin(), nums.end(), 0) << '\n';
// Печатаем первый фрукт в list fruits, с проверкой если они есть.
if (!fruits.empty())
std::cout << "Первый фрукт: " << *fruits.begin() << '\n';
if (empty.begin() == empty.end())
std::cout << "list 'empty' - действительно пуст.\n";
}
Вывод:
1 2 4 8 16
Сумма чисел: 31
Первый фрукт: апельсин
list 'empty' - действительно пуст.
See also
(C++11) |
возвращает итератор на конец (public функция-элемент) |