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

时间:2016-02-09 09:42:27

标签: c++ c++11

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

#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");
}

2 个答案:

答案 0 :(得分:0)

我不确定为什么要尝试使用模板执行此操作。但这里有一些不好的事情。

array = new T[limit];

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

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

这将永远循环(我相信是你问题的原因),你只在start大于next时才开始增加,但是当start低于next时,那将永远不会发生!

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

<强>建议:

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

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

永远不要使用namespace std;

答案 1 :(得分:-1)

考虑使用标准库。

过去大多数问题都已解决,其中很多都是基本的,你可以找到它的标准实现。

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

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

SQL function

如果你以某种方式必须实现一个迭代指针的排序函数。这是一个效率低下的版本:

#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
}