Issue with copy_if

Hello all,

For some reason, the compiler tells me "copy_if was not declared in this scope" when I compile the code below:

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

struct pospos {
	bool operator()( int n ) { return n > 10; }
};

int main() {
	vector<int> vv(20);
	vector<int> tt(20);
	
	for( int i = 0; i < 20; ++i ) vv[i] = i;
	
	cout << "First Print: " << endl;
	for( int i = 0; i < 20; ++i ) cout << "vv[" << i << "]: " << vv[i] << endl;
	for( int i = 0; i < 20; ++i ) cout << "tt[" << i << "]: " << tt[i] << endl;
	
	copy_if( vv.begin(), vv.end(), tt.begin(), pospos() );
	
	cout << "Second Print: " << endl;
	for( int i = 0; i < 20; ++i ) cout << "tt[" << i << "]: " << tt[i] << endl;
}
Last edited on
closed account (3qX21hU5)
Works fine for me... Are you using a C++11 compiler? If so are you using codeblocks? If so I have had this happen to me a few times when I use the same project for multiple things (Mainly writing up code for here). Cleaning the build might work if not try creating a new project and then recompiling.

Otherwise it should work just fine.
Last edited on
copy_if is a C++11 addition, it's likely that your development environment isn't configured for C++11 or is just too old.
Here's your program in C++11 mode: http://ideone.com/oJ0eOP
Here's your program in C++98 mode: http://ideone.com/KqgtYH
Last edited on
Understood. I was using the g++ compiler. I switched to a newer version and that fixed it. Thanks guys.
Topic archived. No new replies allowed.