std::deque:...que::cbegin(3)

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

πŸŽ€ std::deque::begin,std::deque::cbegin πŸŽ€

okay so bestie, you opened the std::deque:...que::cbegin(3) page from the C++ Standard Libary manual, and we're about to make std::deque::begin,std::deque::cbegin make sense fr fr. slay ✨


πŸ’… NAME πŸ’…

the name is std::deque::begin,std::deque::cbegin β€” and yes the page repeats it as std::deque::begin,std::deque::cbegin πŸ’–


✨ Synopsis ✨

okay so the synopsis is literally this, don't skip the annotations:

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)

Returns an iterator to the first element of the deque.

If the deque is empty, the returned iterator will be equal to end().

there's also a lil diagram ref named range-begin-end.svg in the original, cute.


🫢 Parameters 🫢

(none) β€” literally not a single parameter, bestie.


πŸ’– Return value πŸ’–

Iterator to the first element. that's it, that's the return value.


🌈 Complexity 🌈

Constant. periodt.


πŸ¦‹ Notes πŸ¦‹

libc++ backports cbegin() to C++98 mode. that's the whole note, iconic.


πŸ“ Example πŸ“

the original page has // Run this code right before the snippet, so here's the code exactly:

        #include <algorithm>
        #include <iostream>
        #include <numeric>
        #include <string>
        #include <deque>

        int main()
        {
            std::deque<int> nums{1, 2, 4, 8, 16};
            std::deque<std::string> fruits{"orange", "apple", "raspberry"};
            std::deque<char> empty;

            // Print deque.
            std::for_each(nums.begin(), nums.end(), [](const int n) { std::cout
       << n << ' '; });
            std::cout << '\n';

            // Sums all integers in the deque 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 deque fruits, checking if there is
       any.
            if (!fruits.empty())
                std::cout << "First fruit: " << *fruits.begin() << '\n';

            if (empty.begin() == empty.end())
                std::cout << "deque 'empty' is indeed empty.\n";
        }

Output:

1 2 4 8 16
Sum of nums: 31
First fruit: orange
deque 'empty' is indeed empty.

πŸ‘‘ 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 (C++11) (function template) (C++14)

πŸ’œ mwah πŸ’œ this page came from http://cppreference.com, dated 2024.06.10, and its page id is std::deque:...que::cbegin(3) β€” bye bestie, go slay your iterators ✨

πŸ“¦ where this page comes from