not2 stl 否定器

发布于 2024-08-14 00:37:28 字数 907 浏览 2 评论 0原文

在学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南冥有猫 2024-08-21 00:37:28
struct  mystruct : binary_function<int,int,bool> {
        bool operator() (int i,int j) const // add a const here
            { return i<j; }
};

原因:

在这一行中:

cout << "Smallest element:  " << *max_element(first.begin(), first.end(),not2(mystruct())) << endl

not2(mystruct()) 将返回一个 std::binary_negate

template<class AdaptableBinaryPredicate>
binary_negate<AdaptableBinaryPredicate> not2( P const& pred ) {
        return binary_negate<AdaptableBinaryPredicate>(pred);
}

在 std::binary_negate 中,调用运算符是 const 非静态成员函数

template <class AdaptableBinaryPredicate>
class binary_negate
        : public binary_function
                     <typename AdaptableBinaryPredicate::first_argument_type
                     ,typename AdaptableBinaryPredicate::second_argument_type
                     ,bool>
{
        AdaptableBinaryPredicate pred_;
public:
        explicit binary_negate ( const AdaptableBinaryPredicate& pred )
                : pred_ (pred) {} // holds the original predication

         bool operator() (const typename Predicate::first_argument_type& x,
                          const typename Predicate::second_argument_type& y
                         ) const  // the calling operator is const
         { return !pred_(x,y);    // call original predication and negate it!

           // the calling operator of binary_negate is const 
           //   so pred_ in this member has const qualify
           //   and the calling operator of pred_ must be const
         }
};
struct  mystruct : binary_function<int,int,bool> {
        bool operator() (int i,int j) const // add a const here
            { return i<j; }
};

The reason:

In this line:

cout << "Smallest element:  " << *max_element(first.begin(), first.end(),not2(mystruct())) << endl

not2(mystruct()) will return a std::binary_negate:

template<class AdaptableBinaryPredicate>
binary_negate<AdaptableBinaryPredicate> not2( P const& pred ) {
        return binary_negate<AdaptableBinaryPredicate>(pred);
}

In std::binary_negate, the calling operator is a const non-static member function

template <class AdaptableBinaryPredicate>
class binary_negate
        : public binary_function
                     <typename AdaptableBinaryPredicate::first_argument_type
                     ,typename AdaptableBinaryPredicate::second_argument_type
                     ,bool>
{
        AdaptableBinaryPredicate pred_;
public:
        explicit binary_negate ( const AdaptableBinaryPredicate& pred )
                : pred_ (pred) {} // holds the original predication

         bool operator() (const typename Predicate::first_argument_type& x,
                          const typename Predicate::second_argument_type& y
                         ) const  // the calling operator is const
         { return !pred_(x,y);    // call original predication and negate it!

           // the calling operator of binary_negate is const 
           //   so pred_ in this member has const qualify
           //   and the calling operator of pred_ must be const
         }
};
过度放纵 2024-08-21 00:37:28

更改:

bool operator() (int i,int j) { return i<j; }

太:

bool operator() (int i,int j) const { return i<j; }

评论中关于您的问题有两个隐含的问题:

1)为什么我应该向方法添加 const:

您应该使用 const,因为对象的成员没有更改。
创建正确的方法 const 很容易。随着 const 越来越深入地传播到类层次结构中,事后向方法添加 const 正确性可能会成为真正的噩梦。

2)为什么在方法中添加const才能编译。

not2() 返回的对象的构造函数将对象的 const 引用作为参数。这是一种相对常见的技术,但确实要求您使用的任何方法也是 const。

Change:

bool operator() (int i,int j) { return i<j; }

too:

bool operator() (int i,int j) const { return i<j; }

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.

扶醉桌前 2024-08-21 00:37:28

mystruct operator() 应该如下所示:

bool operator() (int i, int j) const // note the const

mystruct operator() should look like this:

bool operator() (int i, int j) const // note the const
只是一片海 2024-08-21 00:37:28
bool operator()  (int i,int j) const{ return i<j; }

你的函子中缺少 const

bool operator()  (int i,int j) const{ return i<j; }

const missing in your functor

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文