std::array:...ray::cbegin(3)
π
std::array::begin, std::array::cbegin β std::array:...ray::cbegin(3) / C++ Standard Libary π
okay bestie, welcome to the manual page for std::array::begin and std::array::cbegin β the littlest iterator-getters that are absolutely doing main character energy in the C++ Standard Libary. Yes, the manual says βLibary.β Weβre keeping it. Periodt. π
β¨ NAME β¨
std::array::begin, std::array::cbegin β literally the page name, bestie: std::array::begin,std::array::cbegin. The dash repeats it too: std::array::begin,std::array::cbegin. Itβs giving βI said what I said.β π
πΈ Synopsis πΈ
iterator begin() noexcept; (1) (since C++11)
(constexpr since C++17)
const_iterator begin() const noexcept; (2) (since C++11)
(constexpr since C++17)
const_iterator cbegin() const noexcept; (3) (since C++11)
(constexpr since C++17)
So there are three overloads, numbered (1), (2), and (3), all since C++11, all noexcept, and all constexpr since C++17. Slay. β¨
Returns an iterator to the first element of the array.
If the array is empty, the returned iterator will be equal to end().
Thereβs also a little visual in the original: range-begin-end.svg. Itβs giving βrange-begin-end diagramβ and we respect it. π¦
π Parameters π
(none) β literally no parameters, not a single one, no cap. π
π Return value π
Iterator to the first element. Thatβs the whole vibe. π«Ά
β Complexity β
Constant. Not linear, not quadratic, just constant. Iconic. π
π₯ Example π₯
// Run this code
#include <algorithm>
#include <array>
#include <iomanip>
#include <iostream>
int main()
{
std::cout << std::boolalpha;
std::array<int, 0> empty;
std::cout << "1) "
<< (empty.begin() == empty.end()) << ' ' // true
<< (empty.cbegin() == empty.cend()) << '\n'; // true
// *(empty.begin()) = 42; // => undefined behavior at run-time
std::array<int, 4> numbers{5, 2, 3, 4};
std::cout << "2) "
<< (numbers.begin() == numbers.end()) << ' ' // false
<< (numbers.cbegin() == numbers.cend()) << '\n' // false
<< "3) "
<< *(numbers.begin()) << ' ' // 5
<< *(numbers.cbegin()) << '\n'; // 5
*numbers.begin() = 1;
std::cout << "4) " << *(numbers.begin()) << '\n'; // 1
// *(numbers.cbegin()) = 42; // compile-time error:
// read-only variable is not
assignable
// print out all elements
std::cout << "5) ";
std::for_each(numbers.cbegin(), numbers.cend(), [](int x)
{
std::cout << x << ' ';
});
std::cout << '\n';
constexpr std::array constants{'A', 'B', 'C'};
static_assert(constants.begin() != constants.end()); // OK
static_assert(constants.cbegin() != constants.cend()); // OK
static_assert(*constants.begin() == 'A'); // OK
static_assert(*constants.cbegin() == 'A'); // OK
// *constants.begin() = 'Z'; // compile-time error:
// read-only variable is not
assignable
}
bestie, those comments are part of the code and they are staying. Especially the βundefined behavior at run-timeβ and βcompile-time errorβ ones. We do not edit the sacred comments. π
π Output π
1) true true
2) false false
3) 5 5
4) 1
5) 1 2 3 4
Ate. No crumbs. π
π See also π
endβ returns an iterator to the endcendβ (public member function)begincbeginβ returns an iterator to the beginning of a container or array(C++11)β (function template)(C++14)
Thatβs the whole see-also situation, and yes, every line is here. iykyk. π§
π source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::array:...ray::cbegin(3) β bye bestie, go slay some iterators! π«Άπ
π¦ 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