//******************************************************** // The following code example is taken from the book // C++20 - The Complete Guide // by Nicolai M. Josuttis (www.josuttis.com) // http://www.cppstd20.com // // The code is licensed under a // Creative Commons Attribution 4.0 International License // http://creativecommons.org/licenses/by/4.0/ //******************************************************** #include #include #include #include #include #include template void printSpan(std::span sp) { for (const auto& elem : sp) { std::cout << '"' << elem << "\" "; } std::cout << '\n'; } int main() { std::vector vec{"New York", "Tokyo", "Rio", "Berlin", "Sydney"}; // define view to first 3 elements: std::span sp3{vec.data(), 3}; std::cout << "first 3: "; printSpan(sp3); // sort referenced elements: std::ranges::sort(vec); std::cout << "first 3 after sort(): "; printSpan(sp3); // insert a new element: // - must reassign the internal array of the vector if it reallocated new memory auto oldCapa = vec.capacity(); vec.push_back("Cairo"); if (oldCapa != vec.capacity()) { sp3 = std::span{vec.data(), 3}; } std::cout << "first 3: "; printSpan(sp3); // let span refer to the last three elements: sp3 = std::span{vec.end()-3, vec.end()}; std::cout << "last 3: "; printSpan(sp3); // let span refer to the last three elements: sp3 = std::span{vec}.last<3>(); std::cout << "last 3: "; printSpan(sp3); }