我对C ++中的STL相当陌生,我尝试使用向量进行堆。没有得到所需的输出 [英] I am fairly new to STLs in C++ and i tried making a heap using vectors. Didnt get the desired output

查看:61
本文介绍了我对C ++中的STL相当陌生,我尝试使用向量进行堆。没有得到所需的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<bits/stdc++.h>
using namespace std;
class Heap
{
    vector <int> v;
    int length;
public:
    void create(vector <int> v, int s);
    void display();
};
void Heap::create(vector <int> v, int s)
{
    length=s+1;
    for(int i=1;i<=s;i++)
    {
        this->v[i]=v[i-1];
    }
    int temp;
    int j;
    for(int i=2;i<length;i++)
    {
        temp=v[i];
        j=i;
        while(j>1&&temp>v[j/2])
        {
            swap(v[j],v[j/2]);
            j=j/2;
        }
        if(j==1)
        {
            v[j]=temp;
        }
    }
}

void Heap::display()
{
    for(int i=1;i<length;i++)
    {
        cout<<v[i]<<"\t";
    }
    cout<<endl;
}

int main()
{
    vector <int> v;
    int ans=1;
    int d;
    while(ans==1)
    {
        cout<<"Enter the Data\n";
        cin>>d;
        v.push_back(d);
        cout<<"Do you want to enter more data?\n";
        cin>>ans;
    }
    cout<<endl;
    Heap h;
    h.create(v,((int)v.size()));
    h.display();
}

当我执行此代码时,它要求我输入数据值。我输入要输入的所有数据值,然后单击Enter按钮。它显示了细分错误。而且处决要花很多时间,这是非常不明智的。我使用代码块版本20。

When i execute this code, it asks me to enter the data value. i enter all the data values i want to enter and click the enter button. it shows segmentation error. also the execution is taking a lot of time which is very unusaul. i use codeblocks version 20.

推荐答案


当我执行此代码时,它要求我输入数据值。我输入了要输入的所有数据值,然后单击输入按钮

When i execute this code, it asks me to enter the data value. i enter all the data values i want to enter and click the enter button

是的,我对猜测要复制的内容不感兴趣你的问题。我也不希望猜测问题出在您的I / O代码还是正在测试的代码中。

Yeah, I'm not interested in guessing what you typed in order to reproduce your problem. I'm also not interested in guessing whether the issue is in your I/O code or the code you think you're testing.

始终删除交互式输入当您准备一个最小可复制示例,以便其他人可以实际复制它时。

Always remove interactive input when you're preparing a minimal reproducible example so that other people can actually reproduce it.

有时删除交互式输入可能会解决您的问题,在这种情况下,您已经学到了一些重要的知识(并且可能想问一个有关您输入代码的问题)。

Sometimes removing the interactive input may fix your problem, in which case you've learnt something important (and probably want to ask a different question about your input code).


它显示分段错误

it shows segmentation error

分段错误会在程序发生的确切点中断程序。如果您在调试器中运行程序,它将向您显示此位置以及发生时程序中所有内容的状态。您应该尝试一下,并学习使用调试器。

A segmentation fault interrupts your program at the exact point where it happens. If you run your program in a debugger, it will show you where this is, and the state of everything in your program when it happened. You should try this, and learn to use your debugger.

        this->v[i]=v[i-1];

在另一个答案中正确指出,此行有一个错误。
读取输入时,您正确地调用了 push_back ,因此您可以在此处执行相同的操作。或者,您需要在索引不存在的元素之前显式设置 this-> v 的大小。

As correctly pointed out in the other answer, there is a bug on this line. You correctly called push_back when reading input, so you could just do the same here. Alternatively you need to explicitly size this->v before indexing elements that don't exist.

与此相关的另一个主要问题功能是将 this-> v (非法,仅在上一行中仅使用一次)和 v 是 main v 本地副本,并且从

The other main problem with this function is that it mixes up this->v (used, illegally, only once on the line above) and v which is a local copy of the v in main, and which goes out of scope and is lost forever at the end of the function.

只需为变量指定不同的名称,这样就不必编写 this-&v; v 在您当前引用 v 的所有其他行上。另外,考虑通过const ref传递原始的 v 而不是进行复制。

Just give your variables different names so you don't have to write this->v on all the other lines where you currently refer to v. Also, consider passing the original v by const ref instead of making a copy.

NB。我确实了解并了解到,您正在故意将基于索引的索引转换为基于1的索引。如果由于某种原因您不能仅使用 std :: sort std :: make_heap ,则至少可以明确地将第零个元素设置为零,然后将其余元素复制。

NB. I do see and understand that you're deliberately switching to 1-based indexing for the sort. If for some reason you can't just use std::sort or std::make_heap, you could at least explicitly set the zeroth element to zero, and then just std::copy the rest.

最后, Heap :: create 看起来确实应该只是一个构造函数。通常,强制两阶段初始化的样式很差,我在这里看不到任何原因。

Finally, Heap::create really looks like it should just be a constructor. Forcing two-phase initialization is poor style in general, and I don't see any reason for it here.

这篇关于我对C ++中的STL相当陌生,我尝试使用向量进行堆。没有得到所需的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆