2011-04-01 3 views
0

내 코드는 ... 사전 순으로 이름을 정렬하는 가장 좋은 방법은 무엇입니까?알파벳순 정렬

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

int main() 
{ 
    int StudentNum; 

    cout << "How many student are in the class?\n"; 
    cin >> StudentNum; 

    string sname[25]; 
    if (StudentNum < 1 || StudentNum > 25) 
    { 
    cout << "Please enter a number between 1-25 and try again\n"; 
    return 0; 
    } 

    for (int i = 1; i <= StudentNum; i++) 
    { 
     cout << "Please enter the name of student #" << i << endl; 
     cin >> sname[i];   
    } 
    for (int output = 0; output <=StudentNum; output++) 
    { 
    cout << sname[output] << endl; 
    } 
    system ("pause"); 
    return 0; 
} 
+2

배열되어야한다. 첫 번째'for'는'for (int i = 0; i Pablo

답변

4

표준 방법은 std::sort을 사용하는 것입니다

#include <algorithm> 

// ... 

std::sort(sname, sname + StudentNum); 

std::sort 실제로 문자열을 알파벳 순으로 비교를 수행 기본적으로 operator< 사용합니다.

편집 : 실제로, StudentNum 대신 'N-1'을 '0'로 번호가 C++ 25.

+2

은'sname + StudentNum'이되어야합니다 – qwertymk

+0

나는 그렇게하고 배열은 그렇지 않습니다. 출력에 나타납니다. –

관련 문제