Inverse modulus<type> return ?

Code below, I know is bad code, but is not even what I'm trying to achieve, that code is only an "excuse" to understand what can I do with the classes from the functional header and algorithm.
Now the code below binds num which is 12 to the modulus<int> class and check all the numbers in the vector nums against it, thus 12 % n, problem is that modulus return 0 when a number is factor of 12 so it only copy numbers that are NOT factors of 12 in the vector factors.

So my question is, there is a way to obtain the opposite return type from modulus<int> without having to use a lambda?
Like can I do some trickery using negate<bool> or something on those lines?
Or maybe a copy_if_not algorithm? (though I don't think there is one)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;


int main()
{
	vector<int> nums{ 1,2,3,4,5,6,7,8,9 };
	vector<int> factors(10);

	int num{ 12 };
	copy_if(nums.begin(), nums.end(), factors.begin(), bind1st(modulus<int>(), num));


	for (auto e : factors)
	{
		cout << e << " ";
	}

	return 0;
}
Last edited on
You can use std::not1

 
not1(bind1st(modulus<int>(), num))

or std::not2.

 
bind1st(not2(modulus<int>()), num)


http://www.cplusplus.com/reference/functional/not1/
http://www.cplusplus.com/reference/functional/not2/
Last edited on
Thank you Peter, that's exactly the kind of trick I was hoping to learn out of this topic :D
Topic archived. No new replies allowed.