std::list::...ist::cbegin(3)
β¨ std::list::begin, std::list::cbegin β¨
okay bestie so this is the page for std::list::begin and std::list::cbegin from the C++ Standard Libary (their spelling, not mine π
), and it's literally the whole glow-up manual for getting an iterator pointed at the front of your list. it's giving "first day of school energy" fr fr π
πΈ Synopsis
iterator begin(); (1) (noexcept since C++11)
const_iterator begin() const; (2) (noexcept since C++11)
const_iterator cbegin() const noexcept; (3) (since C++11)
so what does it do? it returns an iterator to the first element of the list. that's it. that's the whole vibe. π
and the tea β β if the list is empty, the iterator you get back will be equal to end(). so like, no element, no problem, but also no element. π
range-begin-end.svg β yes that's a file name that's just sitting there in the original, iconic honestly, I don't make the rules π¦
β¨ Parameters
none β she takes nothing, she needs nothing, she's that girl π
π«Ά Return value
an iterator to the first element. periodt.
β Complexity
Constant. O(1) baby, instant, zero waiting, ate and left no crumbs π₯
π Notes
libc++backportscbegin()to C++98 mode. yep, they really did that, little time-traveler behavior, so proud ππ
π 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{"orange", "apple", "raspberry"};
std::list<char> empty;
// Print list.
std::for_each(nums.begin(), nums.end(), [](const int n) { std::cout << n << ' '; });
std::cout << '\n';
// Sums all integers in the 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 list fruits, checking if there is any.
if (!fruits.empty())
std::cout << "First fruit: " << *fruits.begin() << '\n';
if (empty.begin() == empty.end())
std::cout << "list 'empty' is indeed empty.\n";
}
ngl the original also had the comment// Run this codechilling above the#includeblock β main character energy, go hit run, I believe in you π§β¨
π» Output:
1 2 4 8 16
Sum of nums: 31
First fruit: orange
list 'empty' is indeed empty.
look at that. 1 2 4 8 16, sum is 31, first fruit is orange (correct answer, apple could never ππ), and the empty list really did prove it's empty. slay.
π See also
endβ returns an iterator to the endcendβ (public member function) (C++11)begincbeginβ returns an iterator to the beginning of a container or array, (function template) (C++11) (C++14)
π source:http://cppreference.comβ version2024.06.10β and that's the whole page bestie, go forth and iterate from the beginning, ily, mwah π«Άπβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms