Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按相同名称对动态数组中的元素进行排序 我有一个C++程序,它将生成具有相同教师姓名的文件,这些文件将按时间顺序(按时间和小时)另外排序:_C++_Sorting_Struct_Dynamic Arrays_Fromfile - Fatal编程技术网

按相同名称对动态数组中的元素进行排序 我有一个C++程序,它将生成具有相同教师姓名的文件,这些文件将按时间顺序(按时间和小时)另外排序:

按相同名称对动态数组中的元素进行排序 我有一个C++程序,它将生成具有相同教师姓名的文件,这些文件将按时间顺序(按时间和小时)另外排序:,c++,sorting,struct,dynamic-arrays,fromfile,C++,Sorting,Struct,Dynamic Arrays,Fromfile,工作后,程序生成文件: 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文件加载到动态数组中(下面的代码)。我不知道如何进行名称搜索和排序(名称可以不同,行数也可以不同)。我在考虑

工作后,程序生成文件:

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;
}
struct行{
弦时;
弦日;
字符串组;
串姓;
字符串主题;
};
void readLine(ifstream&file,Line&Line){
文件>>line.hour>>line.day>>line.group>>line.姓氏>>line.subject;
}
void readLineTab(ifstream&file,Line*lineTab,const int numOfLines){
对于(int i=0;icout一种可能的方法是按姓氏,然后按天,然后按小时对数组进行排序。但要按天排序,必须将字符串转换为可排序索引,例如从周一的1到周六的6

我将对您的代码添加以下内容:

结构中添加
周索引
成员,并为转换构建
映射

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];
}
struct行{
弦时;
弦日;
国际周指数;
字符串组;
串姓;
字符串主题;
};
标准::地图工作日={
{“星期一”,1},
{“星期二”,2},
{“星期三”,3},
{“星期四”,4},
{“星期五”,5},
{“星期六”,6}
};
void readLine(ifstream&file,Line&Line){
文件>>line.hour>>line.day>>line.group>>line.姓氏>>line.subject;
line.week_index=工作日[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;
}
bool comp(常数行和第一行,常数行和第二行){
if(第一姓<第二姓){
返回true;
}
如果(第一个姓氏>第二个姓氏){
返回false;
}
if(第一周指数<第二周指数){
返回true;
}
如果(第一周指数>第二周指数){
返回false;
}
如果(第一小时<第二小时){
返回true;
}
返回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;
...
。。。
readLineTab(文件、lineTab、numOfLines);
printLineTab(lineTab,numOfLines);
std::sort(lineTab,lineTab+numOfLines,comp);
字符串姓氏=”;
std::ofs流;
对于(常量行*行=行选项卡;行<行选项卡+多行;行++){
如果(行->姓氏!=姓氏){
如果(ofs.is_open())ofs.close();
open(行->姓氏+std::字符串(“.txt”);
姓氏=行->姓氏;
}

在一小时内,您可能需要一个
std::map
std::unordered\u map
来完成此操作,但请注意,这些的键字符串必须是唯一的。如果不是唯一的,则需要一个
std::multimap
。您可以在此处找到这些类的文档:您是否被限制使用标准库中的容器类?简单方法是只读取一行,检查教师的文件是否存在,如果不存在,则创建一个。如果确实存在,则将数据附加到文件中。std::string.find(str2)是用于在str中查找str2的工具。当您要按天排序时,应将天名称转换为可排序的周索引(星期一=1,…,星期六=6)。然后按1/姓氏、2/周索引和3/小时对数组进行排序。完成后,只需写入排序后的数组,如果姓氏已更改,则打开一个新文件(以空姓氏开始,在第一条记录上创建一个新文件).BTW,如果您可以使用
std::map
容器,那么实现起来会更容易。非常感谢,它可以工作。我还有一个问题,您可以不使用std::map on vectors来实现吗?@VertcoreDesign:我在这里使用map,因为我不想在数组中实现线性搜索,但它可以被结构的普通数组替代(
struct{const char*day\u name;int week\u day;};
)。您可以表示是否要使用此数组而不是使用map,因为我尝试了,但有些东西不起作用。
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;
...