C++ help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 103
Reputation: Tauren is an unknown quantity at this point 
Solved Threads: 0
Tauren Tauren is offline Offline
Junior Poster

C++ help

 
0
  #1
Apr 10th, 2007
  1. cout << "\nYou trade your sword for a battle axe.";
  2. inventory[0] = "battle axe";
  3. cout << "\nYour items:\n";
  4. for (int i = 0; i < numItems; ++i)
  5. cout << inventory[i] << endl;
Uhh well the question is that I really dont get the ++i Why would you use the pre fix instead of the post fix, I know how they work its just doesnt really make sense
Last edited by WaltP; Apr 11th, 2007 at 3:47 am. Reason: Added CODE tags -- you actually typed right over what they are when you entered this post...
A winner is a loser that got up and gave it one more shot!

A programmer is a failer that edited the code and got it work!
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: C++ help

 
0
  #2
Apr 10th, 2007
first, please post with code tags...


second, it's really use choice... it just increments the variable 'i' before it's accessed. may as well stick to "i++" imo.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: C++ help

 
0
  #3
Apr 10th, 2007
Some people have argued that ++i yields more efficient assembly code. When I checked, they were the same. In this situation, it doesn't matter.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 103
Reputation: Tauren is an unknown quantity at this point 
Solved Threads: 0
Tauren Tauren is offline Offline
Junior Poster

Re: C++ help

 
0
  #4
Apr 10th, 2007
Ur right, If you had a server and maybe the data base which holds the items you wouldnt want the server holding the item before you got it I guess I will always just use i++
A winner is a loser that got up and gave it one more shot!

A programmer is a failer that edited the code and got it work!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 103
Reputation: Tauren is an unknown quantity at this point 
Solved Threads: 0
Tauren Tauren is offline Offline
Junior Poster

Re: C++ help

 
0
  #5
Apr 10th, 2007
I just dont fer what the statments do? whats the diffrence?

/code for (int i = 0; i < numItems; ++i)
what does that do like? lol explain
A winner is a loser that got up and gave it one more shot!

A programmer is a failer that edited the code and got it work!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 103
Reputation: Tauren is an unknown quantity at this point 
Solved Threads: 0
Tauren Tauren is offline Offline
Junior Poster

Re: C++ help

 
0
  #6
Apr 10th, 2007
i++ Would be increased before code is done
++i is increased before code is done
A winner is a loser that got up and gave it one more shot!

A programmer is a failer that edited the code and got it work!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: C++ help

 
0
  #7
Apr 10th, 2007
Originally Posted by Infarction View Post
Some people have argued that ++i yields more efficient assembly code. When I checked, they were the same. In this situation, it doesn't matter.
in this specific case it really does not matter. When applied to primitives such
as int or char, they are indistinguishable in terms of efficiency. When applied
to objects of a user defined type the difference can be significant; the prefix
version avoids the creation of an anonymous temporary.

try this code:
  1. int main()
  2. {
  3. struct A
  4. {
  5. A( int ii ) : i(ii) { cout << "A::constructor\n" ; }
  6. A(const A& that ) : i(that.i) { cout << "A::copy_constructor\n" ; }
  7. ~A() { cout << "A::destructor\n" ; }
  8. A& operator++() // prefix
  9. { ++i ; return *this ; }
  10. A operator++(int) // postfix
  11. { A temp(*this); ++i ; return temp ; }
  12. operator int() const { return i ; }
  13. int i ;
  14. };
  15. cout << "------------- start -----------------------------\n" ;
  16. A a1 = 100, a2 = 100 ;
  17. cout << "a1: " << a1 << "\ta2: " << a2 << '\n' ;
  18. cout << "------------- prefix++ ------------------------\n" ;
  19. int i = ++a1 ;
  20. cout << "a1: " << a1 << "\ti: " << i << '\n' ;
  21. cout << "------------- postfix++ -----------------------\n" ;
  22. i = a2++ ;
  23. cout << "a2: " << a2 << "\ti: " << i << '\n' ;
  24. cout << "------------- done -----------------------------\n" ;
  25. };
and here is the output:
------------- start -----------------------------
A::constructor
A::constructor
a1: 100 a2: 100
------------- prefix++ ------------------------
a1: 101 i: 101
------------- postfix++ -----------------------
A::copy_constructor
A::destructor
a2: 101 i: 100
------------- done -----------------------------
A::destructor
A::destructor


we can really see the difference now between ++a1 and a2++, both
in terms of the difference in semantics as well as performance.

if you are into a lot of C++ programming, it is a good idea to get
into the habit of using the prefix versions of ++ and -- as the normal
case. even for builtin types like int, the prefix version is as efficient
as the postfix version.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: C++ help

 
0
  #8
Apr 11th, 2007
Originally Posted by Tauren View Post
Ur right, If you had a server and maybe the data base which holds the items you wouldnt want the server holding the item before you got it I guess I will always just use i++
Originally Posted by Tauren View Post
I just dont fer what the statments do? whats the diffrence?

/code for (int i = 0; i < numItems; ++i)
what does that do like? lol explain
Huh? Try using the PREVIEW button and proofreading your posts please. And please read this, too.


Try this program for illustration:
  1. int main()
  2. {
  3. int i = 5;
  4.  
  5. cout << " i = " << i << endl;
  6. cout << "prefix i = " << ++i << endl;
  7. cout << "now i = " << i << endl;
  8. cout << "postfix i = " << i++ << endl;
  9. cout << "now i = " << i << endl;
  10. return 0;
  11. }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC