2012-04-21 42 views
1

我想比較兩個字符串我while循環,繼承人我的代碼片段比較字符串:如何使用(<,>)

//variables 
string pivot, array[10]; 
int rightBoundary; 

//loop 
while(pivot < array[rightBoundary]) 

這個代碼是從快速排序,但我嘗試教程將其轉換爲使用字符串。

所以我的問題是做這個比較的最佳方法是什麼。

目前我得到這個錯誤(未處理的異常在0x774215de在quickSortNumbers.exe:0000005:訪問衝突讀取位置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 
} 
+0

崩潰前'rightBoundary'的值是多少? – Gowtham 2012-04-21 15:23:45

回答

2

你可能有一個出界外的錯誤,可能是由於未初始化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

+0

嗨我沒有初始化變量對不起,我沒有顯示它,請看我的整個代碼,看看你是否可以發現錯誤,你可以complile代碼,看看它崩潰的地方。 – user1348463 2012-04-21 15:45:19

+0

這對於調試器來說非常有用。 – chris 2012-04-21 15:54:59

+0

以及我使用的視覺工作室,它的所有內置,但我不能看到問題出在哪裏:S – user1348463 2012-04-21 15:57:22

1

使用<來比較字符串是好的,但如果您要按字母順序排列,可能不會如您所期望的那樣,因爲如果您希望按照字典順序排列,則所有小寫字母都必須使用lexicographical_compare。

你的代碼崩潰的原因是因爲你不指定任何初始值rightBoundary,你應該做的:

int rightBoundary = 0; 

否則rightBoundary會有這將壓倒性的概率比更大的任意初始值'數組'的大小並導致出界限訪問。

+0

嗨我沒有初始化該變量對不起,我沒有顯示它,請看我的整個代碼,看看你是否可以發現錯誤,你可以complile代碼,看看它崩潰的地方。 – user1348463 2012-04-21 15:45:24

相關問題