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

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

πŸ’– 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 end
  • cend β€” (public member function)
  • begin
  • cbegin β€” 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