Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

STL

Posted on 2001-08-03
1
Medium Priority
?
365 Views
Last Modified: 2013-12-14
Why prefix is better than postfix in STL?
0
Comment
Question by:t55555
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
prashant_n_mhatre earned 900 total points
ID: 6348359
Consider a loop:

for(int count = 0; count < 10; count++)
{
}
 
This is fine when the variable are basic types, but I think this is a good habit to get out of when you use STL...Take the example of these two iterator loops:

std::list<Object> aList;

for(std::list<Object>::iterator iter = aList.begin(); iter != aList.end();
iter++)
{
}

for(std::list<Object>::iterator iter = aList.begin(); iter != aList.end();
++iter)
{
}
 
A subtle difference, but a big one... Let's see how the pre and postfix operators are defined in the Microsoft implementation...(I guess many people use this..), and it's fairly representative of how many implementations work. The first function is the postfix operator and the later is the prefix operator.

iterator operator++(int)
{
     iterator _Tmp = *this;
     ++*this;
     return (_Tmp);
}

iterator& operator++()
{
     _Ptr = _Acc::_Next(_Ptr);
     return (*this);
}
 
Because postfix operations must occur after any operations, it must make a copy of the object it's operation on first to preserve the original state of the object. This means the copy constructor gets invoked on the object, and this can be very expensive if something like a string copy is involved, and is generally a waste of valuable instructions. For that matter, it winds up calling the prefix operator anyway... The prefix operator doesn't have to worry about the data changing, so it's free to do what it wants, which is to just set the iterator to point to the next object in the list.

Hence the moral of the story is... "Unless you absolutely need to use postfix, get in the habit of using prefix."
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Introduction to Processes

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question