#include <span>
#include <array>
#include <iostream>
constexpr std::span<const int> createSpan(const int* arr, std::size_t size) {
return std::span<const int>(arr, size);
}
constexpr int calculateSum(std::span<const int> sp) {
int sum = 0;
for (const auto& value : sp) {
sum += value;
}
return sum;
}
int main() {
constexpr std::array<int, 5> arr = {1, 2, 3, 4, 5};
constexpr auto sp = createSpan(arr.data(), arr.size());
constexpr int total = calculateSum(sp);
std::cout << "Sum: " << total << std::endl; // Outputs: Sum: 15
return 0;
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?