Skip to main content
GameDev.net gamedev.net
🔒 Locked

push_back,push_front,pop_front,pop_back

Started by DaveE9th Jul 12, 2003 at 1:33 PM 8 replies 800+ views
Original Post
DaveE9th
DaveE9th
I'm having a difficult time understanding how these functions work on linked lists, deques and vectors. I was hoping someone would look at the code below for me. Although pop_front and pop_back are not implemented below, I would like to know how they work as well. I realize that these 4 functions are involved in the inserting and deleting of data from a list, but I don't understand the process. I'd appreciate any help. Thanks, Dave
// list.cpp

#include <list>
#include <iostream>

using namespace std;

void OutputList(list<int> output)
{
   list<int>::iterator pos;

   for(pos = output.begin(); pos != output.end(); ++pos)
      cout << *pos << " ";
   cout << endl << endl;
}

int main()
{
   list<int> mylist;
   list<int>::iterator pos;

   for(int a = 0; a <= 10; a++)
   {
      mylist.push_back(a * 2);
      if(a) mylist.push_front(a * -2);
   }

   cout << "Even numbers from -20 to 20" << endl;
   OutputList(mylist);

VC++ 6.0
Trienco
Trienco
except pop returns void, which i find a little annoying but making sense as sometimes you just want to remove the element and dont care what it is.
f@dzhttp://festini.device-zero.de
DaveE9th
DaveE9th
Thanks for the help,

Dave
VC++ 6.0
Fruny
Fruny
quote:
Original post by Trienco
except pop returns void, which i find a little annoying but making sense as sometimes you just want to remove the element and dont care what it is.


pop() return void to avoid problems (messing up the internal structure of the container) if an exception is thrown during copy-construction of the return value.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
DaveE9th
DaveE9th
That''s a bit over my head but I''m learning little by little. I ordered a C++ Standard Libary Book, I hope it''ll help me. The topics of containers, lists and binary sorting are challenging. I''m eager to know of any recommended reading on the subject.

Thanks again, Dave
VC++ 6.0
Qw3r7yU10p!
Qw3r7yU10p!
It's the 'one reponsisbility rule'. for example front() or back() will query the value giving you a reference to the stored value (i.e. a copy doesn't necessarily have to be made). pop_front() or pop_back() will remove it.

If pop_front() and front() were combined so that the value was returned, a copy would have to be made (because it wouldn't exist in the container anymore). So when the copy is made there is a possibility that the copy constructor of the class will throw an exception. If that happens you'll have lost the value because it will have already been 'popped'.

It is a good rule of thumb to give functions one responsibility. And often classes too. If a function or a class is taking care of too many things it may be a good idea to split them up. You'll find the classes and functions will have names which represent what they do more clearly and also the side effect that you'll have less commments (the class and function names will be the documentation).

edit: The usual STL documentation cited around here is sgi's. If it's a bit overwhelming, each section has an example which will help make things clearer.

[edited by - petewood on July 14, 2003 4:14:05 AM]
Fruny
Fruny
quote:
Original post by DaveE9th
That''s a bit over my head but I''m learning little by little. I ordered a C++ Standard Libary Book, I hope it''ll help me. The topics of containers, lists and binary sorting are challenging. I''m eager to know of any recommended reading on the subject.



The best book I know on the C++ standard library is "The C++ Standard Library, a tutorial and reference" by N. Josuttis. It covers more than just the STL.

"Generic Programming and the STL:
Using and Extending the C++ Standard Template Library
" by M. Austern offers a different approach of the STL,

For binary sorting, consult an algorithms book, such as "Introduction to algorithms" by Cormen/Leiserson/Rivest/Stein

More C++ books on programming, the STL, templates and iostreams.

See also the links in my sig.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
DaveE9th
DaveE9th
Wow! Thanks, I''ll pick up a few of those books. I''ve already ordered the C++ Windows STL book, I''m waiting for it. I''ll look into the other books as well.

Dave
VC++ 6.0

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.