从vector< int>中删除元素使用< algorithm> [英] removing elements from vector<int> using <algorithm>
问题描述
通缉:
/ * C ++入门 - 4 / e
*
*练习:9.26
*语句
*使用以下ia定义,将ia复制到一个向量中并将
复制到一个列表中。使用单个迭代器形式的擦除来删除带有列表中奇数值的
元素*和来自
向量的偶数值。
int ia [] = {0,1,1,2,3,5,8,13,21,55,89};
*
* /
我得到的:
/ home / arnuld / programming / c ++ $ g ++ -ansi -pedantic -Wall -Wextra
ex_09.26.cpp ex_09.26.cpp:
在函数''int main()''中:ex_09.26.cpp:43:
错误:''_1''未在此范围内声明/ home / arnuld / programming / c ++ $
代码:
#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);
>
/ *我使这个数组表现得像一个容器:
" ia_begin"指向ia的第一个元素的指针
" ia_end"指向过去最后一个元素的指针
* /
int * ia_begin = ia;
int * ia_end = ia + ia_size ;
std :: vector< intivec;
std :: list< int ilist;
/ *将数组中的元素复制到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);
/ *打印出值* /
std :: copy(ivec.begin(),ivec.end(),
std :: ostream_iterator< int(std :: cout," \ n"));
返回0;
}
我只想尝试使用Std的Lambda。库。为什么会出现这个问题?
- arnuld
http://lispmachine.wordpress.com
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<intivec;
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::ostream_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
推荐答案
g ++ -ansi -pedantic -Wall -Wextra
ex_09.26.cpp ex_09.26.cpp:
在函数''int main()''中:ex_09.26.cpp:43:
错误:''_1''未在此范围内声明/ 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++
代码:
#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);
/ *我使这个数组的行为像一个容器:
" ia_begin"指向ia的第一个元素的指针
" ia_end"指向过去最后一个元素的指针
* /
int * ia_begin = ia;
int * ia_end = ia + ia_size ;
std :: vector< intivec;
std :: list< int ilist;
/ *将数组中的元素复制到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);
/ *打印出值* /
std :: copy(ivec.begin(),ivec.end(),
std :: ostream_iterator< int(std :: cout," \ n"));
返回0;
}
我只想尝试使用Std的Lambda。库。为什么会出现这个问题?
- arnuld
http://lispmachine.wordpress.com
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<intivec;
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::ostream_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
" arnuld" < No **** @ NoPain.com在留言中写道
news:pa ************************** ** @ NoPain.com ...
"arnuld" <No****@NoPain.comwrote in message
news:pa****************************@NoPain.com...
通缉:
/ * C ++ Primer - 4 / e >
*
*练习:9.26
*语句
*使用以下ia定义,将ia复制到矢量中
列入清单。使用单个迭代器形式的擦除来删除带有列表中奇数值的
元素*和来自
向量的偶数值。
int ia [] = {0,1,1,2,3,5,8,13,21,55,89};
*
* /
我得到的:
/ home / arnuld / programming / c ++
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++
这篇关于从vector< int>中删除元素使用< algorithm>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!