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<intil ist;
/* copy elements from std::cin into ilist */
std::copy( (std::istream_i terator<int>( std::cin )),
(std::istream_i terator<int>()) ,
std::back_inser ter( ilist ) );
std::deque<intd eque_of_evens;
std::deque<intd eque_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.p ush_back( *iter );
}
}
std::cout << "\n Printing Deque of Even Integers: ";
std::copy( deque_of_evens. begin(),
deque_of_evens. end(),
std::ostream_it erator<int>( std::cout, " " ) );
std::cout << "\n\n Printing Deque of Odd Integers: ";
std::copy( deque_of_odds.b egin(),
deque_of_odds.e nd(),
std::ostream_it erator<int>( std::cout, " " ) );
std::cout << std::endl;
return 0;
}
-- http://lispmachine.wordpress.com 12 2650
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<intil ist;
/* copy elements from std::cin into ilist */
std::copy( (std::istream_i terator<int>( std::cin )),
(std::istream_i terator<int>()) ,
std::back_inser ter( ilist ) );
std::deque<intd eque_of_evens;
std::deque<intd eque_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.p ush_back( *iter );
}
}
std::cout << "\n Printing Deque of Even Integers: ";
std::copy( deque_of_evens. begin(),
deque_of_evens. end(),
std::ostream_it erator<int>( std::cout, " " ) );
std::cout << "\n\n Printing Deque of Odd Integers: ";
std::copy( deque_of_odds.b egin(),
deque_of_odds.e nd(),
std::ostream_it erator<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<intil ist; /* copy elements from std::cin into ilist */ std::copy( (std::istream_i terator<int>( std::cin )), (std::istream_ iterator<int>() ), std::back_inse rter( ilist ) );
std::deque<intd eque_of_evens; std::deque<intd eque_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_i terator<int>( std::cout, " " ) );
std::cout << "\n\n Printing Deque of Odd Integers: "; std::copy( deque_of_odds.b egin(), deque_of_odds. end(), std::ostream_i terator<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<intil ist;
/* copy elements from std::cin into ilist */
std::copy( (std::istream_i terator<int>( std::cin )),
(std::istream_i terator<int>()) ,
std::back_inser ter( ilist ) );
std::deque<intd eque_of_evens;
std::deque<intd eque_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.p ush_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_it erator<int>( std::cout, " " ) );
std::cout << "\n\n Printing Deque of Odd Integers: ";
std::copy( deque_of_odds.b egin(),
deque_of_odds.e nd(),
std::ostream_it erator<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_i terator, e.g.:
std::deque< int evens(
boost::make_fil ter_iterator< IsEven >( ilist.begin(),
ilist.end() ),
boost::make_fil ter_iterator< IsEven >( ilist.end(),
ilist.end() ) ) ;
std::deque< int odds(
boost::make_fil ter_iterator< IsOdd >( ilist.begin(),
ilist.end() ),
boost::make_fil ter_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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On Sep 25, 2:56 pm, James Kanze <james.ka...@gm ail.comwrote:
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.
that's nice James (as for as my exercise is concerned ). I have
used that construct, it make my solution look clean :-)
James Kanze wrote:
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<intil ist; /* copy elements from std::cin into ilist */ std::copy( (std::istream_i terator<int>( std::cin )), (std::istream_ iterator<int>() ), std::back_inse rter( ilist ) );
> std::deque<intd eque_of_evens; std::deque<intd eque_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_i terator<int>( std::cout, " " ) );
> std::cout << "\n\n Printing Deque of Odd Integers: "; std::copy( deque_of_odds.b egin(), deque_of_odds. end(), std::ostream_i terator<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.
I have a great deal of sympathy for that sentiment. Functors tend to move
code from the place where you would like to see it to some other place.
That is generally not so good.
For a more experienced programmer, I might consider a
boost::filter_i terator, e.g.:
std::deque< int evens(
boost::make_fil ter_iterator< IsEven >( ilist.begin(),
ilist.end() ),
boost::make_fil ter_iterator< IsEven >( ilist.end(),
ilist.end() ) ) ;
std::deque< int odds(
boost::make_fil ter_iterator< IsOdd >( ilist.begin(),
ilist.end() ),
boost::make_fil ter_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.
[snip]
In a case like this, I like lambda:
std::remove_cop y_if( ilist.begin(), ilist.end(),
std::back_inser ter( deque_of_evens ),
_1 % 2 != 0 );
std::remove_cop y_if( ilist.begin(), ilist.end(),
std::back_inser ter( deque_of_odds ),
_1 % 2 == 0 );
(yet, I would prefer copy_if :-)
I agree, though, that cases where lambda is this concise are rare. But
compared to using std::binder..., std::modulus, ... lambda is great.
Best
Kai-Uwe Bux
On Sep 25, 3:23 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
In a case like this, I like lambda:
std::remove_cop y_if( ilist.begin(), ilist.end(),
std::back_inser ter( deque_of_evens ),
_1 % 2 != 0 );
std::remove_cop y_if( ilist.begin(), ilist.end(),
std::back_inser ter( deque_of_odds ),
_1 % 2 == 0 );
ok, that's nice :)
and i did not get that _1 %2, I know it checks for even and odd
numbers but what exactly that "underscore 1" means: _1 %2
arnuld wrote:
>On Sep 25, 3:23 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
>In a case like this, I like lambda:
std::remove_cop y_if( ilist.begin(), ilist.end(), std::back_inser ter( deque_of_evens ), _1 % 2 != 0 );
std::remove_cop y_if( ilist.begin(), ilist.end(), std::back_inser ter( deque_of_odds ), _1 % 2 == 0 );
ok, that's nice :)
and i did not get that _1 %2, I know it checks for even and odd
numbers but what exactly that "underscore 1" means: _1 %2
That's the magic of expression templates. If you are interested, you can
have a look at my posting http://groups.google.com/group/comp....422ea883a2b151
where I explain how one can about implementing something like that. Be
warned, however: it takes about 600 lines of code (and 300 lines of
comments) to make that _1 work (and that is just a proof of concept!).
Luckily, it already has been done and is in boost.
Best
Kai-Uwe Bux
On Sep 25, 2:56 pm, James Kanze <james.ka...@gm ail.comwrote:
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_i terator, e.g.:
std::deque< int evens(
boost::make_fil ter_iterator< IsEven >( ilist.begin(),
ilist.end() ),
boost::make_fil ter_iterator< IsEven >( ilist.end(),
ilist.end() ) ) ;
std::deque< int odds(
boost::make_fil ter_iterator< IsOdd >( ilist.begin(),
ilist.end() ),
boost::make_fil ter_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.
I am trying this but can not copile my program with that without
errors. I am using remove_copy_if as suggested by Kai-Uwe Bux
(remember the _1 ;-)
but still I am interested in this use of boost library. I have copied
the code you wrote but then I am not able to get what to do to
make use of this make_filter_ite rator.
any explanation ?
On Sep 25, 12:23 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
James Kanze wrote:
[...]
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.
I have a great deal of sympathy for that sentiment. Functors
tend to move code from the place where you would like to see
it to some other place. That is generally not so good.
It depends. If you can supply a good, explicit name for the
function, and it is a "pure" function, not depending on any
local variables, it's not that bad. If you can reasonably
expect to use the function elsewhere, it's even good. I'd say
that his case meets the first criteron, but not really the
second. So it's not too bad, but I still prefer the test in an
explicit loop. (There's also the question of how much you're
throwing at the OP at a time. He IS working his way through a
tutorial text, and shouldn't be expected to handle everything at
once. Each thing in its time.)
For a more experienced programmer, I might consider a
boost::filter_i terator, e.g.:
std::deque< int evens(
boost::make_fil ter_iterator< IsEven >( ilist.begin(),
ilist.end() ),
boost::make_fil ter_iterator< IsEven >( ilist.end(),
ilist.end() ) ) ;
std::deque< int odds(
boost::make_fil ter_iterator< IsOdd >( ilist.begin(),
ilist.end() ),
boost::make_fil ter_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.
[snip]
In a case like this, I like lambda:
std::remove_cop y_if( ilist.begin(), ilist.end(),
std::back_inser ter( deque_of_evens ),
_1 % 2 != 0 );
std::remove_cop y_if( ilist.begin(), ilist.end(),
std::back_inser ter( deque_of_odds ),
_1 % 2 == 0 );
(yet, I would prefer copy_if :-)
I agree, though, that cases where lambda is this concise are
rare. But compared to using std::binder..., std::modulus, ...
lambda is great.
If we had a real lambda, it would be great (although as you
point out, in this particular case, boost::lambda works like a
real lambda). Ideally, even, we'd have a lambda which resolved
to a class which could be used to instantiate a template like
boost::make_fil ter_iterator, so I could replace IsEven and IsOdd
in my example.
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dave |
last post by:
Hello all,
I'm pondering why the default underlying container for std::priority_queue<>
is std::vector<>. It would seem that inserts are liable to happen anywhere,
which would make std::list<> a superior alternative. Why factors am I not
considering here? Why in the general case would std::vector<> be best?
Thanks,
Dave
|
by: Charles L |
last post by:
I have read that the inclusion of <fstream.h> makes the inclusion of
<iostream.h> unnecessary.
Is this correct?
Charles L
|
by: Mikhail N. Kupchik |
last post by:
Hi All.
I have a question regarding C++ programming language standard. It
is related to standard library, not to the core language.
Is it portable to instantiate template class std::list<> with
incomplete type? I've seen some STL implementations which allow this
and some others that does not. I did not find any mentioning of this
topic in the standard, maybe I searched not enough thoroughly?
|
by: Kenneth |
last post by:
<list> seems to be a powerful structure to store the related nodes in
memory for fast operations, but the examples I found are all related to
primitive type storage.
I'm doing a project on C++ with my defined classes to be added to
linked list structure so as to facilitate the operation of all
instances of defined classes. Is that possible to apply such classes to
<list> or <Vector> structure?
Thanks!
|
by: Chris Dams |
last post by:
Dear all,
The following is accepted by the compiler
template <class Tclass test
{ public:
typedef vector<int>::reference rt;
};
but after the change int -T, obtaining the code fragment,
| |
by: janzon |
last post by:
Hi!
Sorry for the bad subject line... Here's what I mean. Suppose we deal
with C++ standard integers lists (the type is indifferent). We have a
function f, declared as
list<intf(int);
Now we have an integer list p, say. For each element x in p, we want to
repace x with f(x) to get a new, possibly larger, integer list. Note
|
by: T. Crane |
last post by:
Hi,
I'm struggling with how to initialize a vector<vector<double>>
object. I'm pulling data out of a file and storing it in the
vector<vector<double>object. Because any given file will have a
large amount of data, that I read off using an ifstream object, I
don't want to use the push_back method because this grows the
vector<vector<double>dynamically, and that will kill my execution
time. So, I want to reserve space first, using, of...
|
by: subramanian100in |
last post by:
There are some operations like sort, remove, remove_if which are
available as list<Tmember functions; but vector<T>, deque<Tdo not
seem have those functions. If am correct in this, what is the reason
for that. Moreover sort, remove, remove_if are available in
<algorithmalso. Then why does list<Thave these member operations ?
Kindly clarify.
Thanks
V.Subramanian
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |