Re: removing elements from vector<int> using <algorithm>
On Wed, 10 Oct 2007 08:07:23 +0000, Erik Wikstr?m wrote:
First, there is not lambda in the standard library (at least not yet,
they might add it in the next version) and second, you are trying to use
the boos lambda, but have not included any boost headers.
OK, I will not use BOOST for now.
By the way, the assignment wants you to use erase() (not sure if they
meant std::erase or std::vector<T>::erase(), I suspect the latter).
I tried it with vector's erase member function but that falls into the
infinite loop:
void rem_evens( std::vector<int>& ivec)
{
std::vector<int>::iterator begin = ivec.begin();
std::vector<int>::iterator end = ivec.end();
while( begin != end )
{
if( (*begin % 2) == 0 )
{
begin++ = ivec.erase( begin );
}
end = ivec.end();
}
}
int main()
{
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
const size_t ia_size = sizeof( ia ) / sizeof( *ia );
int *ia_begin = ia;
int *ia_end = ia + ia_size;
std::vector<int> ivec;
std::list<int> ilist;
/* copy elements from array to vector & list */
std::copy( ia_begin, ia_end, std::back_inserter( ivec ) );
std::copy( ia_begin, ia_end, std::back_inserter( ilist ) );
/* vector before values are removed */
std::copy( ivec.begin(), ivec.end(),
std::ostream_iterator<int>( std::cout, "\n" ) );
rem_evens( ivec );
/* vector after even values are removed */
std::copy( ivec.begin(), ivec.end(),
std::ostream_iterator<int>( std::cout, "\n" ) );
return 0;
}
-- arnuld
http://lispmachine.wordpress.com