GPWiki.org
GPWiki.org
It is currently Sun May 19, 2013 10:17 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Oct 31, 2012 7:32 am 
Rookie

Joined: Wed Oct 31, 2012 7:27 am
Posts: 3
I have a class that has a vector<int> and I need to keep track of where in the vector array i am,
instead of making another variable called say int currentElement, which will keep track of where in the array i am i was wondering (and have looked but can't see) if vector class keeps it's own such variable.

Anyhelp greatly appreciated.


Top
 Profile  
 
PostPosted: Wed Oct 31, 2012 2:06 pm 
Double Guru
User avatar

Joined: Fri Aug 12, 2005 8:58 am
Posts: 2009
Location: LA, CA
In these cases I usually just use a standard for loop instead of iterators.

_________________
My Development Blog | My Website | My Current 3d Engine


Top
 Profile  
 
PostPosted: Thu Nov 01, 2012 9:37 am 
Bit Baby

Joined: Sat Oct 06, 2012 4:53 am
Posts: 8
Not exactly as you described, but like other standard library containers you can use iterators (alternative explanation) to iterate over the contents.

Note that if your compiler supports it C++11 added "range based for statements" (or "for each") syntax which allows for much cleaner and more readable code.


Is there some reason you need to store your position, or can iterate over all (or a subset) of the vector contents.

Hope that helps! :)

_________________
- Jason Astle-Adams
From my blog: What next? Intermediate to advanced C++ | 20 ways to advertise your game | 4 reasons you aren't a successful indie developer


Top
 Profile  
 
PostPosted: Sat Nov 17, 2012 3:25 pm 
Rookie

Joined: Wed Oct 31, 2012 7:27 am
Posts: 3
To answer your question jbadams...sry for late reply,
Ye the reason I wanted to keep track of my current position in the vector was because
the vector(2 vectors actually) holds the x and y positions on the map for an armys route from point a to b,
since the programs realtime and each army is processed every second I thought of keeping track which x'y they were at.
As it turned out I just delete the beginning (current) element every time they move so in the end I dont need to keep track of where in the vector they are since there always at vector.begin().

Your link is useful though and I thank you for your post.


Top
 Profile  
 
PostPosted: Sat Nov 17, 2012 3:37 pm 
Bytewise

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 276
Location: Here (where else?)
Why not make an XY class holding both the x and the y, and have a "std::vector<XY> army_route" instead?

_________________
My project: Messing about in FreeRCT, dev blog, and IRC #freerct at oftc.net


Top
 Profile  
 
PostPosted: Sat Nov 17, 2012 3:43 pm 
Rookie

Joined: Wed Oct 31, 2012 7:27 am
Posts: 3
Well...I'm not sure why I'm not doing this,it's certainly going to make for cleaner code.
When I have a moment of "relaxed programming with a cup of tea" instead of "rushed programming with a jug of coffee" I should probably do this.
THX for the input :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group