#include <vector>
#include <iostream>
#include <algorithm>
int main () {
std::vector<int> v {1,2,3,4,5,6,7};
auto i = rotate(begin(v), begin(v)+2, end(v));
for (int x : v) { std::cout << x <<' '; } // 3 4 5 6 7 1 2std::cout << '\n';
// returned iterator refers to old first:
auto const value = *i; // 1
auto const index = distance(begin(v), i); // 5std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';
}
#include <vector>
#include <iostream>
#include <algorithm>
int main () {
std::vector<int> v {1,2,3,4,5,6,7,8};
int const by = 3;
auto result = std::ranges::shift_left(v, by);
for (int x : result) { std::cout << x <<' '; } // 4 5 6 7 8std::cout << '\n';
}
#include <vector>
#include <iostream>
#include <algorithm>
int main () {
std::vector<int> v {1,2,3,4,5,6,7,8};
int const by = 3;
auto result = std::ranges::shift_right(v, by);
for (int x : result) { std::cout << x <<' '; } // 1 2 3 4 5std::cout << '\n';
}
#include <vector>
#include <iostream>
#include <algorithm>
int main () {
std::vector<int> in {6,2,3,5,1};
// get sorted copy of 3 smallest elements:
int const n = 3;
std::vector<int> out;
out.resize(n);
partial_sort_copy(begin(in), end(in), begin(out), end(out));
for (int x : out) { std::cout << x <<' '; } // 1 2 3std::cout << '\n';
// if output is larger than input:
out.resize(100);
auto const e = partial_sort_copy(begin(in), end(in), begin(out), end(out));
out.erase(e, end(out)); // shrink to fit
for (int x : out) { std::cout << x <<' '; } // 1 2 3 5 6std::cout << '\n';
}
compares elements using operator < by default;
a custom function(object) for comparing elements can be passed as 3rd argument
#include <vector>
#include <iostream>
#include <algorithm>
int main () {
std::vector<int> in {6,2,3,5,1};
// get sorted copy of 3 smallest elements:
int const n = 3;
std::vector<int> out;
out.resize(n);
std::ranges::partial_sort_copy(in, out);
for (int x : out) { std::cout << x <<' '; } // 1 2 3std::cout << '\n';
}
nth_elementnth_element
compares elements using operator < by default;
a custom function(object) for comparing elements can be passed as 4th argument
#include <vector>
#include <iostream>
#include <algorithm>
int main () {
std::vector<int> v {4,2,5,6,3,7,1};
// get 5th element in sorted order
int const n = 4;
auto nth = begin(v) + n;
nth_element(begin(v), nth, end(v));
std::cout << *nth; // 5std::cout << '\n';
}
compares elements using operator < by default;
a custom function(object) for comparing elements can be passed as 3rd argument
#include <vector>
#include <iostream>
#include <algorithm>
int main () {
std::vector<int> v {4,2,5,6,3,7,1};
// get 5th element in sorted order
int const n = 4;
auto nth = begin(v) + n;
std::ranges::nth_element(v, nth);
std::cout << *nth; // 5std::cout << '\n';
}
is_sortedis_sorted
compares elements using operator < by default;
a custom function(object) for comparing elements can be passed as 3rd argument
#include <vector>
#include <iostream>
#include <algorithm>
int main () {
std::vector<int> v {2,3,4,5,4,2,0,7,8};
// test from 1st to 7th (as shown in image):
auto i = is_sorted_until(begin(v), begin(v)+7);
// print sorted subrange
auto const print = [](int x){ std::cout << x << ' '; };
std::for_each(begin(v), i, print); // 2 3 4 5std::cout << '\n';
}
compares elements using operator < by default;
a custom function(object) for comparing elements can be passed as 2nd argument
Comments…