C++ 如何使用(<;,>;)比较字符串
我正在尝试比较while循环的两个字符串,下面是我的代码片段:C++ 如何使用(<;,>;)比较字符串,c++,compare,quicksort,C++,Compare,Quicksort,我正在尝试比较while循环的两个字符串,下面是我的代码片段: //variables string pivot, array[10]; int rightBoundary; //loop while( pivot < array[rightBoundary]) //变量 字符串轴,数组[10]; int右边界; //环路 while(透视
//variables
string pivot, array[10];
int rightBoundary;
//loop
while( pivot < array[rightBoundary])
//变量
字符串轴,数组[10];
int右边界;
//环路
while(透视<数组[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
}
#包括
#包括
使用名称空间std;
#定义阵列1_大小5//在此处更改阵列1大小
void printary1(字符串*array1,int n);
无效快速排序(字符串*array1,int-startIndex,int-endIndex);
int-Splitarray1(字符串*array1,字符串轴,int-startIndex,int-endIndex);
无效掉期(字符串和a、字符串和b);
内部主(空)
{
字符串数组1[array1_SIZE];
int i;
对于(i=0;i cout您可能有越界错误,可能是由于未初始化rightBoundary
。使用比较运算符可以很好地比较字符串
#include <iostream>
using std::cout;
#include <string>
using std::string;
int main()
{
string s1 = "hello";
string s2 = "world!";
string lower = s1 < s2 ? s1 : s2;
cout << lower; //prints "hello"
}
如果你真的想使用,你必须为string
做一个小包装,它实现了你的操作符版本
。在string
中实现的那些使用默认的词典学\u compare
来比较字符串是可以的,但可能不是你所说的pect如果你想按字母顺序排列,因为所有小写字母都在大写字母之前。如果你想按字典顺序排列,你应该改为使用字典顺序
代码崩溃的原因是您没有为rightBoundary指定任何初始值,您应该执行以下操作:
int rightBoundary = 0;
因为否则rightBoundary将有一个任意的初始值,该值极有可能大于“数组”的大小,并导致越界访问。在崩溃之前,
rightBoundary
的值是多少?您好,我确实初始化了该变量,抱歉,我没有显示它,请查看我的整个代码,看看是否您可以发现错误,您可以编译代码以查看崩溃的位置。我使用的是内置的visual studio,但我看不出问题出在哪里:这是它将崩溃的行:splitPoint=Splitarray1(array1,pivot,startIndex,endIndex);另外,您知道比较字符串“hello”和“hello”的更好方法吗将以不同的方式进行排序,并且我希望此排序合理?我更新了我的答案,使其不考虑大小写。进入Splitarray1
并仔细查看它,因为它肯定不会在该行上崩溃。您好,我初始化了该变量。抱歉,我没有显示它,请查看我的整个代码,以查看是否可以发现错误,可以整理代码以查看它在哪里崩溃。
int rightBoundary = 0;