#include <span>
#include <iostream>
#include <vector>
template<typename T>
void insert(std::span<T> span, size_t index, const T& value) {
// Create a new vector to hold the elements
std::vector<T> vec(span.begin(), span.end());
// Insert the value at the specified index
vec.insert(vec.begin() + index, value);
// Create a new span from the modified vector
std::span<T> newSpan(vec.data(), vec.size());
// Output the new span elements
for (const auto& e : newSpan) {
std::cout << e << ' ';
}
std::cout << std::endl;
}
template<typename T>
void erase(std::span<T> span, size_t index) {
// Create a new vector to hold the elements
std::vector<T> vec(span.begin(), span.end());
// Erase the element at the specified index
vec.erase(vec.begin() + index);
// Create a new span from the modified vector
std::span<T> newSpan(vec.data(), vec.size());
// Output the new span elements
for (const auto& e : newSpan) {
std::cout << e << ' ';
}
std::cout << std::endl;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
std::span<int> mySpan(arr);
std::cout << "Insert 10 at index 2: ";
insert(mySpan, 2, 10);
std::cout << "Erase element at index 1: ";
erase(mySpan, 1);
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?