not1 and not2 function templates in C++ STL with Examples
These are functions which takes unary and binary function object(functors) and returns complement of that function objects. It can be usable in competitive programming to get complement of binary and unary functions.
These functions are very useful when writing code for complement function is harder or lengthy. For example, if we want to delete consonants from a string then we can write a unary function for isVowel and can use its complement in the remove_if function of STL.
- not1: This function takes unary function object and returns its complement object. Here we need to define argument_type for unary functor in our functor.
Syntax:
template <class Predicate> unary_negate<Predicate> not1 (const Predicate& pred) { return unary_negate<Predicate>(pred); }Parameters: The function accepts one mandatory parameter pred which specifies the unary function object to be negated.
Return Value: The function returns the negated unary object.
Example:
// C++ program to demonstrate// not1 function template#include <bits/stdc++.h>usingnamespacestd;structpred {booloperator()(constinti)const{return(i % 2 == 0);}// defines argument_type for unary functor// it is begin used by not1// as argument_typetypedefintargument_type;};// Driver codeintmain(){vector<int> odd{ 1, 2, 3, 4, 5,6, 7, 8, 9, 10 };// simple pred functor use// it removes even numbersautoend= remove_if(odd.begin(),odd.end(),pred());odd.resize(distance(odd.begin(), end));cout <<"removal of even elements: ";for(inti = 0; i < odd.size(); i++) {cout << odd[i] <<" ";}cout <<"\n";vector<int> even{ 1, 2, 3, 4, 5,6, 7, 8, 9, 10 };// complement of pred using not1// which removes odd numbersend= remove_if(even.begin(),even.end(),not1(pred()));even.resize(distance(even.begin(), end));cout <<"removal of odd elements: ";for(inti = 0; i < even.size(); i++) {cout << even[i] <<" ";}return0;}chevron_rightfilter_noneOutput:removal of even elements: 1 3 5 7 9 removal of odd elements: 2 4 6 8 10
- not2: This function takes binary functor as argument and returns complement functor of it.
Syntax:
template <class Predicate> binary_negate<Predicate> not2 (const Predicate& pred) { return binary_negate<Predicate>(pred); }Parameters: The function accepts one mandatory parameter pred which specifies the binary function object to be negated.
Return Value: The function returns the negated binary object.
Example:
// C++ program to demonstrate// not2 function template#include <bits/stdc++.h>usingnamespacestd;intmain(){vector<int> vec{ 4, 2, 7, 9, 3, 5, 10, 6 };// simple use of not2 just provide binary// functor and it will return it complementsort(vec.begin(), vec.end(), not2(less<int>()));for(inti = 0; i < vec.size(); i++) {cout << vec[i] <<" ";}return0;}chevron_rightfilter_noneOutput:10 9 7 6 5 4 3 2
References:
- http://www.cplusplus.com/reference/functional/not1/
- http://www.cplusplus.com/reference/functional/not2/
Recommended Posts:
- Variadic function templates in C++
- Templates in C++
- Templates and Static variables in C++
- Templates and Default Arguments
- Templates in C++ vs Generics in Java
- ios bad() function in C++ with Examples
- ios eof() function in C++ with Examples
- ios setstate() function in C++ with Examples
- ios fail() function in C++ with Examples
- ios clear() function in C++ with Examples
- wcslen() function in C++ with Examples
- ios good() function in C++ with Examples
- ios rdstate() function in C++ with Examples
- ios operator() function in C++ with Examples
- ios operator() function in C++11 with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Aakash_Panchal


