简体   繁体   中英

How do I see what element an iterator is pointing to in an vector with vscode using gdb?

I'm trying out VSCode writing some C++ using gdb, and I am finding that I need to determine what element an iterator is pointing at for a particular vector . Is there some way of doing this?

If you're trying to do this as part of your program, you can use STL:

int distance = std::distance(begin(s), result);

If you're trying to do this using the watch window in VSCode, the above will not work, as it's calling a function. Since the underlying structure of a vector is an array, you can use math instead. In the watch window, add:

(&(*myIterator) - &(myVector[0])) / sizeof(myVector[0])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM