C++23 Algorithms C++23 Algorithms C++23 Algorithms
Follow @hackingcpp on twitter so you don't miss any updates.
The algorithms below will be part of the C++23 standard library. They are now listed on the range algorithms overview page in the Beginner's Guide sections and on the Cheat Sheets page.
Note that it might take a few months until the example programs will run in Compiler Explorer as most of these new algorithms are not available in any standard library implementation yet.
#include <vector>
#include <iostream>
#include <algorithm>
int main () {
std::vector<int> v {2,1,7,1,1,5,8};
auto const result = std::ranges::find_last(v, 1);
if (not result.empty()) { // if found
auto const value = result.front(); // int value = 1
auto const index = distance(begin(v),begin(result)); // index = 4
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';
}
for (int x : result) { std::cout << x << ' '; } // 1 5 8
std::cout << '\n';
}
#include <vector>
#include <iostream>
#include <algorithm>
int main () {
std::vector<int> v {4,1,3,8,1};
auto const f = [](int x) { return x >= 2; };
auto const result = std::ranges::find_last_if (v, f);
if (not result.empty()) { // if found
auto const value = result.front(); // int value = 8
auto const index = distance(begin(v),begin(result)); // index = 3
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';
}
for (int x : result) { std::cout << x << ' '; } // 8 1
std::cout << '\n';
}
#include <vector>
#include <iostream>
#include <algorithm>
int main () {
std::vector<int> v {4,0,3,1,5};
auto const f = [](int x) { return x >= 2; };
auto const result = std::ranges::find_last_if_not(v, f);
if (not result.empty()) { // if found
auto const value = result.front(); // int value = 1
auto const index = distance(begin(v),begin(result)); // index = 3
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';
}
for (int x : result) { std::cout << x << ' '; } // 1 5
std::cout << '\n';
}
#include <vector>
#include <iostream>
#include <algorithm>
int main () {
std::vector<int> s {4,6,5,8,7,3};
std::vector<int> w {4,6,5};
if (std::ranges::starts_with(s, w) ) {
std::cout << "yes!\n";
}
}
#include <vector>
#include <iostream>
#include <algorithm>
int main () {
std::vector<int> s {4,6,5,8,7,3};
std::vector<int> w {8,7,3};
if (std::ranges::ends_with(s, w) ) {
std::cout << "yes!\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 8
std::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 5
std::cout << '\n';
}
#include <vector>
#include <iostream>
#include <numeric>
int main () {
std::vector<int> v;
v.resize(5,0);
auto const res = std::ranges::iota(v, 3);
std::cout << res.value << '\n'; // 8
for (int x : v) { std::cout << x << ' '; } // 3 4 5 6 7
std::cout << '\n';
}