Pass a back_inserter as the output iterator in remove_copy_if algorithm

S

silverburgh.meryl

i have the following code, but it does not compile because I pass in
back_inserter(b) as the output iterator in the remove_copy_if
algorithm.
My questions are:
1. why it does not compile? Passing a back_inserter() works for copy()
algorithm, why not remove_copy_if()?
2. is there a work-around for my problem? I think one solution is to
allocate 'b' as big as 'a', but that seems to be a waste of memory for
the case of b is a lot smaller than a.

Thank you.

class EvenValue {
public:
bool operator() (float value) {
return (int)value %2 ? false : true;
}
};

float func1(vector<float>& a, float max ) {

vector<float> b;


vector<float>::iterator itr = remove_copy_if (a.begin(), a.end(),
back_inserter(b), EvenValue() );

// do something with 'b'
return 0.0;
}
 
S

silverburgh.meryl

Here is the compilation error:

../A.cpp: In member function 'float func1(vector<float>&()':
.../A.cpp:361: error: conversion from
 
A

Andrew Koenig

i have the following code, but it does not compile because I pass in
back_inserter(b) as the output iterator in the remove_copy_if
algorithm.

That's not the reason. The reason is that if you pass a back_inserter, you
get back a back_inserter as the result; you're trying to use that
back_inserter to initialize an ordinary iterator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top