2012-04-29 5 views
0

2つの文字列、長い整数、整数の配列からなる構造体のベクトルを持っています。上記の構造体の作成時に、配列の各要素を0に初期化します。私の質問は、配列の各要素に異なる値を割り当てるにはどうすればいいですか?私はスワップと代入を使用しようとしましたが、2次元のものではなく2つの1次元ベクトルを持つためのもので、特定の構造体の値を変更したいだけです。助けてください?ありがとう!構造体ベクトルの内容を入れ替えます。C++

あなたには、いくつかのコードを見てみたいと思います場合は、これは私がこれまで持っているものです。

//this is my struct 
typedef struct { 
    string lastName; 
    string firstName; 
    long int gNumber; 
    int grades[12]; 
} student; 

//this function takes data from a file, fills it into a struct, then pushes back into //vector 

bool loadStudentData(vector<student> &studentRecords, ifstream *inFile, student tempStudent) { 
    int idx = 0; 
    stringstream fileLine; 
    string line; 
    bool isLoaded = true;  
    char letterInGNumber; 
    while (getline(*inFile, line)) { 
    fileLine << line; 
    getline(fileLine, tempStudent.lastName, ','); 
    getline(fileLine, tempStudent.firstName, ','); 
    fileLine >> letterInGNumber; 
    fileLine >> tempStudent.gNumber; 
    for (idx = 0; idx <= 11; idx++) { 
     tempStudent.grades[idx] = 0; 
    } 
    studentRecords.push_back(tempStudent); 
    fileLine.flush(); 
    } 
    return isLoaded; 
} 

//this function is trying to take things from a second file, and if the Gnumber(the //long int) is found in the vector then assign values to the grade array 
void loadClassData(vector<student> &studentRecords, ifstream *inFile) { 
    int idx = 0, idxTwo = 0, idxThree = 0; 
    long int tempGNumber = 0; 
    stringstream fileLine; 
    vector<long int> gNumbers; 
    bool numberFound = false; 
    char letterInGNumber; 
    string line; 
    while (getline(*inFile, line)) { 
    idx++; 
    numberFound = false; 
    fileLine << line; 
    fileLine >> letterInGNumber; 
    fileLine >> tempGNumber; 
    for (idxTwo = 0; idxTwo <= studentRecords.size(); idxTwo++) { 
     if (studentRecords[idxTwo].gNumber == tempGNumber) { 
      numberFound = true; 
      break; 
     } 
    } 
    if (numberFound) { 
     for (idxThree = 0; idxThree <= 11; idxThree++) { 
      //fileLine >> studentRecords[idx].grades[idxThree]; 
      /**********here is the problem, I don't know how to assign the grade values******/ 
     } 
    } 
    else { 
     cout << "G Number could not be found!" << endl << endl; 
    } 
    fileLine.flush(); 
    } 
    return; 
} 

誰?お願いします?あなたの代わりに何をすべき

+7

のではなく、あなたのコードを記述しようとすると、ちょうどいくつかの実際の説明のコードを投稿してください。 –

+0

コードが投稿されました –

+0

全体のロジックがオフになっているのですか、正しいトラックにあるのですか –

答えて

1

は、オペレータ>>オーバーロードを定義するだけでそれを読んでいる。例えば、

//assume the following structure when reading in the data from file 
// firstName lastName N n1 n2 n3 ... nN 
ostream& operator>>(ostream& stream, student& s){ 
     stream >> s.firstName; 
     stream >> s.lastName; 
     stream >> s.gNumber 
     for(int i = 0; i < s.gNumber; ++i){ 
     stream >> s.grades[i]; 
     } 
} 

//... in main 
student temp; 
std::vector<student> studentList; 
while(inFile >> temp) studentList.push_back(temp); 
関連する問題