0
我有一個包含兩個字符串,一個長整數和一個整數數組的結構向量。創建所述結構體時,我將數組中的每個元素初始化爲0.我的問題是,我將如何去爲數組中的每個元素分配不同的值?我試圖使用交換和分配,但它們更多的是具有兩個1維矢量,而不是2維矢量,並且我只想在給定時間更改某個結構中的值。請幫忙?謝謝!結構向量的交換內容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;
}
的人?請?
而不是試圖描述你的代碼,請只是張貼一些實際的說明代碼。 –
代碼已發佈 –
是我的邏輯關閉整個事情,或者我在正確的軌道 –