c++ - 如何使用 (<,>) 比较字符串

标签 c++ compare quicksort

我正在尝试为我的 while 循环比较两个字符串,这是我的代码片段:

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

最佳答案

您可能遇到越界错误,可能是由于未初始化 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"
}

要比较而不用担心大小写,您可以使用 lexicographical_compare使用您自己的比较器功能:

#include <algorithm>
using std::lexicographical_compare;

#include <cctype>
using std::tolower;

#include <iostream>
using std::cout;

#include <string>
using std::string;

bool nocase_compare (char one, char two)
{
    return tolower (one) < tolower (two);
}

int main()
{
    string s1 = "Hello";
    string s2 = "happy";

    if (lexicographical_compare (s1.begin(), s1.end(), s2.begin(), s2.end(), nocase_compare))
        cout << s1;
    else
        cout << s2;
    //prints "happy" even though 'H' < 'h'
}

如果你真的想使用<和>,你必须为string做一个小的包装器。实现了你的 operator< 版本和 operator> .在 string 中实现的那些使用默认 lexicographical_compare .

关于c++ - 如何使用 (<,>) 比较字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10260243/

相关文章:

c++ - 为什么 std::ios_base::sync_with_stdio 没有在 libc++ (clang) 中实现?

c++ - 在 STL 容器中使用模板类

javascript - 如何比较 Javascript 中的数组和对象?

比较字符串中的单词

algorithm - 关于 Fast 3 Way Partition/in place quicksort via Sedgewick 中的排序数据

c - 这个算法的时间复杂度是多少。我可以让它更快吗?

c++ - 在 C++ 中序列化,在 Python 中反序列化?

mysql - 如何使用 Codeigniter 将两个不同的查询合并为一个查询

algorithm - 如何使用我自己选择的数据透视表(逐步)对这些数据进行快速排序

c++ - 将 std::any 转换回其原始类型?