pointers - vector iterators c++ -


The way I work like the start and end, it seems a little bit incompatible to me. When they move forward and backward, they have different behaviors

  vector & lt; Actor * & gt; a; A.push_back (new actor (11)); A.push_back (new actor (22)); A.push_back (new actor (33)); Vector & lt; Actor * & gt; :: iterator = a.begin (); Int x = 0; While (a.begin () + x! = A.end ()) {cout & lt; & Lt; (* (A.begin (+) + x)) & lt; & Lt; "\ N"; X ++; } Cout & lt; & Lt; "\ N"; Int y = 1; // If it is set to 0, then its a mistake = / when I use {a.end () - y! = A.begin ()) {cout & lt; & Lt; (* (A.end (- - y)) & lt; & Lt; "\ N"; Y ++; }   

output

  0x979a008 0x979a028 0x979a018 0 9 00 0018 0x979a028   

How do I get the expected pattern I

  0x979a008 0x979a028 0x979a018 0x979a018 0x979a028 0x979a008    

Note that starts () indicates the first element of the vector, but end () point the last element past . It is never safe to dereference end () , but you can compare iterators to it.

If vector is empty, then start () == end () <

Another idiotic method of loop is on the elements of a vector: / P>

 For  (Vector :: Iterator i = a.begin (); i! = A.end (); ++ i) {// Here are some To do this again in reverse, it is easy to use  rbegin ()  and  rend () , which is very Works by the way and  start ()  /  end () , but iterate in reverse order:  
 for  (vector & Lt; actor *>: Reverse_iterator i = a.rbegin (); i! = A.rend (); ++ i) {  

Also, if you want to modify the elements If you do not want intentions, then you have to const_iterator (or const_reverse_iterator instead.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -