Solved

STL

Posted on 2001-08-03
1
355 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 225 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb stateless example 2 43
Is there any way to convert exponential value to number in sql server 5 67
BATCH to EXE Converter 2 70
print bytes of an integer 6 25
A short article about problems I had with the new location API and permissions in Marshmallow
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

749 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