c++ - 交换结构 vector 的内容 c++

标签 c++ vector struct

我有一个结构 vector ,由两个字符串、一个长整数和一个整数数组组成。创建所述结构后,我将数组中的每个元素初始化为 0。我的问题是,我将如何为数组中的每个元素分配不同的值?我尝试使用交换和分配,但它们更多地用于具有两个一维 vector ,而不是二维 vector ,而且我只想在给定时间更改特定结构中的值。请帮忙?谢谢!

如果你想看一些代码,这是我目前所拥有的:

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

有人吗?请?

最佳答案

您应该做的是定义一个运算符 >> 重载并读入它。例如,

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

关于c++ - 交换结构 vector 的内容 c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10375588/

相关文章:

haskell 如何从此代码中删除列表用法?

c++ - 具有不可 move 的默认可构造值类型的 map/unordered_map

c++ - 使用 OpenGL 进行实时逐像素过滤的最佳方法?

C++:将 vector 传输到子程序中

c++ - 使用 vector 中每个对象的函数

c++ - 语句范围 [ c++11 ]

c++ - 获取文件句柄作为参数

C:将 RGB 值的结构写入文件以创建 ppm 图像 - 文件过早结束

c - 存储在结构中的字符串打印不正确

json - 戈朗 : Multiple structs marshall issue: json format