简体   繁体   English

如何使用 gdb 在带有 vscode 的向量中查看迭代器指向的元素?

[英]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 .我正在尝试使用 gdb 编写一些 C++ 的 VSCode,并且我发现我需要确定iterator针对特定vector指向的元素。 Is there some way of doing this?有没有办法做到这一点?

If you're trying to do this as part of your program, you can use STL:如果您尝试将此作为程序的一部分,您可以使用 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.如果您尝试使用 VSCode 中的监视窗口执行此操作,则上述操作将不起作用,因为它正在调用一个函数。 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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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