std::vector...ector::cend(3)
π 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++backportscend()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
- 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