Is () used in the bool operator()(int num) of the below code a another operator or it means something else ? Please help.
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
class IsOdd{
public:
bool operator()(int num){
return ((num%2)==1);
}
};
int main(){
vector <int> v {1,2,3,4,5,6,7,8,9,10};
vector <int>::iterator pend;
vector<int>:: iterator q;
pend = remove_if(v.begin(), v.end(), IsOdd());
for(q=v.begin(); q!=pend; q)
cout<<*q<<endl;
return 0;
}
CodePudding user response:
That means overloading the operator () to work with the IsOdd class.
Here is a sample of code of the overloaded operator in action:
#include <iostream>
#include <vector>
#include<algorithm>
class IsOdd {
public:
bool operator()(int num) {
return ((num % 2) == 1);
}
};
int main()
{
std::vector <int> v{ 1,2,3,4,5,6,7,8,9,10 };
IsOdd isOdd; // Defining object isOdd of the class IsOdd
for (int i = 0; i < v.size(); i )
{
if (isOdd(v[i])) std::cout << v[i] << " is odd." << std::endl;
else std::cout << v[i] << " is not odd." << std::endl;
}
return 0;
}
I've also removed the iterators and shown with a much simpler way to iterate through elements in a std::vector.
Also, you should not use the following in your code:
using namespace std;
...as it's considered as bad practice.
Output:
1 is odd.
2 is not odd.
3 is odd.
4 is not odd.
5 is odd.
6 is not odd.
7 is odd.
8 is not odd.
9 is odd.
10 is not odd.
CodePudding user response:
It's a function, and the ()
is part of the name - operator()
is a special spelling for "function that is used to make an object behave like a function".
(It's a bit strange to call it "operator", but the C committee prefers reusing keywords to introducing new ones.)
If you have an object of the IsOdd
type, such as
IsOdd is_odd;
then what looks like a regular function call,
is_odd(3)
is transformed behind the scenes into
is_odd.operator() (3)
CodePudding user response:
IsOdd
is a functor class. It has an overload of the function call operator, so that given an instance x
you can call it via x(42)
to determine if 42
is odd.
In the code it is used to pass a callable to the algorithm. Nowadays you would rather use a lambda:
auto isOdd = [](int x) { return ((num % 2) == 1);};
pend = remove_if(v.begin(), v.end(), isOdd);
or perhaps, if the functor is only needed for the algorithm but not elsewhere, define the lambda in-line:
pend = remove_if(v.begin(), v.end(), [](int x){ return ((num % 2) == 1);});
Is () used in the bool operator()(int num) of the below code a another operator or it means something else ?
Here:
pend = remove_if(v.begin(), v.end(), IsOdd());
the ()
in IsOdd()
is a call to the constructor to create an instance of the class IsOdd
. The operator()
of that instance is then called by the algorithm. If you like you could also call it yourself:
auto x = IsOdd(); // call constructor
std::cout << x(42); // call operator()
CodePudding user response:
It means it's a function named operator(), returns a boolean, and takes in an integer named num.
Edit: Its an overloaded function operator so you can have it take no arguments or the argument int num.