| | |
C++ help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2006
Posts: 103
Reputation:
Solved Threads: 0
c Syntax (Toggle Plain Text)
cout << "\nYou trade your sword for a battle axe."; inventory[0] = "battle axe"; cout << "\nYour items:\n"; for (int i = 0; i < numItems; ++i) cout << inventory[i] << endl;
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!
A programmer is a failer that edited the code and got it work!
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
•
•
•
•
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.
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:
C++ Syntax (Toggle Plain Text)
int main() { struct A { A( int ii ) : i(ii) { cout << "A::constructor\n" ; } A(const A& that ) : i(that.i) { cout << "A::copy_constructor\n" ; } ~A() { cout << "A::destructor\n" ; } A& operator++() // prefix { ++i ; return *this ; } A operator++(int) // postfix { A temp(*this); ++i ; return temp ; } operator int() const { return i ; } int i ; }; cout << "------------- start -----------------------------\n" ; A a1 = 100, a2 = 100 ; cout << "a1: " << a1 << "\ta2: " << a2 << '\n' ; cout << "------------- prefix++ ------------------------\n" ; int i = ++a1 ; cout << "a1: " << a1 << "\ti: " << i << '\n' ; cout << "------------- postfix++ -----------------------\n" ; i = a2++ ; cout << "a2: " << a2 << "\ti: " << i << '\n' ; cout << "------------- done -----------------------------\n" ; };
------------- 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.
•
•
•
•
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++
•
•
•
•
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
Try this program for illustration:
c Syntax (Toggle Plain Text)
int main() { int i = 5; cout << " i = " << i << endl; cout << "prefix i = " << ++i << endl; cout << "now i = " << i << endl; cout << "postfix i = " << i++ << endl; cout << "now i = " << i << endl; return 0; }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Other Threads in the C++ Forum
- Previous Thread: Prime_NUMBER
- Next Thread: urgent help
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






