简体   繁体   English

如何转换我的堆排序程序以使用 C++ 标准容器(例如 std::vector)?

[英]How can I convert my heap sort program to use C++ standard containers (for example std::vector)?

I know how to code with C, however, this is my first time I try to use C++.我知道如何使用 C 进行编码,但是,这是我第一次尝试使用 C++。 And the use of VLAs(Variable Length Arrays) is not allowed in C++.并且在 C++ 中不允许使用 VLA(可变长度数组)。 So how can I convert this program to use C++ standard containers( for example std::vector) for the same instead of going the C route?那么如何转换这个程序以使用 C++ 标准容器(例如 std::vector)而不是走 C 路线?

Instead of int arr[n];而不是int arr[n]; in main() , use std::vector<int> arr(n);main()中,使用std::vector<int> arr(n); and what further changes I have to do?我还需要做哪些进一步的改变? Please assist.请协助。

Here is my code,这是我的代码,


#include<iostream>
 
using namespace std;
 
// A function to heapify the array.
void MaxHeapify(int a[], int i, int n)
{
    int j, temp;
    temp = a[i];
    j = 2*i;
 
    while (j <= n)
    {
        if (j < n && a[j+1] > a[j])
        j = j+1;
        // Break if parent value is already greater than child value.
        if (temp > a[j])
            break;
        // Switching value with the parent node if temp < a[j].
        else if (temp <= a[j])
        {
            a[j/2] = a[j];
            j = 2*j;
        }
    }
    a[j/2] = temp;
    return;
}
void HeapSort(int a[], int n)
{
    int i, temp;
    for (i = n; i >= 2; i--)
    {
        // Storing maximum value at the end.
        temp = a[i];
        a[i] = a[1];
        a[1] = temp;
        // Building max heap of remaining element.
        MaxHeapify(a, 1, i - 1);
    }
}
void Build_MaxHeap(int a[], int n)
{
    int i;
    for(i = n/2; i >= 1; i--)
        MaxHeapify(a, i, n);
}
int main()
{
    int n, i;
    cout<<"\nEnter the number of data element to be sorted: ";
    cin>>n;
    n++;
    int arr[n];
    for(i = 1; i < n; i++)
    {
        cout<<"Enter element "<<i<<": ";
        cin>>arr[i];
    }
    // Building max heap.
    Build_MaxHeap(arr, n-1);
    HeapSort(arr, n-1);
 
    // Printing the sorted data.
    cout<<"\nSorted Data ";
 
    for (i = 1; i < n; i++)
        cout<<"->"<<arr[i];
    cout<<"\nTime Complexity: Best case = Avg case = Worst case = O(n logn)";
 
    return 0;
} 

And the use of VLAs(Variable Length Arrays) is not allowed in C++并且在 C++ 中不允许使用 VLA(可变长度数组)

I've just compiled your code in C++ and it works perfectly fine.我刚刚在 C++ 中编译了你的代码,它工作得很好。 Feel free to rephrase if you think I misunderstood you.如果您认为我误解了您,请随时改写。 If you want to stick to arrays instead of std::vector you can use std::array .如果您想坚持使用 arrays 而不是std::vector ,则可以使用std::array Otherwise in your case it would be mostly swapping int[] for vector<int> with & in function parameters.否则,在您的情况下,它主要是在 function 参数中将int[] & vector<int>交换。

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

相关问题 C++,在 class 指针向量上使用 std::sort - 或者我无法正确命名我的函数 - C++, using std::sort on a vector of class pointers - OR I can't name my functions properly C++ 如何转换 std::vector<std::byte> 常量 uint8_t * - C++ how can I convert std::vector<std::byte> to const uint8_t * 使用c ++,我如何实现自己的容器类的itetator,以便我可以使用类似std :: sort() - using c++, how do I implement itetator to my own container class so i can use something like std::sort() C++:std::variant 可以保存向量、map 和其他容器吗? - C++: Can std::variant hold vector, map, and other containers? 为什么我不能在C ++中输入我的std :: vector - Why I can't input into my std::vector in C++ [C ++] [std :: sort]它如何在2D容器上工作? - [C++][std::sort] How does it work on 2D containers? 我可以使用std :: vector <std::vector<T> &gt;代表C ++中的二维数组? - Can I use std::vector<std::vector<T>> to represent two dimensional arrays in C++? 如何使动态数组或向量以与标准数组类似的速度运行? C ++ - How can I make my dynamic array or vector operate at a similar speed to a standard array? C++ C ++:向量容器和 <algorithm> std ::查找 - C++: vector containers and <algorithm> std::find 如何在C ++中使用std :: sort? - How do I use std::sort in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM