2011-11-01 38 views
1

我有vectorstructs。 我需要根據struct中向量中的學生姓氏的字母順序對向量進行排序。這可能嗎?在struct每個學生有下列信息:在其他函數中對結構體和向量部分的向量排序?

struct Student 
{ 
    string lastName; 
    string firstName; 
    string stdNumber; 
    double assgn1;//doubles are what I want to add in another function. 
    double assign2; 
    double assign3; 
    double assign4; 
    double midTerm; 
    double finalGrade; 

bool operator<(Student const &other) const { 
    return lastName < other.lastName; 
} 
}; 

這是我students矢量由填充:

int getFileInfo() 
{ 
int failed=0; 
ifstream fin; 
string fileName; 
vector<Student> students;// A place to store the list of students 
Student s;     // A place to store data of one student 
cout<<"Please enter the filename of the student grades (ex. filename_1.txt)."<<endl; 
do{ 
    if(failed>=1) 
    cout<<"Please enter a correct filename."<<endl; 
    cin>>fileName; 
    fin.open(fileName.c_str());// Open the file 
    failed++; 
}while(!fin.good()); 
    while (fin >> s.firstName >> s.lastName >> s.stdNumber){ 
    cout<<"Reading "<<s.firstName<<" "<<s.lastName<<" "<<s.stdNumber<<endl; 
    students.push_back(s); 
} 
    vector<Student>::iterator loop = students.begin(); 
    vector<Student>::iterator end = students.end(); 
    fin.close(); 

    return 0; 
} 

所以我想在最後的名字struct排序,然後是能夠在另一個函數中處理矢量中的每個「Student」結構體。這可能嗎?

我想能夠將double零件添加到我在另一個函數中的矢量中的每個學生。然後我希望能夠打印出每個學生的所有信息。如果我在students矢量所在的功能中打印出每個學生的信息,但我需要使用另一個功能void gradeInput()進行打印。我將cin>>double年級爲每個學生,一次一個學生。我在想它會看起來像這樣:

void gradeInput() 
{ 
For(each student)// I don’t know what to do to in this for loop to loop through 
//each student. I want to make it so everywhere 「stud」 is change to the 
//next student after one loop iteration. 

//I made a default `Student` called ‘stud’ to show this example.. 
    cout<<"Student "<<stud.firstName<<" "<<stud.lastName<<" "<<stud.stdNumber<<":"; 
    cout<<"Please, enter a grade (between 0.0 and 100.0) for ... "<<endl; 
    cout<<"Assignment 1:"; 
    cin>>stud.assgn1; 
    cout<<endl; 
    cout<<"Assignment 2:"; 
    cin>>stud.assign2; 
    cout<<endl; 
    cout<<"Assignment 3:"; 
    cin>>stud.assign3; 
    cout<<endl; 
    cout<<"Assignment 4: "; 
    cin>>stud.assign4; 
    cout<<endl; 
    cout<<"MidTerm: "; 
    cin>>stud.midTerm; 
    cout<<endl; 
    cout<<"Final Exam: "; 
    cin>>stud.finalGrade; 
    cout<<endl; 

return; 
} 

希望這是有道理的,我可以得到一些幫助!我的老師對我沒有幫助,因爲她沒有回覆電子郵件,並有45分鐘的辦公時間,所以你所有友好的人都是一個偉大的資產!謝謝!
P.S.對不起,糟糕的代碼格式,我仍然試圖找出網站的代碼輸入。

回答

0

獲取排序工作是很容易 - 只需定義該類型的operator<:您可能希望(例如)

class student { 
    // ... 

    bool operator<(student const &other) const { 
     return last_name < other.last_name; 
    } 
}; 

注意使用學生的名字作爲第二個排序字段,只有在/姓名相同的情況下才能使用。

至於讀取數據對一個學生的話,你通常想要做的是,在一個名爲operator>>功能,如:

std::istream &operator>>(std::istream &is, student &s) { 
    is >> student.last_name; 
    is >> student.first_name; 
    // read other fields; 
    return is; 
} 

如果在所有合理的,我會避免進入交互性儘管如此。雖然在學生作業中很常見,但卻使得該程序幾乎無法使用。通常使用operator<<來打印出這些信息,它基本上是operator>>的鏡像(即,它寫入而不是讀取,但應該以operator>>可以讀取的格式以相同的順序寫入字段)。

+0

上課還剩下一個月,我們還沒有學過D班:...所以請幫助我。我是否使用'class'而不是'struct'?我看到你評論了課程的開始,我知道我必須初始化並創建一些東西,但我不知道是什麼。你能在那裏寫一些僞代碼,看看我能弄清楚該怎麼做?與'&operator'功能一樣,以前從未使用過。謝謝 - – b3orion

+0

不 - 您可以將其保留爲結構體。只需在該結構中定義你的'operator <'。 –

+0

請參閱http://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c –