我正在尝试为竞争性编程竞赛编写自己的库,我需要这样的代码:
#include <functional>
#include <algorithm>
template <typename T>
using binop = std::function<T (T, T)>;
int main()
{
binop<int> op = std::max<int>;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,它产生以下错误:
error: conversion from '<unresolved overloaded function type>' to non-scalar type 'binop<int> {aka std::function<int(int, int)>}' requested
Run Code Online (Sandbox Code Playgroud)
但是当我删除该行
#include <algorithm>
Run Code Online (Sandbox Code Playgroud)
它神奇地编译.(虽然不应该确实定义了最大函数)
问题是:如何在不删除"算法"的情况下编译代码?
请注意,我也试过这个:
binop<int> op = (int(*)(int, int)) std::max<int>;
Run Code Online (Sandbox Code Playgroud)
哪个产生
error: insufficient contextual information to determine type
Run Code Online (Sandbox Code Playgroud) 我在竞争性程序员手册中找到了一个递归代码来执行相同的操作,但我很难理解其背后的逻辑。
\n它指出:
\n\n与子集一样,排列可以使用递归生成。下面的函数搜索遍历集合 {0,1,...,n\xc2\xa11} 的排列。该函数\n构建一个包含该排列的向量排列,并且在不带参数调用该函数时\n开始搜索。\n
\n
void search() {\n if (permutation.size() == n) {\n // process permutation\n } else {\n for (int i = 0; i < n; i++) {\n if (chosen[i]) continue;\n chosen[i] = true;\n permutation.push_back(i);\n search();\n chosen[i] = false;\n permutation.pop_back();\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\n每个函数调用都会向排列添加一个新元素。所选数组\表示哪些元素已包含在排列中。如果\n排列的大小等于集合的大小,则已生成排列。\n
\n
我似乎无法理解正确的直觉和所使用的概念。
\n有人可以解释一下这段代码在做什么以及它背后的逻辑是什么吗?