0

I have a vector of structs consisting of two strings, a long integer, and an array of integers. Upon creation of said structs, I initialize each element in the array to 0. My question is, How would I go about assigning different values to each of the elements in the array? I tried to use swap and assign, but they are more for having two 1 dimensional vectors, not 2 dimensional ones, and I only want to change the values in a certain struct at a given time. Please help? Thanks!

If you'd like to see some code, this is what I have so far:

//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;
}

anyone? please?

4

1 回答 1

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);
于 2012-04-30T05:19:22.887 回答