从< list>复制元素到< deque> [英] copying elements from a <list> to <deque>

查看:69
本文介绍了从< list>复制元素到< deque>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

工作正常。任何关于使它变得更好的建议,或者我是否能够提高我的C ++编码技能:


/ * C ++ Primer - 4 / e

*

*第9章 - 顺序容器

*练习9.18 - 声明

*编写一个程序来复制列表中的元素; ints"

* to 2" deques"。列表元素甚至应该进入一个deque

*甚至元素应该进入第二个双端队列。

*

* /


#include< iostream>

#include< list>

#include< deque>

#include< algorithm>

#include< iterator>


int main()

{

std :: cout<< 输入一些整数:;

std :: list< intilist;

/ *将元素从std :: cin复制到ilist * /

std :: copy((std :: istream_iterator< int>(std :: cin)),

(std :: istream_iterator< int>()),

std :: back_inserter(ilist));


std :: deque< intdeque_of_evens;

std :: deque< intdeque_of_odds;

/ *将偶数元素复制到1个双端队列中,并将赔率复制到另一个* /

for(std :: list< int> :: const_iterator iter = ilist.begin();

iter!= ilist.end();

++ iter)

{

if(* iter%2 == 0)

{

deque_of_evens.push_back(* iter);

}

else

{

deque_of_odds.push_back(* iter);

}

}


std :: cout<< \ n打印甚至整数的Deque:;

std :: copy(deque_of_evens.begin(),

deque_of_evens.end(),
std :: ostream_iterator< int>(std :: cout,""));


std :: cout<< \ n \ n打印奇数整数的副词:" ;;

std :: copy(deque_of_odds.begin(),

deque_of_odds.end(),

std :: ostream_iterator< int>(std :: cout,""));


std :: cout<< std :: endl;


返回0;

}

-
http://lispmachine.wordpress.com

解决方案

在2007-09-25 09:59,arnuld写道:


它工作正常。任何关于使它变得更好的建议,或者我是否能够提高我的C ++编码技能:


/ * C ++ Primer - 4 / e

*

*第9章 - 顺序容器

*练习9.18 - 声明

*编写一个程序来复制列表中的元素; ints"

* to 2" deques"。列表元素甚至应该进入一个deque

*甚至元素应该进入第二个双端队列。

*

* /


#include< iostream>

#include< list>

#include< deque>

#include< algorithm>

#include< iterator>


int main()

{

std :: cout<< 输入一些整数:;

std :: list< intilist;

/ *将元素从std :: cin复制到ilist * /

std :: copy((std :: istream_iterator< int>(std :: cin)),

(std :: istream_iterator< int>()),

std :: back_inserter(ilist));


std :: deque< intdeque_of_evens;

std :: deque< intdeque_of_odds;

/ *将偶数元素复制到1个双端队列中,并将赔率复制到另一个* /

for(std :: list< int> :: const_iterator iter = ilist.begin();

iter!= ilist.end();

++ iter)

{

if(* iter%2 == 0)

{

deque_of_evens.push_back(* iter);

}

else

{

deque_of_odds.push_back(* iter);

}

}


std :: cout<< \ n打印甚至整数的Deque:;

std :: copy(deque_of_evens.begin(),

deque_of_evens.end(),
std :: ostream_iterator< int>(std :: cout,""));


std :: cout<< \ n \ n打印奇数整数的副词:" ;;

std :: copy(deque_of_odds.begin(),

deque_of_odds.end(),

std :: ostream_iterator< int>(std :: cout,""));


std :: cout<< std :: endl;


返回0;

}



你应该研究( IMO严重命名)remove_copy_if算法和

创建一个谓词来确定元素是否是偶数。


-

Erik Wikstr ?? m


Erik Wikstr?m写道:


2007-09-25 09: 59,arnuld写道:


>它工作正常。任何关于使它变得更好的建议,或者我是否可以提高我的C ++编码技能:

/ * C ++入门 - 4 / e
*
*第9章 - 顺序容器
*练习9.18 - 声明
*编写一个程序来复制
ints列表中的元素。 *至2deques。列表元素甚至应该进入一个双端*甚至元素应该进入第二个双端队列。
*
* /

#include< iostream>
#include< list>
#include< deque>
#include< algorithm>
#include< iterator>

int main ()
{
std :: cout<< 输入一些整数:;
std :: list< intilist;
/ *将元素从std :: cin复制到ilist * /
std :: copy((std ::) istream_iterator< int>(std :: cin)),
(std :: istream_iterator< int>()),
std :: back_inserter(ilist));

std :: deque< intdeque_of_evens;
std :: deque< intdeque_of_odds;
/ *将偶数元素复制成1个双端队列并将其复制到另一个* /
for(std :: list< int>: :const_iterator iter = ilist.begin();
iter!= ilist.end();
++ iter)
{
if(* iter%2 == 0)
{
deque_of_evens.push_back(* iter);
}

{
deque_of_odds.push_back(* iter);
}
}

std :: cout<< \ n打印甚至整数的Deque:;
std :: copy(deque_of_evens.begin(),
deque_of_evens.end(),
std :: ostream_iterator< int> (std :: cout,""));

std :: cout<< \ n \ n打印奇数整数的副词:" ;;
std :: copy(deque_of_odds.begin(),
deque_of_odds.end(),
std :: ostream_iterator< ; int>(std :: cout,""));

std :: cout<< std :: endl;

返回0;
}



你应该研究(IMO命名不当)remove_copy_if算法和

创建一个谓词来确定元素是否是偶数。



并且:你应该实现缺少的(sic!)copy_if算法并使用

相同的谓词用于其他复制作业。 />
最佳


Kai-Uwe Bux


9月25日上午11:24,Kai-Uwe Bux< jkherci ... @ gmx.netwrote:


Erik Wikstr?m写道:


在2007-09-25 09:59,arnuld写道:


它工作正常。任何关于使它变得更好的建议或者我是否能够提高我的C ++编码技能:


/ * C ++ Primer - 4 / e

*

*第9章 - 顺序容器

*练习9.18 - 声明

*编写一个程序来复制

ints列表中的元素*至2deques。列表元素甚至应该进入

一个deque *甚至元素应该进入第二个双端队列。

*

* /


#include< iostream>

#include< list>

#include< deque>

#include< algorithm>

#include <迭代>


int main()

{

std :: cout<< 输入一些整数:;

std :: list< intilist;

/ *将元素从std :: cin复制到ilist * /

std :: copy((std :: istream_iterator< int>(std :: cin)),

(std :: istream_iterator< int>()),

std :: back_inserter(ilist));


std :: deque< intdeque_of_evens ;

std :: deque< intdeque_of_odds;

/ *将偶数元素复制到1个双端队列中,将赔率复制到另一个* /

for(std) :: list< int> :: const_iterator iter = ilist.begin();

iter!= ilist.end();

++ iter)

{

if(* iter%2 == 0)

{

deque_of_evens.push_back(* iter);

}

其他

{

deque_of_odds.push_back(* iter);

}



您可以将if替换为:


(* iter%2 == 0?deque_of_evens:

deque_of_odds).push_back(* iter);


关于此的意见各不相同;我倾向于不使用?:非常适用于

lvalues,但在这种情况下,它确实引起了对所有*元素的最终结果的注意事实

两个deques中的一个。


}


std :: cout<< \ n打印甚至整数的Deque:;

std :: copy(deque_of_evens.begin(),

deque_of_evens.end(),
std :: ostream_iterator< int>(std :: cout,""));


std :: cout< < \ n \ n打印奇数整数的副词:" ;;

std :: copy(deque_of_odds.begin(),

deque_of_odds.end(),

std :: ostream_iterator< int>(std :: cout,""));


std :: cout< < std :: endl;

返回0;

}

< blockquote class =post_quotes>
你应该研究(IMO命名不当)remove_copy_if

算法并创建一个谓词来确定元素

是偶数还是不。


并且:你应该实现缺少的(sic!)copy_if算法

并使用相同的谓词对于其他复印工作。



我不确定我是否同意这些建议中的任何一个。

两者都混淆了混淆,强迫事情只是使用一个

标准(或非标准,在copy_if的情况下)算法。

对于一个更有经验的程序员,我可能会考虑一个

boost :: filter_iterator,例如:


std :: deque< int evens(

boost :: make_filter_iterator< IsEven>(ilist.begin(),

ilist.end()),

boost :: make_filter_iterator< IsEven>(ilist.end(),

ilist.end()));

std :: deque< int赔率(

boost :: make_filter_iterator< IsOdd>(ilist.begin(),

ilist.end()),

提升:: make_filter_iterator< IsOdd>(ilist.end(),

ilist.end()));


这样可以正确初始化;它甚至可以制作

evens和odds const。但是它仍然需要将测试移出循环中的
,甚至还需要将功能移出。而且我很确定

这不是演习的目的。


-

詹姆斯Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9个地方Sémard,78210 St.-Cyr-l''coco,法国,+ 33(0)1 30 23 00 34


It works fine. any advice on making it better or if I can
improve my C++ coding skills:

/* C++ Primer - 4/e
*
* Chapter 9 - Sequential Containers
* exercise 9.18 - STATEMENT
* Write a program to copy elements from a list of "ints"
* to 2 "deques". The list elements that are even should go into one deque
* and even elements should go into 2nd deque.
*
*/

#include <iostream>
#include <list>
#include <deque>
#include <algorithm>
#include <iterator>

int main()
{
std::cout << "enter some integers: ";
std::list<intilist;
/* copy elements from std::cin into ilist */
std::copy( (std::istream_iterator<int>( std::cin )),
(std::istream_iterator<int>()),
std::back_inserter( ilist ) );

std::deque<intdeque_of_evens;
std::deque<intdeque_of_odds;
/* copy even elements into 1 deque and odds into the other */
for( std::list<int>::const_iterator iter = ilist.begin();
iter != ilist.end();
++iter)
{
if( *iter % 2 == 0 )
{
deque_of_evens.push_back( *iter );
}
else
{
deque_of_odds.push_back( *iter );
}
}

std::cout << "\n Printing Deque of Even Integers: ";
std::copy( deque_of_evens.begin(),
deque_of_evens.end(),
std::ostream_iterator<int>( std::cout, " " ) );

std::cout << "\n\n Printing Deque of Odd Integers: ";
std::copy( deque_of_odds.begin(),
deque_of_odds.end(),
std::ostream_iterator<int>( std::cout, " " ) );

std::cout << std::endl;

return 0;
}
--
http://lispmachine.wordpress.com

解决方案

On 2007-09-25 09:59, arnuld wrote:

It works fine. any advice on making it better or if I can
improve my C++ coding skills:

/* C++ Primer - 4/e
*
* Chapter 9 - Sequential Containers
* exercise 9.18 - STATEMENT
* Write a program to copy elements from a list of "ints"
* to 2 "deques". The list elements that are even should go into one deque
* and even elements should go into 2nd deque.
*
*/

#include <iostream>
#include <list>
#include <deque>
#include <algorithm>
#include <iterator>

int main()
{
std::cout << "enter some integers: ";
std::list<intilist;
/* copy elements from std::cin into ilist */
std::copy( (std::istream_iterator<int>( std::cin )),
(std::istream_iterator<int>()),
std::back_inserter( ilist ) );

std::deque<intdeque_of_evens;
std::deque<intdeque_of_odds;
/* copy even elements into 1 deque and odds into the other */
for( std::list<int>::const_iterator iter = ilist.begin();
iter != ilist.end();
++iter)
{
if( *iter % 2 == 0 )
{
deque_of_evens.push_back( *iter );
}
else
{
deque_of_odds.push_back( *iter );
}
}

std::cout << "\n Printing Deque of Even Integers: ";
std::copy( deque_of_evens.begin(),
deque_of_evens.end(),
std::ostream_iterator<int>( std::cout, " " ) );

std::cout << "\n\n Printing Deque of Odd Integers: ";
std::copy( deque_of_odds.begin(),
deque_of_odds.end(),
std::ostream_iterator<int>( std::cout, " " ) );

std::cout << std::endl;

return 0;
}

You should research the (IMO badly named) remove_copy_if algorithm and
create a predicate to determine if the element is even or not.

--
Erik Wikstr??m


Erik Wikstr?m wrote:

On 2007-09-25 09:59, arnuld wrote:

>It works fine. any advice on making it better or if I can
improve my C++ coding skills:

/* C++ Primer - 4/e
*
* Chapter 9 - Sequential Containers
* exercise 9.18 - STATEMENT
* Write a program to copy elements from a list of
"ints" * to 2 "deques". The list elements that are even should go into
one deque * and even elements should go into 2nd deque.
*
*/

#include <iostream>
#include <list>
#include <deque>
#include <algorithm>
#include <iterator>

int main()
{
std::cout << "enter some integers: ";
std::list<intilist;
/* copy elements from std::cin into ilist */
std::copy( (std::istream_iterator<int>( std::cin )),
(std::istream_iterator<int>()),
std::back_inserter( ilist ) );

std::deque<intdeque_of_evens;
std::deque<intdeque_of_odds;
/* copy even elements into 1 deque and odds into the other */
for( std::list<int>::const_iterator iter = ilist.begin();
iter != ilist.end();
++iter)
{
if( *iter % 2 == 0 )
{
deque_of_evens.push_back( *iter );
}
else
{
deque_of_odds.push_back( *iter );
}
}

std::cout << "\n Printing Deque of Even Integers: ";
std::copy( deque_of_evens.begin(),
deque_of_evens.end(),
std::ostream_iterator<int>( std::cout, " " ) );

std::cout << "\n\n Printing Deque of Odd Integers: ";
std::copy( deque_of_odds.begin(),
deque_of_odds.end(),
std::ostream_iterator<int>( std::cout, " " ) );

std::cout << std::endl;

return 0;
}


You should research the (IMO badly named) remove_copy_if algorithm and
create a predicate to determine if the element is even or not.

And: you should implement the missing (sic!) copy_if algorithm and use the
same predicate for the other copy job.
Best

Kai-Uwe Bux


On Sep 25, 11:24 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:

Erik Wikstr?m wrote:

On 2007-09-25 09:59, arnuld wrote:

It works fine. any advice on making it better or if I can
improve my C++ coding skills:

/* C++ Primer - 4/e
*
* Chapter 9 - Sequential Containers
* exercise 9.18 - STATEMENT
* Write a program to copy elements from a list of
"ints" * to 2 "deques". The list elements that are even should go into
one deque * and even elements should go into 2nd deque.
*
*/

#include <iostream>
#include <list>
#include <deque>
#include <algorithm>
#include <iterator>

int main()
{
std::cout << "enter some integers: ";
std::list<intilist;
/* copy elements from std::cin into ilist */
std::copy( (std::istream_iterator<int>( std::cin )),
(std::istream_iterator<int>()),
std::back_inserter( ilist ) );

std::deque<intdeque_of_evens;
std::deque<intdeque_of_odds;
/* copy even elements into 1 deque and odds into the other */
for( std::list<int>::const_iterator iter = ilist.begin();
iter != ilist.end();
++iter)
{
if( *iter % 2 == 0 )
{
deque_of_evens.push_back( *iter );
}
else
{
deque_of_odds.push_back( *iter );
}

You might replace the if with:

(*iter % 2 == 0 ? deque_of_evens :
deque_of_odds).push_back( *iter ) ;

Opinions about this vary; I tend not to use ?: very much for
lvalues, but in this case, it does draw attention to the fact
that *all* of the elements end up in one of the two deques.

}

std::cout << "\n Printing Deque of Even Integers: ";
std::copy( deque_of_evens.begin(),
deque_of_evens.end(),
std::ostream_iterator<int>( std::cout, " " ) );

std::cout << "\n\n Printing Deque of Odd Integers: ";
std::copy( deque_of_odds.begin(),
deque_of_odds.end(),
std::ostream_iterator<int>( std::cout, " " ) );

std::cout << std::endl;
return 0;
}

You should research the (IMO badly named) remove_copy_if
algorithm and create a predicate to determine if the element
is even or not.

And: you should implement the missing (sic!) copy_if algorithm
and use the same predicate for the other copy job.

I''m not sure that I agree with either of these recommendations.
Both smack of obfuscation, and forcing things just to use a
standard (or non-standard, in the case of copy_if) algorithm.
For a more experienced programmer, I might consider a
boost::filter_iterator, e.g.:

std::deque< int evens(
boost::make_filter_iterator< IsEven >( ilist.begin(),
ilist.end() ),
boost::make_filter_iterator< IsEven >( ilist.end(),
ilist.end() ) ) ;
std::deque< int odds(
boost::make_filter_iterator< IsOdd >( ilist.begin(),
ilist.end() ),
boost::make_filter_iterator< IsOdd >( ilist.end(),
ilist.end() ) ) ;

This allows correct initialization; it would even allow making
evens and odds const. But it still requires moving the test out
of the loop, and even out of the function. And I''m pretty sure
that it''s not the intent of the exercise.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于从&lt; list&gt;复制元素到&lt; deque&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆