Solved

STL

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This is an explanation of a simple data model to help parse a JSON feed
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

705 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

18 Experts available now in Live!

Get 1:1 Help Now