sort sub vector, sort range of vector c++
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {9, 2, 5, 1, 7, 4, 8, 3, 6};
// Sorting a sub vector from index 2 to index 6
std::sort(vec.begin() + 2, vec.begin() + 7);
// Output the sorted sub vector
std::cout << "Sorted sub vector: ";
for (int i = 0; i < vec.size(); ++i) {
std::cout << vec[i] << " ";
}
std::cout << std::endl;
return 0;
}
This C++ code snippet demonstrates how to sort a subvector within a vector. The std::sort
function is used with the iterator range from the third element (vec.begin() + 2
) to the seventh element (vec.begin() + 7
) of the vector vec
. The resulting sorted subvector from index 2 to index 6 is then printed to the console.