Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对指针数组进行排序。_C++_Sorting_Pointers - Fatal编程技术网

C++ 对指针数组进行排序。

C++ 对指针数组进行排序。,c++,sorting,pointers,C++,Sorting,Pointers,我有下面的函数,它接受一个指针数组和一个大小。然后按字母顺序对数组中的字符串进行排序 但是,每次我在主函数中运行该函数时,我的程序都会崩溃。我的代码中可能有什么错误 int main() { int size = 3; string *myList= new string[size]; myList[0] = "Hello"; myList[1] = "What's your name"; myList[2] = "How are you?"; a

我有下面的函数,它接受一个指针数组和一个大小。然后按字母顺序对数组中的字符串进行排序

但是,每次我在主函数中运行该函数时,我的程序都会崩溃。我的代码中可能有什么错误

int main()
{
    int size = 3;
    string *myList= new string[size];
    myList[0] = "Hello";
    myList[1] = "What's your name";
    myList[2] = "How are you?";
    arrSelectSort(&myList, size);

    return 0;
}
void arrSelectSort(string *arr[], int size)
{
    int startScan, minIndex;
    string *minElem = nullptr;
    for (startScan = 0; startScan < (size - 1); startScan++) {
        minIndex = startScan;
        minElem = arr[startScan];
        for (int index = startScan + 1; index < size; index++) {
            if (*(arr[index]) < *minElem) {
                minElem = arr[index];
                minIndex = index;
            }
        }
        arr[minIndex] = arr[startScan];
        arr[startScan] = minElem;
    }
}
intmain()
{
int size=3;
字符串*myList=新字符串[大小];
myList[0]=“你好”;
myList[1]=“你叫什么名字?”;
myList[2]=“你好吗?”;
arrSelectSort(&myList,size);
返回0;
}
void arrSelectSort(字符串*arr[],整数大小)
{
int startScan,minIndex;
字符串*minElem=nullptr;
对于(startScan=0;startScan<(大小-1);startScan++){
minIndex=startScan;
minElem=arr[startScan];
对于(int index=startScan+1;index
上面的代码暗示您有一个指针数组,而您没有。您拥有的是一个由
string*myList
指向的字符串数组,您将这个指针(而不是数组)的地址传递给函数


因此,您只有一个指针(
myList
)指向存储字符串数组的内存位置,例如
string*arr[]=&myList
arr[索引]最终(当
索引>0
时)将从包含垃圾数据(不是有效指针)的内存位置访问一个值,尝试执行该值将导致错误。

以下是工作代码

#include<cstdio>
#include<string>
#include<iostream>
using namespace std;

void arrSelectSort(string *arr, int size)
{
    int startScan, minIndex;
    string minElem;
    for (startScan = 0; startScan < (size - 1); startScan++) {
        minIndex = startScan;
        minElem = arr[startScan];
        for (int index = startScan + 1; index < size; index++) {
            if (arr[index] < minElem) {
                minElem = arr[index];
                minIndex = index;
            }
        }
        arr[minIndex] = arr[startScan];
        arr[startScan] = minElem;
    }
}

int main()
{
    int size = 3;
    string *myList= new string[size];
    myList[0] = "Hello";
    myList[1] = "What's your name";
    myList[2] = "How are you?";
    arrSelectSort(myList, size);

    for(int i=0;i<size;i++) {
        cout << myList[i] << endl;
    }
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
void arrSelectSort(字符串*arr,整数大小)
{
int startScan,minIndex;
细绳雷姆;
对于(startScan=0;startScan<(大小-1);startScan++){
minIndex=startScan;
minElem=arr[startScan];
对于(int index=startScan+1;index对于(int i=0;i)您没有指针数组。这是什么意思?myList本身不是指针数组吗?不,myList是指向字符串数组的指针。@user3577478
myList
是指针。它指向
string
(我猜是
std::string
,但鉴于您发布的代码不完整,我不能肯定。)
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;

void arrSelectSort(string *arr, int size)
{
    int startScan, minIndex;
    string minElem;
    for (startScan = 0; startScan < (size - 1); startScan++) {
        minIndex = startScan;
        minElem = arr[startScan];
        for (int index = startScan + 1; index < size; index++) {
            if (arr[index] < minElem) {
                minElem = arr[index];
                minIndex = index;
            }
        }
        arr[minIndex] = arr[startScan];
        arr[startScan] = minElem;
    }
}

int main()
{
    int size = 3;
    string *myList= new string[size];
    myList[0] = "Hello";
    myList[1] = "What's your name";
    myList[2] = "How are you?";
    arrSelectSort(myList, size);

    for(int i=0;i<size;i++) {
        cout << myList[i] << endl;
    }
    return 0;
}