vector

Hi . i want cout vector form end to begin

for ( int i = v.size(); i >= 0 ; i--)

cout << v[i] << endl ;

this way is true ?
Array indexes start at zero your loop must start at int i = v.size()-1
1
2
for ( int i = v.size()-1; i >= 0 ; i--) // notice -1
cout << v[i] << endl ; 


another way is to use iterators:
1
2
3
for(vector<int>::iterator i=v.end()-1;i>=v.begin();i--)
cout << *i << endl ;

http://www.cplusplus.com/reference/stl/vector/begin/
http://www.cplusplus.com/reference/stl/vector/end/


Topic archived. No new replies allowed.