2014-01-09 125 views
1

我想使用結構對數組進行排序(我第一次使用結構)。這個結構獲取一些學生的名字和ID號,問題在於顯示FirstName時顯示其他FirstNames(我使用dev的C++)在C++中對結構進行排序

#include<iostream> 
#include<fstream> 
#include<conio.h> 
#include <sstream> 
using namespace std; 
struct Student 
{ 
    long long int ID; 
    string FirstName,LastName; 
} mystudent[100]; 
void PrintInformation(Student sEmployee[100],int b) 
{ 
    for(int i=0;i<b;i++) 
    { 
      cout << "First Name: " << sEmployee[i].FirstName<< endl; 
      cout << "ID: " << sEmployee[i].ID<< endl; 
      } 
} 
void Sort (Student mine); 
int main() 
{ 
    int n, c, d, temp; 
    cout << "Enter element: "; 
    cin>>n; 
    for(int i=0;i<n;i++) 
    { 
    cout << "Enter FirstName "<<i+1<<": "; 
    cin>>mystudent[i].FirstName; 
    } 
    for(int i=0;i<n;i++) 
    { 
    cout << "Enter ID "<<mystudent[i].FirstName<<": "; 
    cin>>mystudent[i].ID; 
    } 
    for (int i = 1 ; i <= n - 1; i++) 
    { 
    d = i; 
    while (d > 0 && mystudent[i].ID < mystudent[i-1].ID) 
    { 
     temp   = mystudent[i].ID; 
     mystudent[i].ID = mystudent[i-1].ID; 
     mystudent[i-1].ID = temp; 
     mystudent[i-1].FirstName=mystudent[i].FirstName; 
     d--; 
    } 
    } 
    cout<<"Sorted list in ascending order:\n"; 

    PrintInformation(mystudent,n); 
    getch(); 
    return 0; 
} 
+0

如果這是實際生產代碼,我會建議[使用std ::排序您陣列(http://stackoverflow.com/questions/15669900/using-sort-in-stl-to- sort-an-array)和[爲你的Student類提供嚴格的小於運算符](http://stackoverflow.com/questions/9336579/overloading-comparison-operators-to-work-with-stl-sort-in- c) - 也可以使用自定義的操作符函數來替代小於等於的操作符。 –

+0

我使用下面的代碼** woolstar **交換名字,但現在它沒有正確排序。 – user2589043

+0

感謝大家,我的問題解決了。 – user2589043

回答

2

此代碼:

temp   = mystudent[i].ID; 
    mystudent[i].ID = mystudent[i-1].ID; 
    mystudent[i-1].ID = temp; 
    mystudent[i-1].FirstName=mystudent[i].FirstName; 

交換的ID,但複製FirstName。更糟糕的是,它與LastName無關。

嘗試,而不是:

struct Student tempstudent = mystudent[i] ; 
mystudent[i]= mystudent[i-1] ; 
mystudent[i-1]= tempstudent ; 

或者更好的是,使用STL。

#include <utility> 

std::swap(mystudent[i], mystudent[i-1]) ; 

注意,swap<algorithm>如果你預先C++ 11。

-2

您的代碼正在交換結構的ID字段。您正在分配(複製)FirstName字段,並且不要對LastName執行任何操作。 struct S可使用=運營商直接分配給對方:

struct Student temp = mystudent[i] ; 
mystudent[i]= mystudent[i-1] ; 
mystudent[i-1]= temp ; 
+2

認真嗎?如果你要複製我的答案,至少要再次輸入,而不是複製和粘貼。 – woolstar

2

提供一個operator<和使用std::sort,並依賴於默認的拷貝構造函數做正確的事。如果您需要以多種方式排序,則可以將排序算法提供給排序算法,而不是提供operator<

... 

struct Student { 
    long long int ID; 
    string FirstName,LastName; 
} mystudent[100]; 

bool operator<(const Student& s1, const Student& s2) { 
    return s1.ID < s2.ID; 
} 

... 

int main() { 
    ... 
    std::sort(mystudent,mystudent+100) 
    cout<<"Sorted list in ascending order:\n"; 
    PrintInformation(mystudent,n); 
    getch(); 
    return 0; 
} 

如果你的編譯器支持C++ 11,你可以交替使用內聯lambda表達式的排序,並省略operator<

... 

struct Student { 
    long long int ID; 
    string FirstName,LastName; 
} mystudent[100]; 

... 

int main() 
{ 
    ... 
    std::sort(mystudent, mystudent+100, 
    [](mystudent const & a, mystudent const &b){return a.ID < b.ID;}); 

    cout<<"Sorted list in ascending order:\n";  
    PrintInformation(mystudent,n); 
    getch(); 
    return 0; 
} 
相關問題