A
arnuld
WANTED:
/* C++ Primer - 4/e
*
* Exercise: 9.26
* STATEMENT
* Using the following definition of ia, copy ia into a vector and
into a list. Use the single iterator form of erase to remove the
elements with odd values from your list * and the even values from your
vector.
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
*
*/
WHAT I GET:
/home/arnuld/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra
ex_09.26.cpp ex_09.26.cpp:
In function 'int main()': ex_09.26.cpp:43:
error: '_1' was not declared in this scope /home/arnuld/programming/c++ $
CODE:
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>
int main()
{
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
const size_t ia_size = sizeof( ia ) / sizeof( *ia );
/* I made this array behave like a container:
"ia_begin" is the pointer to 1st element of ia
"ia_end" is the pointer to one past the last element of 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 ) );
std::remove_if( ivec.begin(),
ivec.end(),
_1 % 2 == 0 );
/* print out the values */
std::copy( ivec.begin(), ivec.end(),
std:stream_iterator<int> (std::cout, "\n" ) );
return 0;
}
I am just trying to use Lambda from Std. Lib. Why it is the problem ?
-- arnuld
http://lispmachine.wordpress.com
/* C++ Primer - 4/e
*
* Exercise: 9.26
* STATEMENT
* Using the following definition of ia, copy ia into a vector and
into a list. Use the single iterator form of erase to remove the
elements with odd values from your list * and the even values from your
vector.
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
*
*/
WHAT I GET:
/home/arnuld/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra
ex_09.26.cpp ex_09.26.cpp:
In function 'int main()': ex_09.26.cpp:43:
error: '_1' was not declared in this scope /home/arnuld/programming/c++ $
CODE:
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>
int main()
{
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
const size_t ia_size = sizeof( ia ) / sizeof( *ia );
/* I made this array behave like a container:
"ia_begin" is the pointer to 1st element of ia
"ia_end" is the pointer to one past the last element of 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 ) );
std::remove_if( ivec.begin(),
ivec.end(),
_1 % 2 == 0 );
/* print out the values */
std::copy( ivec.begin(), ivec.end(),
std:stream_iterator<int> (std::cout, "\n" ) );
return 0;
}
I am just trying to use Lambda from Std. Lib. Why it is the problem ?
-- arnuld
http://lispmachine.wordpress.com