2014-05-20 43 views
0

讓說,我有一個包含該數據學生信息系統級分配矢量搜索

First Name Last Name  ID   Tuition GPA 
Alex   Robinson  02341  1400.50 3.45 
John   Brown   043261 1200.00 3.21 
Rachel   VanGorp  77239  600.00 4.00 
James   Smith   89167  1150.55 1.45 
Juan   Garcia   23165  1200.00 3.33 
Lindsey  McDonald  32501  1340.50 2.25 
Tom   Barry   78442  500.00 3.85 
Gwen   Poss   30029  1150.50 3.85 
Janice   Jones   44560  2200.50 2.45 
Jack   Price   99314  1850.55 3.12 

一個文本文件,我導入項目名單爲載體,這裏是我的學生類和我classOfStudents類。幫助這個信息將是偉大的。我迷路了,不知道該去哪裏。只有參考和一個正確的方向點將是偉大的!

 class student 
     { 
    public: 
     student() 
     { 
      firstName = ""; 
      lastName = ""; 
      ID = ""; 
      tuitionBalance = 0.0; 
    GPA = 0.0; 
} 
student(string first, string last, string id, double tuition, double gpa) 
{ 
    first = firstName; 
    last = lastName; 
    id = ID; 
    tuition = tuitionBalance; 
    gpa = GPA; 
} 

void openIF() 
{ 
    inFile.open("students.txt"); 
} 
void setFirstName() 
{ 
    inFile >> temp; 
    firstName = temp; 
} 
void setLastName() 
{ 
    inFile >> temp; 
    lastName = temp; 
} 
void setID() 
{ 
    inFile >> temp; 
    ID = temp; 
} 
void setBalance() 
{ 
    inFile >> tuitionBalance; 
} 
void setGPA() 
{ 
    inFile >> GPA; 
} 
void closeIF() 
{ 
    inFile.close(); 
} 

string firstName, lastName, ID; 
double tuitionBalance, GPA; 
    private: 

ifstream inFile; 
char temp[128]; 
    } 

class classOfStudents 
{ 
public: 
    vector<student> students; 

    classOfStudents() 
    { 
     student Students = student(firstName, lastName, ID, tuitionBalance, GPA); 
     students.push_back(Students); 
    } 

    void findStudent() 
    { 
     int searchID; 
     cout << "Enter ID of student" << endl; 
     cin >> searchID; 
     find(students.begin(), students.end(), searchID); 
    } 

private: 
    string firstName, lastName, ID; 
    double tuitionBalance, GPA; 
} 

我怎麼會拉與學生的只是ID其餘的信息?

+1

你可以用'ID'作爲鍵和'student'作爲'map'作爲值。 – CoryKramer

+1

最基本的解決方案是迭代矢量,直到找到具有正確ID的「學生」;網絡方法用於開始使用更復雜的數據結構(一點點)。 – SJuan76

+0

@Cyber​​當我試圖映射它時Visual Studio告訴我,我在地圖 – user3658110

回答

0
using namespace std; 

#include <iostream> 
#include <map> 

class student 
{ 
public: 
string firstName, lastName; 
int ID; 
double tuitionBalance, GPA; 

student(){} 

student(string fName, string lName, int id, double tBalance, double gpa) 
{ 
    firstName = fName; 
    lastName = lName; 
    ID = id; 
    tuitionBalance = tBalance; 
    GPA = gpa; 
} 
}; 

class classOfStudents 
{ 
public: 

std::map <int, student> StudentMap; 

classOfStudents() 
{ 
    student Students = student("Firstname", "Lastname", 123, 0.00, 4.00); 
    StudentMap.insert(std::pair<int, student>(123, Students)); 
} 

void findStudent() 
{ 
    int searchID; 
    cout << "Enter ID of student" << endl; 
    cin >> searchID; 

    if (StudentMap.find(searchID) == StudentMap.end()) 
    { 
     printf("Student not found"); 
    } 
    else 
    { 
     printf("Name: %s %s ", StudentMap[searchID].firstName, StudentMap[searchID].lastName); 
    } 
} 
}; 
+0

檢查更新的代碼。您可以使用插入方法插入一對,[]運算符可用於查找給定鍵的值。 – Sanzad