2017-12-03 4 views
0

선택 정렬이 문자열과 함께 작동하는 방식을 이해하려고합니다.C++ 선택 문자열 배열 정렬

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


// Prototypes 
void selectionSort(string arr[], int size); 
void showArray(string arr[], int size); 

int main() { 
const int NUM_NAMES = 20; 
string names[NUM_NAMES] = {"Collins, Bill", "Smith, Bart", "Allen, Jim", 
          "Griffin, Jim", "Stamey, Marty", "Rose, Geri", 
          "Taylor, Terri", "Johnson, Jill", "Allison, Jeff", 
          "Looney, Joe", "Wolfe, Bill", "James, Jean", 
          "Weaver, Jim", "Pore, Bob", "Rutherford, Greg", 
          "Javens, Renee", "Harrison, Rose", "Setzer, Cathy", 
          "Pike, Gordon", "Holland, Beth"}; 

// Insert your code to complete this program 
cout << "The names on the list in no particlular order are: "; 
showArray(names, NUM_NAMES); 


// Calling the sorted array 
selectionSort(names, NUM_NAMES); 

// Displaying sorted array 
cout << "The names in sorted order are: "; 
showArray(names, NUM_NAMES); 

return 0; 
} 

// Function to sort the string 
void selectionSort(string arr[], int size) { 
int startScan, minIndex, minValue; 

for (startScan = 0; startScan < (size - 1); startScan++) { 
    minIndex = startScan; 
    minValue = arr[startScan]; 

    for (int index = (startScan + 1); index < size; index++) { 
     if (arr[index] < minValue) { 
      minValue = arr[index]; 
      minIndex = index; 
     } 
    } 
    arr[minIndex] = arr[startScan]; 
    arr[startScan] = minValue; 
    } 

} 

// Function to display the array's conents 
void showArray(const int arr[], int size) { 
    for (int count = 0; count < size; count++) { 
     cout << arr[count]; 
     cout << "\t\n"; 
    } 
} 

내가 그것을 구축하려고하면 실패와 나는 오류 메시지 :

이것은 내가 지금까지있는 것입니다. " 'selectionSort'에 대한 호출과 일치하는 함수가 없습니다."

그러나 문자열 대신 int를 사용하면 제대로 컴파일하고 정렬 할 수 있습니다.

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


// Prototypes 
void selectionSort(int [], int); 
void showArray(const int [], int); 

int main() { 
const int NUM_NAMES = 8; 
int names[NUM_NAMES] = {1,3,5,7,3,5,7,9}; 

// Insert your code to complete this program 
cout << "The names on the list in no particular order are: "; 
showArray(names, NUM_NAMES); 


// Calling the sorted array 
selectionSort(names, NUM_NAMES); 

// Displaying sorted array 
cout << "The names in sorted order are: "; 
showArray(names, NUM_NAMES); 

return 0; 
} 

// Function to sort the string 
void selectionSort(int array[], int size) { 
int startScan, minIndex, minValue; 

for (startScan = 0; startScan < (size - 1); startScan++) { 
    minIndex = startScan; 
    minValue = array[startScan]; 

    for (int index = (startScan + 1); index < size; index++) { 
     if (array[index] < minValue) { 
      minValue = array[index]; 
      minIndex = index; 
     } 
    } 
    array[minIndex] = array[startScan]; 
    array[startScan] = minValue; 
    } 

} 

// Function to display the array's contents 
void showArray(const int array[], int size) { 
    for (int count = 0; count < size; count++) { 
     cout << array[count]; 
     cout << "\t\n"; 
    } 
} 

문자열 정렬이 잘못되어 가고 있는지 잘 모르겠습니다. Image of c++ program with the portion that has an issue

답변

0

컴파일 오류는 간단합니다. void selectionSort(int [], int);은 정수 배열을 허용하고 배열을 정렬합니다. 그러나 문자열 배열을 정렬하려고합니다. 귀하는 다음을 선언했습니다 :

string names[NUM_NAMES]; 

문자열 배열을 허용하도록 정렬 기능을 변경해야합니다. std::string=, <, >, == 연산자를 지원하기 때문에 수정이 쉽습니다.

void selectionSort(string arr[], int size) 
{ 
    int startScan, minIndex; 
    string minValue; 

    for(startScan = 0; startScan < (size - 1); startScan++) 
    { 
     minIndex = startScan; 
     minValue = arr[startScan]; 

     for(int index = (startScan + 1); index < size; index++) 
     { 
      if(arr[index] < minValue) 
      { 
       minValue = arr[index]; 
       minIndex = index; 
      } 
     } 
     arr[minIndex] = arr[startScan]; 
     arr[startScan] = minValue; 
    } 
} 
+0

감사합니다. 그러나 나는 아직도 약간의 오류를 얻고있다. 내 게시물을 하단 오류로 업데이트했습니다. –

+0

함수 인자'void selectionSort (string arr [], int size)'만 변경했습니다. 나는 또한 'string minValue;'로 변경했다. void showArray (int arr [], int size);에 대한 함수 선언 (또는 프로토 타입)을 올바르게 고쳤지만 함수 정의는 여전히'void showArray (int arr [], int size) {...}를 사용합니다. ', 이것은 자연스럽게 링크 에러를 일으킬 것이다. 왜냐하면 컴파일러는 나중에 나타날 내용을 찾을 수 없기 때문이다. –

+0

아, 고맙습니다. 나는 내가 그것을 바꿨다 고 생각했다. 그러나 나는 추측한다. 고맙습니다! :) –