std::vector...ector::cend(3)

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

πŸ’… std::vector::end, std::vector::cend πŸ’…

okay bestie, welcome to the <vector> section of the C++ Standard Libary β€” that's Libary, yeah, that's literally how the header spells it, no cap πŸ’€ β€” this is section 3, page std::vector...ector::cend(3) (the header truncates it like that, iconic mess). Anyway. Sit down, grab a strawberry πŸ“, we're talking about the two little functions that tell you where your vector stops existing.


✨ SYNOPSIS ✨

ok so there are like, three overloads (1), (2), (3), and they all got dropped at different times in C++ history. read the labels carefully bestie:

iterator end();                          (until C++11)

iterator end() noexcept;                 (since C++11) (constexpr since C++20)

const_iterator end() const;              (1) (until C++11)

const_iterator end() const noexcept;     (since C++11) (constexpr since C++20) (2)

const_iterator cend() const noexcept;    (since C++11) (constexpr since C++20) (3)

like, end() got a noexcept glow-up in C++11, then a constexpr glow-up in C++20, and cend() (that's the const one, bestie, the c is literally for const) showed up in C++11. slay. πŸ’…


🌸 DESCRIPTION 🌸

end() / cend() hand you an iterator pointing at the element following the last element of the vector. 🫢

and here's the tea: that "element" isn't a real element. it's a placeholder. it's giving main character energy but it's literally just the finish line. touching it? accessing it? that's undefined behavior 😭 β€” not "kinda risky", not "usually fine", undefined, as in the standard has zero opinions about what happens and your program can do whatever it wants. do NOT dereference it. periodt.

πŸŽ€ also there's a whole diagram referenced here β€” range-begin-end.svg β€” so yeah, picture a lil arrow at the end of the range. that's the vibe.

πŸ¦‹ PARAMETERS πŸ¦‹

  • (none) β€” that's it. no arguments, no nothing. we love a low-maintenance function. ✨

πŸŽ€ RETURN VALUE πŸŽ€

An iterator to the element following the last element. that's the whole return value section. short and sweet. πŸ“


πŸ’– COMPLEXITY πŸ’–

Constant. β€” O(1), instantly, no scrolling, no searching, she just knows. πŸ”₯


🌈 NOTES 🌈

  • libc++ backports cend() to C++98 mode. so if you're out here living that C++98 life (why bestie 😭), libc++ still has you covered. 🫢

⭐ EXAMPLE ⭐

ok this example is so cute, it does like four different things and every one of them uses begin()/end() correctly, which is the whole point:

// Run this code

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

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

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

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

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

🌟 Output 🌟

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

see that last one?? empty.begin() == empty.end() β€” that's the whole trick, bestie πŸ‘‘. an empty vector's begin and end are literally the same place. vibe check passed. πŸ’€βœ¨


πŸ‘‘ SEE ALSO πŸ‘‘

  • begin β€” returns an iterator to the beginning (public member function)
  • cbegin (C++11) β€” returns an iterator to the beginning (public member function)
  • end, cend (C++11) (C++14) β€” returns an iterator to the end of a container or array (function template)

🧚 and that's the whole page bestie, go forth and never dereference end() πŸ’œ

source: http://cppreference.com Β· version dated 2024.06.10 Β· page std::vector...ector::cend(3) β€” mwah, bye πŸŽ€πŸ’–πŸ¦‹

πŸ“¦ where this page comes from