not2 stl 否定器
在学习STL时,我试图用 not2 来否定函子,但遇到了问题。 示例如下:
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
struct mystruct : binary_function<int,int,bool> {
bool operator() (int i,int j) { return i<j; }
};
template <class T>
class generatore
{
public:
generatore (T start = 0, T stp = 1) : current(start), step(stp)
{ }
T operator() () { return current+=step; }
private:
T current;
T step;
};
int main () {
vector<int> first(10);
generate(first.begin(), first.end(), generatore<int>(10,10) );
cout << "Smallest element " << *min_element(first.begin(), first.end(),mystruct() ) << endl;
cout << "Smallest element: " << *max_element(first.begin(), first.end(),not2(mystruct())) << endl;
}
最后一行代码将无法使用 g++ 进行编译。可能是一个愚蠢的错误,但在哪里?
Studying STL I have tried to negate a functor with not2 but encontered problems.
Here is the example:
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
struct mystruct : binary_function<int,int,bool> {
bool operator() (int i,int j) { return i<j; }
};
template <class T>
class generatore
{
public:
generatore (T start = 0, T stp = 1) : current(start), step(stp)
{ }
T operator() () { return current+=step; }
private:
T current;
T step;
};
int main () {
vector<int> first(10);
generate(first.begin(), first.end(), generatore<int>(10,10) );
cout << "Smallest element " << *min_element(first.begin(), first.end(),mystruct() ) << endl;
cout << "Smallest element: " << *max_element(first.begin(), first.end(),not2(mystruct())) << endl;
}
Last line of code wil not compile using g++. Probably a stupid error but where ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
原因:
在这一行中:
not2
(mystruct()) 将返回一个std::binary_negate
:在 std::binary_negate 中,调用运算符是 const 非静态成员函数
The reason:
In this line:
not2
(mystruct()) will return astd::binary_negate
:In std::binary_negate, the calling operator is a const non-static member function
更改:
太:
评论中关于您的问题有两个隐含的问题:
1)为什么我应该向方法添加 const:
您应该使用 const,因为对象的成员没有更改。
创建正确的方法 const 很容易。随着 const 越来越深入地传播到类层次结构中,事后向方法添加 const 正确性可能会成为真正的噩梦。
2)为什么在方法中添加const才能编译。
not2() 返回的对象的构造函数将对象的 const 引用作为参数。这是一种相对常见的技术,但确实要求您使用的任何方法也是 const。
Change:
too:
There are two implicit question on your question in the comments:
1) Why should I add const to the method:
You should use const as no members of the object are changed.
Creating methods const correct is easy. Adding const correctness to methods after the fact can become a real nighmare as the const propogates further and further into your class hierarchy.
2) Why adding const to the method makes it compile.
The constructor of the object returned by not2() is taking a const reference to your object as a parameter. This is a relatively common technique, but does require that any methods that you use are also const.
mystruct operator() 应该如下所示:
mystruct operator() should look like this:
你的函子中缺少 const
const missing in your functor