473,411 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

vector, get index from iterator

Seems odd, but I was wondering if I am in the middle of iterating
through a vector, is there a way to calc which index I am currently
on? something like: it - vec.begin()?

I don't want to change the loop to iterate through using indexes
instead of iterators, I just need the index for a little debug output,
the rest of the code in the loop uses the iterator.

thanks.
Nov 21 '07 #1
2 9762
Christopher wrote:
Seems odd, but I was wondering if I am in the middle of iterating
through a vector, is there a way to calc which index I am currently
on? something like: it - vec.begin()?
Absolutely! Since vector's iterators are of 'random access' kind,
the binary minus is defined for them to give the distance. For
all others you can use 'std::distance'.
I don't want to change the loop to iterate through using indexes
instead of iterators, I just need the index for a little debug output,
the rest of the code in the loop uses the iterator.
No need to justify yourself :-)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 21 '07 #2
Christopher <cp***@austin.rr.comwrote in news:c6f679ff-5c3d-4af2-ac81-
18**********@f3g2000hsg.googlegroups.com:
Seems odd, but I was wondering if I am in the middle of iterating
through a vector, is there a way to calc which index I am currently
on? something like: it - vec.begin()?
That would work for vector... but a more general method would be:

std::distance(vec.begin(), it);

I don't want to change the loop to iterate through using indexes
instead of iterators, I just need the index for a little debug output,
the rest of the code in the loop uses the iterator.
Or, maintain the index as you are iterating through:

for (it = vec.begin(), i = 0; it != vec.end(); ++it, ++i) {
}

Nov 21 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
6
by: Joe | last post by:
I have a: vector<string> which contains a few dozen elements. I want to find the index of the element containing a certain string. for example: vector<string> strings;...
5
by: streamkid | last post by:
i have a class table, which has a vector of records(-db). i 'm trying to remove an element, but it doesn't seem to work.. i read this http://www.cppreference.com/cppvector/erase.html] and that's...
5
by: Alan | last post by:
I was wondering whether it is good programming practice or asking for trouble to modify a vector while iterating through it. That is, I want to do something like the following pseudocode in C++: ...
1
by: toton | last post by:
Hi, I am using vector and similar containers extensively for my project, as well as boost circular_buffer_space_optimized (and adobe circular_queue which is built over vector ). They all work with...
10
by: StephQ | last post by:
I found that old post: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/3a2562c9a5f8998/15519204726d01e8?lnk=gst&q=vector+no+surprise&rnum=2#15519204726d01e8 I just erased the...
3
by: chsalvia | last post by:
I have a question about the design of STL vector. One thing I wonder was why the STL designers chose to have the insert() and erase() functions take an iterator as the first argument, rather than...
3
by: JackC | last post by:
Hi, I am trying to find if its at all possible to create a pointer to an object inside a vector, based upon a vector iterator that will remain a valid pointer once the iterator is invalid. ...
5
by: Boltar | last post by:
Hi Is there a way of inserting and erasing to/from a vector using an array index instead of an iterator or alternatively a way to map an index number to an iterator? eg: vector<intv;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.