while ループで 2 つの文字列を比較しようとしています。コードのスニペットを次に示します。
//variables
string pivot, array[10];
int rightBoundary;
//loop
while( pivot < array[rightBoundary])
このコードはクイックソートのチュートリアルからのものですが、文字列で動作するように変換しようとしています。
だから私の質問は、この比較を行うための最良の方法は何ですか.
現在、このエラーが発生します (quickSortNumbers.exe の 0x774215de で未処理の例外: 0xC0000005: 場所 0x965b7214 を読み取るアクセス違反)。
そして助けは素晴らしいでしょう:)
編集:申し訳ありませんが、すべてのコードをアップロードする必要がありました。問題は実際には文字列配列である可能性があります。すべての私のコードをここに示します:
#include <iostream>
#include <string>
using namespace std;
#define array1_SIZE 5 //change the array1 size here
void Printarray1(string* array1, int n);
void QuickSort(string* array1, int startIndex, int endIndex);
int Splitarray1(string* array1, string pivot, int startIndex, int endIndex);
void swap(string &a, string &b);
int main(void)
{
string array1[array1_SIZE];
int i;
for( i = 0; i < array1_SIZE; i++) //array1 elements input
{
cout<<"Enter an integer : ";
cin>>array1[i];
}
cout<<endl<<"The list you input is : "<<endl;
Printarray1(array1, array1_SIZE);
QuickSort(array1,0,array1_SIZE - 1); //sort array1 from first to last element
cout<<endl<<"The list has been sorted, now it is : "<<endl;
Printarray1(array1, array1_SIZE);
cin.get();
cin.get();
int read;
cin >> read;
return 0;
}
/* This function swaps two numbers
Arguments :
a, b - the numbers to be swapped
*/
void swap(string &a, string &b)
{
string temp;
temp = a;
a = b;
b = temp;
}
/* This function prints an array1.
Arguments :
array1 - the array1 to be printed
n - number of elements in the array1
*/
void Printarray1(string* array1, int n)
{
int i;
for( i = 0; i < n; i++)
{
cout << array1[i] << '\t';
}
}
/* This function does the quicksort
Arguments :
array1 - the array1 to be sorted
startIndex - index of the first element of the section
endIndex - index of the last element of the section
*/
void QuickSort(string* array1, int startIndex, int endIndex)
{
string pivot = array1[startIndex]; //pivot element is the leftmost element
int splitPoint;
if(endIndex > startIndex) //if they are equal, it means there is
//only one element and quicksort's job
//here is finished
{
splitPoint = Splitarray1(array1, pivot, startIndex, endIndex);
//Splitarray1() returns the position where
//pivot belongs to
array1[splitPoint] = pivot;
QuickSort(array1, startIndex, splitPoint-1); //Quick sort first half
QuickSort(array1, splitPoint+1, endIndex); //Quick sort second half
}
}
/* This function splits the array1 around the pivot
Arguments :
array1 - the array1 to be split
pivot - pivot element whose position will be returned
startIndex - index of the first element of the section
endIndex - index of the last element of the section
Returns :
the position of the pivot
*/
int Splitarray1(string* array1, string pivot, int startIndex, int endIndex)
{
int leftBoundary = startIndex;
int rightBoundary = endIndex;
while(leftBoundary < rightBoundary) //shuttle pivot until the boundaries meet
{
while( pivot < array1[rightBoundary]//keep moving until a lesser element is found
&& rightBoundary > leftBoundary) //or until the leftBoundary is reached
{
rightBoundary--; //move left
}
swap(array1[leftBoundary], array1[rightBoundary]);
//Printarray1(array1, array1_SIZE); //Uncomment this line for study
while( pivot >= array1[leftBoundary] //keep moving until a greater or equal element is found
&& leftBoundary < rightBoundary) //or until the rightBoundary is reached
{
leftBoundary++; //move right
}
swap(array1[rightBoundary], array1[leftBoundary]);
//Printarray1(array1, array1_SIZE); //Uncomment this line for study
}
return leftBoundary; //leftBoundary is the split point because
//the above while loop exits only when
//leftBoundary and rightBoundary are equal
}