下面的代码不能在 Visual Studio 中编译,给出
错误 C2672 'operator __surrogate_func':找不到匹配的重载函数 sortms C:\Users\David\source\repos\sortms\sortms.cpp 103
当我使用 C 样式数组时,该函数按编写方式工作。如果我使用指定的注释代码,该函数也可以工作input.begin(), input.end(), output.begin()
。
我正在使用 Visual Studio Community 2019 v16.8.6,使用该/std:c++latest
选项进行编译。我认为标准容器是基于范围的?
有人可以帮助我更好地理解std::range::copy()
vsstd::copy()
在处理向量或其他标准容器时的优势吗?
#include <iostream>
#include <ranges>
#include <algorithm>
#include <array>
#include <utility>
#include <vector>
#include <functional>
#include <string>
#include <concepts>
void ranges_copy_demo()
{
std::cout << "\nstd::ranges::copy()\n";
/*
int const input[] = { 1, 2, 3, 5, 8, 13, 21, 34, 45, 79 };
int output[10] = { 0 };
*/
std::vector<int> input = { 1, 2, 3, 5, 8, 13, 21, 34, 45, 79 };
std::vector<int> output(10, 0);
// auto r1 = std::ranges::copy(input.begin(), input.end(), output.begin());
auto r1 = std::ranges::copy(input, output);
print_range("copy output", output, r1);
// copy number divisible by 3
// auto r2 = std::ranges::copy_if(input.begin(), input.end(), output.begin(), [](const int i) {
auto r2 = std::ranges::copy_if(input, output, [](const int i) {
return i % 3 == 0;
});
print_range("copy_if %3 output", output, r2);
// copy only non-negative numbers from a vector
std::vector<int> v = { 25, 15, 5, 0, -5, -15 };
// auto r3 = std::ranges::copy_if(v.begin(), v.end(), output.begin(), [](const int i) {
auto r3 = std::ranges::copy_if(v, output, [](const int i) {
return !(i < 0);
});
print_range("copy_if !(i < 0) output", output, r3);
}