在 Visual Studio 调试中检查 STL 容器

发布于 2024-07-05 21:53:45 字数 139 浏览 5 评论 0原文

如果我有一个 std::vector 或 std::map 变量,并且我想查看其内容,那么在调试时查看第 n 个元素会很痛苦。 是否有插件或一些技巧可以让您在调试 (VS2003/2005/2008) 时更轻松地观察 STL 容器变量?

If I have a std::vector or std::map variable, and I want to see the contents, it's a big pain to see the nth element while debugging. Is there a plugin, or some trick to making it easier to watch STL container variables while debugging (VS2003/2005/2008)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

眼藏柔 2024-07-12 21:53:45

如果您想同时查看多个元素,可以附加一个逗号和元素数量,如下所示:

(v._Myfirst)[startIndex], count

但是,请注意,count 必须是常量,它不能是另一个表达式的结果。

If you want to watch more than one element at the same time, you can append a comma and the number of elements as so:

(v._Myfirst)[startIndex], count

However, note that count must be a constant, it cannot be the result of another expression.

戏剧牡丹亭 2024-07-12 21:53:45

For vectors, this thread on the msdn forums has a code snippet for setting a watch on a vector index that might help.

帅冕 2024-07-12 21:53:45

至少对我来说,Visual Studio 2008 在标准鼠标悬停内容框中显示 STL 容器的内容。

Visual Studio 2008, at least for me, displays the contents of STL containers in the standard mouseover contents box.

爱给你人给你 2024-07-12 21:53:45

您还可以右键单击手表中的任何值,然后选择“添加手表”。 如果您只需要查看地图或集合中的一个元素,这会很有用。

它还导致了 christopher_f 发布的向量解决方案 - ((v)._Myfirst)[index]

You can also right-click any value in your watch, and choose 'add watch'. This can be useful if you only need to look at one element of a map or set.

It also leads to the solution that christopher_f posted for vectors - ((v)._Myfirst)[index]

伴梦长久 2024-07-12 21:53:45

上面讨论的方法[((v)._Myfirst)[index]]仅适用于特定容器(std::vector),不适用于所有可能的STL容器。 例如,如果您想查看 std::deque 的内容,那么您必须寻找其他方法来浏览 std::deque 中的内容。

也许您可以尝试以下类似的设置来解决您的问题

[我仅针对随 Microsoft Visual studio 2010 Service pack 1 安装的 Visual Studio 2010 Professional 版本测试了此设置]

步骤 1:卸载 Microsoft Visual studio 2010 Service Pack 1 - 对于我的项目工作,我实际上并不需要 Service Pack 1,因此卸载 Service Pack 1 不会对我的案例造成任何问题。

步骤 2:重新启动系统。

步骤 3:如果您没有收到错误“LINK:致命错误 LNK1123:转换为 COFF 期间失败:文件无效或损坏”,则无需执行此步骤。 否则浏览

项目属性 -> 链接器(通用)-> 将启用增量链接更改为否 (/INCRMENTAL:NO)

Above discussed method [((v)._Myfirst)[index]] will work only for specific container(std::vector) not for all possible STL containers. For example if you want to see the content of std::deque then you have to look for some other method to browse through the content in std::deque.

Maybe you can try the following similar setting to solve your issue

[I tested this setting only for Visual Studio 2010 Professional version installed with Microsoft Visual studio 2010 Service pack 1]

Step 1: Uninstall the Microsoft Visual studio 2010 Service pack 1 - for my project work I don't really need the Service pack 1 so uninstalling service pack 1 will not cause any issue for my case.

Step 2: Restart your system.

Step 3: This step is not necessary if you are not getting Error 'LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt'. Otherwise browse through

Project Property -> Linker (General) -> Change Enable Incremental Linking to No (/INCREMENTAL:NO)

绝不服输 2024-07-12 21:53:45

在 vs 2015 中,我无法让这些工作
所以,我写了一些代码

1:我有长长元素向量的向量

std::vector<std::string> vs(M_coins + 1);
for (unsigned long long i = 0; i <= M_coins; i++) {
    std::for_each(memo[i].begin(), memo[i].end(), [i, &vs](long long &n) {
        vs[i].append(std::to_string(n));
        });
}
// now vs is ready for use as vs[0], vs[1].. so on, for your debugger

基本上我所做的就是将向量转换为字符串。
我有向量的向量,所以我有字符串向量来填充。

2:如果您只有长长元素的向量,则只需按如下方式进行转换:

std::vector<std::string> s;
std::for_each(v1.begin(), v1.end(), [&s](long long &n) {
    s.append(std::to_string(n));
    });
// now s is ready for use, for your debugger

希望它有所帮助。

In vs 2015, I could not get any of these working
so, i wrote a bit of code

1: I had vector of vector of long long elements

std::vector<std::string> vs(M_coins + 1);
for (unsigned long long i = 0; i <= M_coins; i++) {
    std::for_each(memo[i].begin(), memo[i].end(), [i, &vs](long long &n) {
        vs[i].append(std::to_string(n));
        });
}
// now vs is ready for use as vs[0], vs[1].. so on, for your debugger

basically what i did was converted vector into string.
i had vector of vector so i had string vector to fill.

2: if you have just a vector of long long elements, then just convert as below:

std::vector<std::string> s;
std::for_each(v1.begin(), v1.end(), [&s](long long &n) {
    s.append(std::to_string(n));
    });
// now s is ready for use, for your debugger

hope it helped.

誰ツ都不明白 2024-07-12 21:53:45

在VS2005和VS 2008中你可以看到STL容器的内容。 获取数据的规则位于 autoexp.dat“c:\Program Files\Microsoft Visual Studio 9\Common7\Packages\Debugger\autoexp.dat”中。

AutoExp.dat 旨在进行定制。 然而,STL 定义位于名为 [Visualizer] 的部分下。 如果您能弄清楚该部分中使用的语言,那么您就会更有能力,但是我建议您不要理会该部分。

Autoexp.dat 在 VS2003 中存在,但不支持 STL 容器([Visualizer] 不存在)。 在 VS2003 中,您必须手动导航底层数据表示。

通过修改 autoexp.dat 可以添加用于导航您自己类型的数据表示的规则,以便更容易调试。 如果你这样做,你应该只添加到[AutoExp]下的内容。 请小心并在修改此文件之前对其进行备份。

In VS2005 and VS 2008 you can see the contents of STL containers. The rules for getting at the data are in autoexp.dat "c:\Program Files\Microsoft Visual Studio 9\Common7\Packages\Debugger\autoexp.dat".

AutoExp.dat is meant to be customized. However, the STL defs are under a section called [Visualizer]. If you can figure out the language used in that section, more power to you, however I'd recommend just leaving that part alone.

Autoexp.dat existed in VS2003, but there was no support for STL containers ([Visualizer] didn't exist). In VS2003 you have to manually navigate the underlying data representation.

By modifying autoexp.dat it is possible to add rules for navigating the data representation of your own types so they are easier to debug. If you do this, you should only add to the stuff under [AutoExp]. Be careful and keep a back up of this file before you modify it.

美人如玉 2024-07-12 21:53:45

要在 Visual Studio 调试器中查看容器的第 n 个元素,请使用:

container.operator[](n)

To view the nth element of a container in the Visual Studio debugger, use:

container.operator[](n)
月亮坠入山谷 2024-07-12 21:53:45

您可以创建自定义可视化工具
看看这个:http://www.virtualdub.org/blog/pivot /entry.php?id=120

You could create a custom visualiser
Check this out: http://www.virtualdub.org/blog/pivot/entry.php?id=120

仲春光 2024-07-12 21:53:45

最简单的方法是您必须准备一个指针来监视这样的变量。

vector<int> a = { 0,1,2,3,4,5 };
int* ptr = &a[0]; // watch this ptr in VisualStudio Watch window like this "ptr,6".

我在VisualStudio2015中尝试了“a._Myfirst[0]”,但它没有显示数组数据。

如果您可以使用“natvis”,它将解决您的问题。

这是“sample.natvis”,用于显示 Visual studio 2015 的 std::vector 数据。

<?xml version="1.0" encoding="utf-8"?> 
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
  <Type Name="std::vector<*>">
    <DisplayString>{{ size={_Mypair._Myval2._Mylast - _Mypair._Myval2._Myfirst} }}</DisplayString>
    <Expand>
      <Item Name="[size]" ExcludeView="simple">_Mypair._Myval2._Mylast - _Mypair._Myval2._Myfirst</Item>
      <Item Name="[capacity]" ExcludeView="simple">_Mypair._Myval2._Myend - _Mypair._Myval2._Myfirst</Item>
      <ArrayItems>
        <Size>_Mypair._Myval2._Mylast - _Mypair._Myval2._Myfirst</Size>
        <ValuePointer>_Mypair._Myval2._Myfirst</ValuePointer>
      </ArrayItems>
    </Expand>
  </Type>
</AutoVisualizer>

之前
输入图片此处描述

之后
输入图片此处描述

Most simply method is you have to ready a pointer to watch variable like this.

vector<int> a = { 0,1,2,3,4,5 };
int* ptr = &a[0]; // watch this ptr in VisualStudio Watch window like this "ptr,6".

I tried "a._Myfirst[0]" in VisualStudio2015, But It wasn't display array data.

If you can use "natvis", it will resolve your problems.

This is "sample.natvis" for display std::vector data for Visual studio 2015.

<?xml version="1.0" encoding="utf-8"?> 
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
  <Type Name="std::vector<*>">
    <DisplayString>{{ size={_Mypair._Myval2._Mylast - _Mypair._Myval2._Myfirst} }}</DisplayString>
    <Expand>
      <Item Name="[size]" ExcludeView="simple">_Mypair._Myval2._Mylast - _Mypair._Myval2._Myfirst</Item>
      <Item Name="[capacity]" ExcludeView="simple">_Mypair._Myval2._Myend - _Mypair._Myval2._Myfirst</Item>
      <ArrayItems>
        <Size>_Mypair._Myval2._Mylast - _Mypair._Myval2._Myfirst</Size>
        <ValuePointer>_Mypair._Myval2._Myfirst</ValuePointer>
      </ArrayItems>
    </Expand>
  </Type>
</AutoVisualizer>

Before
enter image description here

After
enter image description here

会发光的星星闪亮亮i 2024-07-12 21:53:45

这是旧的,但由于我经常偶然发现这篇文章,因为它在 google 上仍然被引用很高,从 Visual Studio 2019 开始,人们可以简单地在调试器的 Watch 中写入:(

vectorName.data()

用你的变量名称替换 vectorName)来获取指向内容的指针。

然后,知道向量的当前大小,您可以告诉调试器向您显示前 N 个单元:

vectorName.data(),N

(N 是向量的大小)

如果像我一样,您有很多实际存储另一个数据的字节向量结构,您甚至可以告诉调试器将指针解释为其他内容的数组:

(float*)vectorName.data(),4

例如,我有一个 16 字节的 std::vector,使用它我可以告诉调试器向我显示 4 个浮点数的数组(这对我来说比单独的字节更有用)。

This is old but since I regularly stumble on this post because it is still referenced high on google, as of Visual Studio 2019, one can simply write in the debugger's Watch:

vectorName.data()

(replace vectorName by your variable name) to get a pointer to the content.

Then, knowing the current size of the vector, you can tell the debugger to show you the first N cells:

vectorName.data(),N

(N being the size of your vector)

And if like me, you have a lot of vectors of bytes that actually store another data structure, you can even tell the debugger to interpret the pointer as an array of something else :

(float*)vectorName.data(),4

For example, I have a std::vector of 16 bytes and using that I can tell the debugger to show me an array of 4 floats instead (which is more useful to me than the bytes alone).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文