#include
#include
#include
int main()
{
std::vector v = { 1, 2, 3 };
std::span myintspan = v;
myintspan[2] = 256;
for (auto el : v)
{
std::cout << el << '
';
}
}
Here, we created a span that references vector elements. Then we used the span to change the vector’s third element. With span, we do not have to worry about passing a pointer and a length around, and we just use the neat syntax of a span wrapper. Since the size of the vector can change, we say our span has a dynamic extent . We can create a fixed-size span from a fixed-sized array. We say our span now has a static extent. Example:
#include
#include
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
std::span myintspan = arr;
myintspan[4] = 10;
for (auto el : arr)
{
std::cout << el << '
';
}
}
20.8 Mathematical Constants
C++20 standard introduces a way to represent some of the mathematical constants. To use them, we need to include the header. The constants themselves are inside the std::numbers namespace. The following example shows how to use numbers pi and e, results of logarithmic functions and square roots of numbers 2 and 3:
#include
#include
int main()
{
std::cout << "Pi: " << std::numbers::pi << '
';
std::cout << "e: " << std::numbers::e << '
';
std::cout << "log2(e): " << std::numbers::log2e << '
';
std::cout << "log10(e): " << std::numbers::log10e << '
';
std::cout << "ln(2): " << std::numbers::ln2 << '
';
std::cout << "ln(10): " << std::numbers::ln10 << '
';
std::cout << "sqrt(2): " << std::numbers::sqrt2 << '
';
std::cout << "sqrt(3): " << std::numbers::sqrt3 << '
';
}
ref:
Slobodan Dmitrović 《Modern C++ for Absolute Beginners》
-End-