std::begin(std::valarray)(3)
πΈ NAME πΈ
std::begin(std::valarray) - std::begin(std::valarray).
Okay so bestie, this is the std::begin(std::valarray) page from the C++ Standard Libary manual, section 3. Itβs giving βtiny iterator helper with main character energyβ π
.
π Synopsis π
template< class T > (1) (since C++11)
/* see below */ begin( valarray<T>& v );
template< class T > (2) (since C++11)
/* see below */ begin( const valarray<T>& v );
The overload of std::begin for valarray returns an iterator of unspecified type referring to the first element in the numeric array. Lowkey mysterious, highkey useful β¨.
- The return type must:
- meet the requirements of mutable LegacyRandomAccessIterator,
- model
contiguous_iterator(sinceC++20), - have a member type
value_type, which isT, and - have a member type
reference, which isT&.
- The return type must:
- meet the requirements of constant LegacyRandomAccessIterator,
- model
contiguous_iterator(sinceC++20), - have a member type
value_type, which isT, and - have a member type
reference, which isconst T&.
The iterator returned from this function is invalidated when the member function resize() is called on v or when the lifetime of v ends, whichever comes first. No cap, that timing detail matters π.
πΈ Parameters πΈ
- v β a numeric array.
π Return value π
Iterator to the first value in the numeric array. Slay.
π¦ Exceptions π¦
May throw implementation-defined exceptions.
π Notes π
Unlike other functions that take std::valarray arguments, begin() cannot accept the replacement types (such as the types produced by expression templates) that may be returned from expressions involving valarrays: std::begin(v1 + v2) is not portable, and std::begin(std::valarray<T>(v1 + v2)) has to be used instead. Thatβs the gotcha, bestie β donβt get got π.
The intent of this function is to allow range for loops to work with valarrays, not to provide container semantics. Periodt.
π¬ Example π¬
// Run this code
#include <algorithm>
#include <iostream>
#include <valarray>
void show(const std::valarray<int>& v)
{
std::for_each(std::begin(v), std::end(v), [](int c)
{
std::cout << c << ' ';
});
std::cout << '\n';
};
int main()
{
const std::valarray<int> x{47, 70, 37, 52, 90, 23, 17, 33, 22, 16,
21, 4};
const std::valarray<int> y{25, 31, 71, 56, 21, 21, 15, 34, 21, 27,
12, 6};
show(x);
show(y);
const std::valarray<int> z{x + y};
for (char c : z)
std::cout << c;
}
β¨ Output β¨
47 70 37 52 90 23 17 33 22 16 21 4
25 31 71 56 21 21 15 34 21 27 12 6
Hello, C++!
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR:
LWG 2058- Applied to:
C++11 - Behavior as published:
begin()was required to support replacement types- it was unspecified when the returned iterators will be invalidated
- Correct behavior:
- not required
- specified
- Applied to:
π See also π
std::end(std::valarray)β specializesstd::end(C++11) (function template).
Thatβs the whole page, bestie π«Ά β source/version: http://cppreference.com, date: 2024.06.10 β mwah π
π¦ 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