std::forwar...ist::cbegin(3)
π NAME π
Okay bestie, welcome to the std::forwar...ist::cbegin(3) page from the C++ Standard Libary manual β yes, that's the truncated title and the manual name folded right in. π
std::forward_list::begin,std::forward_list::cbegin β std::forward_list::begin,std::forward_list::cbegin
πΈ Synopsis πΈ
iterator begin() noexcept; (1) (since C++11)
const_iterator begin() const noexcept; (2) (since C++11)
const_iterator cbegin() const noexcept; (3) (since C++11)
So there are three overloads, all noexcept, and all since C++11. They return an iterator to the first element of the forward_list.
If the forward_list is empty, the returned iterator will be equal to end().
There's also a diagram named range-begin-end.svg chilling on the page. π¦
π Parameters π
(none) β literally no parameters, she takes nothing. Periodt.
π¦ Return value π¦
Iterator to the first element. That's the whole vibe. β¨
π Complexity π
Constant. No cap, it's fast.
π Example π
The example starts with // Run this code, and here's the code exactly as the page has it:
// Run this code
#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.
The output ate and left no crumbs. π₯
π See also π
Here's the See also stack, bestie β all the refs and parentheticals are here:
endβ returns an iterator to the endcendβ (public member function)begincbeginβ returns an iterator to the beginning of a container or array(C++11)(function template)(C++14)
Source/version: http://cppreference.com, 2024.06.10 β k bye bestie, go be a main character with your iterators π
β¨
π¦ 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