- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
template<class T>
struct gSorting : public std::binary_function<T, T,bool> {
bool operator() (int number, int n2)
{
cout << "int" << endl;
return (number<n2);
}
bool operator() (double number, double n2)
{
cout << "double" << endl;
return (number<n2);
}
bool operator() (const MYClass& obj1, const MYClass& obj2)
{
return (obj1.element<obj2.element);
}
};
int main () {
gSorting<int> sorting_object;
std::cout << std::boolalpha << sorting_object (2.0f, 4.3f) << ".\n";
std::getchar();
return 0;
}
这段代码有什么问题吗?它是通用的吗?或者是否有更好的方法来执行通用排序算法以包括我使用的所有类
它编译,输出指向 double 这很好,但是我怎样才能使它成为一个模板,而不必在声明中指定输入类型?
gSorting
------------^^^^ 我们不需要任何特定类型?我说得对吗
输出:
最佳答案
我会亲自为二元谓词定义一个类模板,并根据需要对其进行专门化,例如:
template <typename T>
struct gSorting
: std::binary_function<T const&, T const&, bool> // not really needed
{
bool operator()(T const& t0, T const& t1) const {
return t0 < t1;
}
};
template <>
struct gSorting<MyClass>
: std::binary_function<MyClass const&, MyClass const&, bool>
{
bool operator()(MyClass const& c0, MyClass const& c1) const {
return c0.element < c1.element;
}
};
在实际实现中,泛型版本的参数类型可能应该根据类型的种类和/或基于特征的特征来决定参数是按值还是按 const&
传递然后根据需要专门化。例如:
template <typename T>
struct argument_type
{
typedef typename std::conditional<
std::is_fundamental<T>::value, T, T const&>::type type;
};
template <typename T>
struct gSorting
: std::binary_function<typename argument_type<T>::type,
typename argument_type<T>::type, bool>
{
typedef typename argument_type<T>::type arg_type;
bool operator()(arg_type t0, arg_type t1) const {
return t0 < t1;
}
};
关于c++ - 广义排序函数和使用 binary_function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19083745/
这是我感兴趣的测试: http://www.itl.nist.gov/div898/handbook/eda/section3/eda35h3.htm 如何将这段代码改编成接受数值向量并返回指定要删除
我使用 TensorFlow 1.12 基于 Material 进行语义(图像)分割。使用多项式交叉熵损失函数,这会产生不错的结果,尤其是考虑到我使用的训练数据量稀疏,mIoU 为 0.44: 然而,
AFAIK,在 Hindley-Milner 类型系统中使用的统一可以通过在构造函数位置允许类型变量并在这种情况下放宽 arity 约束来推广以统一更高级的类型: f a ~ T a1 b1 f ~
在阅读 article 时在 Javascript 中实现通用 curry 时,我偶然发现了这段代码。 function curry(fn) { return (...xs) => { i
我想使用 C++14 中引入的广义 lambda 捕获(有关解释,请参阅 Move capture in lambda)。但是,我的代码的其余部分是 C++11 友好的。我想按照以下方式做一些事情 #
我正在尝试实现 this paper 中介绍的广义 Hough 变换在 MATLAB 中。我也试过使用 this document理解算法。我一直在研究如何计算梯度角以找到要在 R 表中使用的 Φ。
假设我们有这样一段代码: std::vector> tasks; 然后我们添加这样的任务: tasks.push_back([]() { // ... } 这行得通。但现在我们要添加该任务:
问题:为什么会打印出以下内容: ChildB___Parent of ChildB ChildB___Parent of ChildB 而不是我认为它应该打印出来的: ChildA___Parent
W3C 推荐 RDF 1.1 概念和抽象语法 defines多么广义 RDF 三元组 是(即,一个非标准三元组,其中每个主语/谓语/宾语都可以是 IRI/bnode/文字)。 关于它的可能用途,它给出
我是一名优秀的程序员,十分优秀!