按相同名称对动态数组中的元素进行排序

问题描述

我有一个用 C++ 编写的程序,该程序将生成具有相同教师姓名的文件,这些文件将按时间顺序(按天和小时)另外排序:

<hour> <day> <group> <surname> <subject>

(这里是example.txt内容):

10:15-11:30 Friday gr1 Smith Programming
07:10-09:15 Wednesday gr2 Taylor InternetofThings
11:00-12:00 Monday gr2 Smith Java
10:20-11:45 Thursday gr1 Taylor Matchematic

工作后,程序生成文件

Smith.txt :

11:00-12:00 Monday gr2 Java
10:15-11:30 Friday gr1 Programming

Taylor.txt :

07:10-09:15 Wednesday gr2 InternetofThings
10:20-11:45 Thursday gr1 Matchematic

我已经设法将数据从 txt 文件加载到动态数组中(下面的代码)。我不知道如何进行名称搜索和排序(名称可以不同,行数也可以不同)。我正在考虑一个循环,从动态数组的“姓氏”变量中查找相同的字母,但我不知道如何实现它。

struct Line {
  string hour;
  string day;
  string group;
  string surname;
  string subject; 
};

void readLine(ifstream& file,Line& line) {
  file >> line.hour >> line.day >> line.group >> line.surname >> line.subject;
}

void readLineTab(ifstream& file,Line* lineTab,const int numOfLines) {
  for (int i = 0; i < numOfLines; i++) {
    readLine(file,lineTab[i]); 
  }
}

void printLine(const Line& line) {
  cout << line.hour << " " << line.day << " " << line.group << " " << line.surname << " " << 
  line.subject << endl;
}

void printLineTab(Line* lineTab,const int size) {
  for (int i = 0; i < size; i++) {
    printLine(lineTab[i]);
  }
}

int checkFile(string& filePath,int& numOfLines) {
  ifstream file;
  file.open(filePath.c_str());
  if (file.fail()) {
    cerr << "Error file open: " << filePath << endl;
    file.close();
    return 1;
  }
  string line;
  int lineNr = 0;
  while (getline(file,line)) {
    lineNr++;
    numOfLines++;
  }
  file.close();
  return 0;
}

int main(int argc,char** argv) {
  int numOfLines = 0; 
  ifstream file; 
  string filePath = "example.txt"; 

  if (checkFile(filePath,numOfLines)) {
    return 1;
  }

  Line* lineTab = new Line[numOfLines];

  file.open(filePath.c_str());
  if (file.fail()) {
    cerr << "Error file open: " << filePath << endl;
    return 1;
  }

  readLineTab(file,lineTab,numOfLines);
  printLineTab(lineTab,numOfLines);

  delete[] lineTab;
  file.close();
  return 0;
}

解决方法

一种可能的方法是按姓氏对数组进行排序,然后是天,然后是小时。但是要按天排序,您必须将字符串转换为可排序的索引,例如从周一的 1 到周六的 6。

我会对您的代码进行以下添加:

week_index 结构中添加一个 Line 成员,并为转换构建一个 map

struct Line {
    string hour;
    string day;
    int week_index;
    string group;
    string surname;
    string subject;
};

std::map<std::string,int> weekdays = {
    {"Monday",1},{"Tuesday",2},{"Wednesday",3},{"Thursday",4},{"Friday",5},{"Saturday",6}
};

void readLine(ifstream& file,Line& line) {
    file >> line.hour >> line.day >> line.group >> line.surname >> line.subject;
    line.week_index = weekdays[line.day];
}

添加比较函数:

bool comp(const Line& first,const Line& second) {
    if (first.surname < second.surname) {
        return true;
    }
    if (first.surname > second.surname) {
        return false;
    }
    if (first.week_index < second.week_index) {
        return true;
    }
    if (first.week_index > second.week_index) {
        return false;
    }
    if (first.hour < second.hour) {
        return true;
    }
    return false;
}

最后,对数组进行排序并将其写入相关文件:

...
readLineTab(file,lineTab,numOfLines);
printLineTab(lineTab,numOfLines);

std::sort(lineTab,lineTab + numOfLines,comp);

string surname = "";
std::ofstream ofs;
for (const Line* line = lineTab; line < lineTab + numOfLines; line++) {
    if (line->surname != surname) {
        if (ofs.is_open()) ofs.close();
        ofs.open(line->surname + std::string(".txt"));
        surname = line->surname;
    }
    ofs << line->hour << " " << line->day << " " << line->group << " " << line->subject << '\n';
}
ofs.close();

delete[] lineTab;
...