|
-
November 28th, 2022, 10:08 AM
#14
Re: use of algorithms in function cause ERROR: no instance of overloaded function
I have never tried it before, but std::span of C++ 20 seems a nice way of using C-style arrays with the standard library. An advantage is that it works with std::vector and std::array too.
https://www.modernescpp.com/index.php/c-20-std-span
(In C++ 23, there is also the std::mdspan. It will allow you to view a one-dimensional array (std::vector, std::array, or C-style array) as a multidimensional array.)
https://www.youtube.com/watch?v=aFCLmQEkPUw
Code:
#include <iostream>
#include <algorithm>
#include <span>
#include <vector>
#include <array>
constexpr size_t ELEMENTS { 8 };
void show(std::span<int> arr) {
for (const auto& c : arr)
std::cout << c;
std::cout << '\n';
}
void bool_element_option_03(std::span<int> arr_value, std::span<int> arr_copy_value) {
std::copy(std::begin(arr_value), std::end(arr_value), std::begin(arr_copy_value));
std::sort(std::rbegin(arr_copy_value), std::rend(arr_copy_value));
std::cout << "\nReverse sorted:\n";
show(arr_copy_value);
}
void test() {
int arr_value[ELEMENTS] { 1, 2, 9, 4, 5, 6, 7, 8 };
int arr_copy_value[ELEMENTS] {};
// works too
// std::vector<int> arr_value { 1, 2, 9, 4, 5, 6, 7, 8 };
// std::vector<int> arr_copy_value(arr_value.size(), {});
// works too
// std::array<int, ELEMENTS> arr_value { 1, 2, 9, 4, 5, 6, 7, 8 };
// std::array<int, ELEMENTS> arr_copy_value {};
std::cout << "\nOriginal order:\n";
show(arr_value);
bool_element_option_03(arr_value, arr_copy_value);
}
Last edited by wolle; November 30th, 2022 at 03:47 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|