排序算法不适用于使用指针操作的模板

Sort algorithm is not working with my template manipulated with pointers

本文关键字:操作 指针 算法 不适用 适用于 排序      更新时间:2023-10-16

我在下面的代码狙击中编写了三个模板。存在输入和显示功能正常工作但排序功能不完全工作的问题。代码中存在一些逻辑问题,但我找不到它。请帮助我找到并解决它。该程序执行得很好,但是当我输入数据时,我认为它会以某种方式进入无限循环,并且其中的所有内容似乎都停止了。

#include<iostream>
using namespace std;
template<typename T>
void input(T* &array, int limit){
    array = new T[limit];
    T* start = array;
    T* beyond = &start[limit];
    while(start != beyond){
        cout<<"nEnter: ";
        cin>>*start;
        start++;
    }
}

template<typename T>
void sort(T* array, int limit){
    T hold;
    T* start = array, *beyond = &start[limit];
    T* Next = &start[1];
    for(int j=0; j<limit-1; j++){
        while((start != beyond) || (Next != beyond)){
            if(start>Next){
                    hold=*start;
                    start=Next;
                    *Next=hold;
                    start++;
                    Next++;
            }
        }
    }

}

template<typename T>
void display(T* start, int limit){
    T* beyond = &start[limit];
    cout<<"nAfter Sorting: "<<endl;
    while(start != beyond){
        cout<<*start<<"t";
        start++;
    }
}
int main(){
    int* x=NULL;
    float* y=NULL;
    char* z=NULL;
    int size;
    cout<<"Enter the number of elements: ";
    cin>>size;
    cout<<"nEnter integer values:";
    input<int>(x, size);
    sort<int>(x, size);
    display<int>(x, size);

    cout<<"nEnter floating values:";
    input<float>(y, size);
    sort<float>(y, size);
    display<float>(y, size);
    cout<<"nEnter character values:";
    input<char>(z, size);
    sort<char>(z, size);
    display<char>(z, size);
    cout<<endl;
    system("pause");
}

我不确定你为什么要尝试使用模板来做到这一点。但这里有一些不好的事情。

array = new T[limit];

在这里,您通过new分配内存,但从不删除它 - 这是一个严重的错误。阅读新建和删除 - 您可能需要考虑使用类似std::shared_ptr<T> array (new T[limit], std::default_delete<T[]>())来创建共享指针(include <memory>在任何 c++11 编译器上

(
while ((start != beyond) || (Next != beyond)) {
        if (start>Next) {

这将永远循环(我相信这是您问题的原因(,您仅在开始大于下一个时增加启动,但是由于开始低于下一个,这永远不会发生!

例如:start = 0x0000A 和 next = 0x0000C,Start 低于 next,因此您永远不会输入 if 语句start++永远不会发生。

建议:

正如其他人所说,使用标准库是一个非常好的主意。

尝试以这种方式对数据进行排序,使用指针和数组是一个非常糟糕的主意,在现代C++中使用new和delete几乎是一个禁忌(至少在我工作的地方;-(。

永远不要使用namespace std;

考虑使用标准库。

大多数问题在过去已经解决,其中很多问题都是如此基本,以至于您可以找到它的标准实现。

在重新实现解决方案时,您可以期望的最好的事情就是不要编写错误的代码。这些标准不仅经过充分测试,在阅读代码时,只需查看它,您就可以了解很多功能。如果你阅读了一个自定义实现,你必须检查它的作用。如果您阅读正在使用的标准实现,您已经确切地知道它的作用。

下面是一个代码片段,它将以与上述类似的方式对代码进行排序,但使用标准实现。

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
using std::cout;
using std::endl;
using std::cin;
using std::vector;
using std::string;
template<typename T>
void input(string msg, T &element)
{
    cout << msg;
    cin >> element;
}
template<typename T>
void display(string label, const vector<T> & v)
{
    cout << label;
    for (const T & t : v)
    {
        cout << " ";
        cout << t;
    }
    cout << endl;
}
int main()
{
    vector<int> in;
    int size;
    cout << "Enter the number of elements: ";
    cin >> size;
    in.reserve(size);
    for (int i = 0; i < size; ++i)
    {
        int temp;
        input("Input Number: ", temp);
        in.emplace_back(temp);
    }
    //http://en.cppreference.com/w/cpp/algorithm/sort
    display("before sorting", in);
    std::sort(in.begin(), in.end(), std::greater<int>());
    display("after sort greater", in);
    std::sort(in.begin(), in.end(), std::less<int>());
    display("after sort less", in);
    //in Visual Studio hit (CTRL + F5) to start with a pause after execution ends
}

如果您必须以某种方式实现循环访问指针的排序函数。这是一个低效的版本:

template<typename T>
void sort(T* array, int limit)
{
    T* start = array;
    T* end = start + limit;
    for (T* outer = start; outer < end; ++outer)
    {
        for (T* inner = start; inner < end; ++inner)
        {
            if (*outer > *inner)
            {
                T hold = *inner;
                *inner = *outer;
                *outer = hold;
            }
        }
    }
}