我已经看到了operator()STL容器的使用,但它是什么,你什么时候使用它?
我发现在这么喜欢仿函数的一些很好的例子这一个,所有的说服力的例子似乎在定义类的使用状态operator().
我在一本书中找到了一个例子来定义没有状态的函数调用操作符,我不禁觉得这是一个尴尬的用法,而且一个普通的样式函数指针,会比operator()在各方面使用更好 - 更少的代码,更少的变量(你必须实例化比较器),它可能由于实例化而更有效,并且没有意义或封装的损失(因为它只是一个函数).
我知道std::sort让你在operator()类和函数之间进行选择,但由于上述逻辑,我总是只使用这些函数.
为什么选择课程的原因是什么?
这是一个例子(释义):
class Point2D {
//.. accessors, constructors
int x,y;
};
class HorizComp {
public:
bool operator()(const Point2D& p, const Point2D& q) const
{ return p.getX() < q.getX(); }
};
class VertComp {
public:
bool operator()(const Point2D& p, const Point2D& q) const
{ return p.getY() < q.getY(); }
};
template <typename E, typename C>
void printSmaller(const E& p, const E& q, const C& isLess) { …Run Code Online (Sandbox Code Playgroud) 我试图在那里找出 - 一个很好的技术原因来定义一个只包含一个成员的类,这个成员碰巧是一个operator().
我偶然发现了一个人 - 无论出于什么原因 - 创建了一个包含几个类的命名空间,但每个类只包含一个operator()作为成员.
我很清楚,这些类可能会被用作方法(很可能),但为什么这是一个很好的技术方法(我假设有一个很好的方法),而不是简单地定义一组不同的方法单例类,在这种特殊情况下属于上面提到的命名空间.
命名空间看起来像:
namespace myNamespace {
class ClassA {
public:
void operator()();
};
class ClassB {
public:
int operator()(arg1, arg2);
};
...
}
Run Code Online (Sandbox Code Playgroud)
这仅仅是个人品味/风格的问题还是这是先进/精致设计的表现?我认为这个设计包含了我尚未收集的一些明智的知识,但另一方面,我没有找到一篇文章或问题来讨论这一点 - 这让我怀疑设计中的这种"明智的知识".
这里的专家有什么要说的?
提前致谢!
编辑
再次问好!我接受这个问题是一个重复的问题,其答案可以在这里找到.
"Functor"一词对我来说是未知的,在试图找到我怀疑的答案时(在提出这个问题之前),我没有看到任何对这个术语的引用.
我仍然接受谢尔盖的答案,因为他的答案向我介绍了这个词,他的解释是回答我的问题.他的回答 - 从我的观点来看 - 通过我在上面所说的链接中找到的答案得到了扩展.
谢谢!
我有两个文件:main.cpp并crypto.h包含模板类Crypto.在类构造函数中,我需要传递一个函数指针并将其分配给(*keyGen)方法.我需要传递的函数有一个可选的参数
template <class keyType>
class Crypto {
private:
keyType (*keyGen)(keyType);
public:
Crypto(keyType (*keyGen)(keyType)) {
this->keyGen = keyGen;
}
void decode() {
keyType foundKey;
vector<string> output;
keyType curKey = keyGen(); // Here keyGen has no args
// if curKey does not decode the input
curKey = keyGen(curKey); // Here keyGen has 1 arg of type keyType
// else foundKey = curKey;
// Save the decoded file
}
};
Run Code Online (Sandbox Code Playgroud)
在main.cpp中
int keyGen(int key = -1) { // This …Run Code Online (Sandbox Code Playgroud) template <class T> struct greater : binary_function <T, T, bool> {
bool operator() (const T& x, const T& y) const {
return x > y;
}
};
Run Code Online (Sandbox Code Playgroud)
我在STL库中找到了"函数对象类的大于不等式比较"的定义.有人可以向我解释这段代码是如何工作和编译的吗?
我有class Passanger变量string name; string station; string ticket;
,然后我有另一个类,在这个类我有vector<Passanger*> myQueue;
现在我想stable_sort用来排序myQueue.有没有可能,怎么说stable_sort,什么应该是关键,根据它应该排序myQueue?
std::stable_sort(myQueue.begin(),myQueue.end(), maybeSomethingElse() ); ?
我需要实现算法,使用模板递归计算两个向量的标量积.
有我的代码:
#include <iostream>
#include <vector>
template<typename T, int Size>
T scalar_product(const std::vector<T> &a, const std::vector<T> &b)
{
return a[Size - 1]*b[Size - 1] + (scalar_product<T, Size - 1>(a, b));
}
template<typename T>
T scalar_product<T, 0>(const std::vector<T> &a, const std::vector<T> &b) // error!
{
return a[0] * b[0];
}
int main()
{
std::vector<int> a(3, 1);
std::vector<int> b(3, 3);
std::cout << scalar_product<int, 3>(a, b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我使用这种专业化T scalar_product<T, 0>(...),我会收到错误" 'scalar_product':非法使用显式模板参数 ".但是,如果我像这个T scalar_product(...)编译器报告一样删除它,递归将是无限的(因为没有专业化,据我所知).
这里有很多这类问题,但我找不到有用的答案.如何在不使用类的情况下专门化这个功能?先谢谢你了!
我发现仿函数可以用来模拟在这样的函数中定义一个函数
using namespace std;
int main(int argc, char* argv[])
{
struct MYINC {
int operator()(int a) { return a+1; }
} myinc;
vector<int> vec;
for (int i = 0; i < 10; i++) vec.push_back(myinc(i));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我将它传递给外部函数,如std :: transform,如下例所示,我有一个编译错误说error: no matching function for call to ‘transform(std::vector<int>::iterator, std::vector<int>::iterator, std::vector<int>::iterator, main(int, char**)::MYINC&)’
using namespace std;
int main(int argc, char* argv[])
{
struct MYINC{
int operator()(int a) { return a+1; }
} myinc;
vector<int> vec;
for (int i = …Run Code Online (Sandbox Code Playgroud) 你能解释一下这段代码是如何工作的吗?它成功地计算了具有偶数值的向量元素,但我不清楚在这种特殊情况下绑定是如何工作的。
count_if(vec.begin(), vec.end(),
std::bind(logical_not<bool>(),
std::bind(modulus<int>(), placeholders::_1, 2)));
Run Code Online (Sandbox Code Playgroud) 我目前正在编写一个程序,它读取一个非常大的文本文件,并按字母顺序和字符长度对文本文件进行排序.我实施了一个快速排序来做到这一点.即时通讯的问题,希望能得到一些明确的解决方案是我有两种方法可以解决问题.这里是quickSortLen的代码
void SortingCompetition::quickSortLen(vector<char*>& words, int left, int right){
int i, j, middle, underMiddle, overMiddle;
char* pivot;
//Median of FIVE pivot point
i = left;
j = right;
middle = left + (right - left) / 2;
underMiddle = left + (middle - left) / 2;
overMiddle = middle + (right - middle) / 2;
//Right and Left
if(strlen(words[right]) < strlen(words[left]))
{
swap(words[right], words[left]);
}
// 4/5 and left
if(strlen(words[overMiddle]) < strlen(words[left]))
{
swap(words[overMiddle], words[left]);
}
//Middle and Left
if(strlen(words[middle]) < …Run Code Online (Sandbox Code Playgroud)