vscode-debug-visualizer icon indicating copy to clipboard operation
vscode-debug-visualizer copied to clipboard

Trying to get Visualizer to show up in C++

Open hoaug-tran opened this issue 1 year ago • 3 comments

I tried installing Vscode and installing the Debug Visualizer Extension. However I can't get it to work properly. I tried to get it to print out a sorted array using Quick Sort but it failed. Can someone help me? Thanks

#include <iostream>
#include <vector>
#include <string>

using namespace std;

void Swap(int &a, int &b)
{
    int tmp = a;
    a = b;
    b = tmp;
}

void QuickSort(vector<int> &a, int low, int high)
{
    if (low < high)
    {
        int pivot = a[high];
        int i = low - 1;

        for (int j = low; j < high; j++)
        {
            if (a[j] < pivot)
            {
                i++;
                Swap(a[i], a[j]);
            }
        }

        Swap(a[i + 1], a[high]);
        int pi = i + 1;

        
        cout << "Array after partition with pivot " << pivot << ": ";
        for (int k = 0; k < a.size(); k++)
        {
            cout << a[k] << " ";
        }
        cout << endl;

        QuickSort(a, low, pi - 1);
        QuickSort(a, pi + 1, high);
    }
}

int main()
{
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }

    cout << "Array before sorting: ";
    for (int i = 0; i < n; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;

    QuickSort(a, 0, n - 1);

    cout << "Array after sorting: ";
    for (int i = 0; i < n; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;

    return 0;
}

image

hoaug-tran avatar Oct 20 '24 21:10 hoaug-tran

I'm also having trouble getting the visualizer to actually display in contents of containers in c++.

Documentation says we should create a helper method, that converts the container contents to a json string, and this will be visualizable by debug visualizer, but I haven't had success there either.

demo/main.cpp

std::string myGraphJson = "{"kind":{"graph":true}," ""nodes":[{"id":"1"},{"id":"2"}]," ""edges":[{"from":"1","to":"2"}]}";

I have not had success on visualizing myGraphJson, myGraphJson.c_str(), etc.

If @hediet could help explain what we're doing wrong, that would be very helpful.

crispyrooster avatar Jan 20 '25 15:01 crispyrooster

I am having the same problem at my side with the cpp demo.

langyuly avatar Feb 03 '25 14:02 langyuly

It is probably because the option “-enable-pretty-printing” is not set in gdb.

Has the option been changed in the launch.json file? See example https://github.com/hediet/vscode-debug-visualizer/blob/master/demos/cpp/.vscode/launch.json

Image

See also C/C++ visualization #119

For an optimal view (e.g. grid), you should create a function that formats the data accordingly, see Visualization Playground.

Klummel69 avatar Feb 12 '25 22:02 Klummel69