将用户输入从int[]转换为char[][]

4

我写了一个程序,以int数组作为输入,并使用快速排序对其进行排序,但我想知道如何修改此程序,使其接受char[][]作为输入(字符串数组)并按字母顺序对它们进行排序?如果只有一个字符串,它可以工作,但我想知道如果有人想要字符串数组

//following program sorts an array using quicksort alorithm

#include<iostream.h>
#include<conio.h>

void swap(int *a, int *b)   //function to swap elements
{
    int t;
     t = *a;
    *a = *b;
    *b = t;
}

int partition(int arr[], int left, int right)  //function takes last element as pivot and places all smaller elements on left of pivot and greater elements on right
{
    int pivot=arr[right];  //Pivot
    int i= (left-1);      //index of smaller element

    for(int j=left; j<=(right-1); j++)
    {
        if(arr[j]<=pivot)           //if current element is smaller or equal to pivot, theyre swapped
        {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i+1], &arr[right]);
    return (i+1);
}

void quicksort(int arr[], int left, int right) //left is starting index, right is last index
{
    if(left<right)
    {
        int index=partition(arr,left,right);
        quicksort(arr, left, index-1);      //sort elements before and after partition
        quicksort(arr, index+1, right);
    }
}

void print(int *arr, int size)      //function to print elements in array
{
    for(int i=0; i<size; i++)
    {
        cout<<arr[i]<<" ";
    }
}

int main()
{
    int n;      //to store no. of elements in array
    char ch;   //ch for choice
    do{
        int *arr=NULL;     //dynamic int array
        clrscr();
    cout<<"\nEnter Number of Elements:";
    cin>>n;
    cout<<"\nEnter Elements in Array to be sorted:";
    for(int i=0; i<n; i++)
    {
        cout<<"\nEnter "<<i<<"th element:";
        cin>>arr[i];
    }
    quicksort(arr,0,(n-1));
    cout<<"\nSorted Array= ";
    print(arr,n);
    delete arr;
    cout<<"\nwanna sort again??(y/n):";
    cin>>ch;
    }while(ch=='Y'||ch=='y');
    return 0;
}

1
为了比较字符串,你应该使用strcmp()或类似的函数。你可以像交换整数一样轻松地交换两个char*指针(指针是简单类型,就像整数一样)。顺便说一句,你的编译器太古老了。请,请升级到至少支持C++14的版本。MinGW/GCC和Microsoft的编译器都可以免费使用。 - Dúthomhas
2
在C++中,你应该使用std::string而不是char[]数组。 - Barmar
1
@UdayPatel 那么你应该相应地打标签 -- [c++] 单独表示当前的 C++。自从标准化以来,C++ 就拥有了 std::string,那么你的编译器是哪个年代的呢? - Quentin
这真的很老 xD 可能是 99 年的,但我从头开始学习... 嘿嘿 - Uday Patel
1
如果你正在学习,为什么不使用现代编程语言呢?在C++17中,你问题中的几乎所有代码都将被淘汰。学习如何使用一个过时的编译器只会让你忘记这些内容,然后不得不从头开始学习。 - Quentin
显示剩余4条评论
1个回答

2
您的代码中需要使用 int *arr = new int[n]; 来分配整数数组。
要使用字符串数组,请声明 char **arr = new char*[n]; 并使用 strdup 将 char 数组分配给每个字符串。
在旧编译器中,您应该可以使用标准的 qsort,否则请使用修改过的快速排序。主要区别是将 if(arr[j]<=pivot){} 替换为 if(strcmp(arr[j], pivot) <= 0){}
void swap(char* &a, char* &b)
{
    char *t = a;
    a = b;
    b = t;
}

int partition(char** arr, int lo, int hi)
{
    int i = lo - 1;
    for(int j = lo; j < hi - 1; j++)
    {
        if(strcmp(arr[j], arr[hi]) < 0)
        {
            i++;
            swap(arr[i], arr[j]);
        }
    }

    if(strcmp(arr[hi], arr[i + 1]) < 0)
        swap(arr[hi], arr[i + 1]);

    return i + 1;
}

void quicksort(char** arr, int const lo, int const hi)
{
    if(lo < hi)
    {
        int p = partition(arr, lo, hi);
        quicksort(arr, lo, p);
        quicksort(arr, p + 1, hi);
    }
}

void print(char **arr, int size)      
{
    for(int i = 0; i<size; i++)
        cout << arr[i] << ", ";
    cout << "\n";
}

int main()
{
    int n;      
    cout << "Enter Number of Elements: ";
    cin >> n;
    cout << "Enter Elements in Array to be sorted:\n";

    char buf[255];

    char **arr = new char*[n];
    for(int i = 0; i < n; i++)
    {
        cout << "Enter " << i << "th element: ";
        cin >> buf;
        arr[i] = strdup(buf);
    }

    quicksort(arr, 0, (n - 1));
    cout << "Sorted:\n";
    print(arr, n);
    cout << "\n";

    for(int i = 0; i < n; i++)
        free(arr[i]); //<=== edit**
    delete[]arr;

    return 0;
}

编辑1:更改quicksort函数
编辑2:更改清理。必须使用free清理strdup


2
我认为qicksort有一个错误,我已经更改了代码。 - Barmak Shemirani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接