std::list::...ist::cbegin(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

✨ 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++ backports cbegin() 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 code chilling above the #include block β€” 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 end
  • cend β€” (public member function) (C++11)
  • begin
  • cbegin β€” returns an iterator to the beginning of a container or array, (function template) (C++11) (C++14)

πŸ’– source: http://cppreference.com β€” version 2024.06.10 β€” and that's the whole page bestie, go forth and iterate from the beginning, ily, mwah πŸ«ΆπŸŽ€βœ¨
πŸ“¦ where this page comes from