Solved

STL

Posted on 2001-08-03
1
353 Views
Last Modified: 2013-12-14
Why prefix is better than postfix in STL?
0
Comment
Question by:t55555
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CKEditor - will not function 7 61
Export Table to CSV - Access to CSV - using python 18 99
How can i compile this github project?? 2 74
C# Error - Add Failed 12 78
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 …
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 new code templates in NetBeans IDE 8.0 for Windows.

911 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now