Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 508141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:52:35+00:00 2026-05-13T06:52:35+00:00

Studying STL I have tried to negate a functor with not2 but encontered problems.

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:52:36+00:00Added an answer on May 13, 2026 at 6:52 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While studying about JMX, I have seen one of the important feature of it
I have very technical question, I was working with C, and now I'm studying
While studying C# in ASP.net I have trouble understanding several classes. In which scenario
In my adventures studying the boost libraries, I've come across function signatures that have
Studing STL I have written a a simple program to test functors and modifiers.
Currently I am studying Standard Template Library (STL). In this program I am storing
Im studying inline functions in C++ and have come to a section concerning limitations
I am new to studying jquery.ajax. Through some tutorials, I tried some code by
I'm studying java web services and trying to follow the oracle tutorial but it
I have just started studying Python . I am referring to the tutorials at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.