如何在C++匹配函数中避免输出重复的候选人匹配结果?
问题:C++匹配求职者与职位时重复输出同一位求职者
我编写了一个C++程序,读取两个文本文件分别存储求职者信息与空缺职位信息并将它们存入数组,二者共享一个数据字段——position。程序中的FindMatches函数负责查找并打印所有匹配项,但当前存在一个问题:当同一职位存在多个空缺时,同一个求职者信息会被重复输出。
相关代码:
#include <iostream> #include <fstream> #include <iomanip> #include <string> #include <sstream> #include <stdlib.h> using namespace std; void FindMatches(string candidates[][6], int cand_size, string openings[][7], int opening_size) { cout <<setw(15) <<"F. NAME:" <<setw(15) <<"L. NAME" <<setw(15)<< "POSITION" <<setw(15)<<"SALARY" << setw(15) << "RATING" << setw(15) << "PHO. #"<< endl << endl; for(int i=0; i<opening_size; i++) { string openingHolder = openings[i][1]; ///placeholder variable for position that matches are being searched for for(int j=0; j<cand_size; j++) { if(candidates[j][2] == openingHolder) { for(int k=0; k<6; k++) cout<<setw(15)<<left<<candidates[j][k]; cout << endl; } } } }
我曾尝试在循环中添加break和continue语句,但要么没有效果,要么导致程序异常;还尝试创建duplicateChecker数组存储已匹配项并进行比对,但未能成功,现求助解决该去重问题。
解决方案1:用标记数组记录已打印的求职者
核心思路是通过一个布尔数组标记每个求职者是否已经被输出过,确保每个求职者只打印一次。修改后我们先遍历求职者,再检查其是否有匹配的空缺职位,避免原逻辑中“多职位→多遍历→多输出”的问题。
修改后的代码:
#include <iostream> #include <fstream> #include <iomanip> #include <string> #include <sstream> #include <stdlib.h> using namespace std; void FindMatches(string candidates[][6], int cand_size, string openings[][7], int opening_size) { cout << setw(15) << "F. NAME:" << setw(15) << "L. NAME" << setw(15) << "POSITION" << setw(15) << "SALARY" << setw(15) << "RATING" << setw(15) << "PHO. #" << endl << endl; // 初始化标记数组,默认所有求职者未被打印 bool printed[cand_size] = {false}; // 遍历每个求职者 for(int j = 0; j < cand_size; j++) { string candidate_pos = candidates[j][2]; bool has_match = false; // 检查该求职者的职位是否存在空缺 for(int i = 0; i < opening_size; i++) { if(openings[i][1] == candidate_pos) { has_match = true; break; // 找到一个匹配职位就停止,无需继续遍历 } } // 有匹配且未被打印时,输出并标记 if(has_match && !printed[j]) { for(int k = 0; k < 6; k++) cout << setw(15) << left << candidates[j][k]; cout << endl; printed[j] = true; } } }
解决方案2:先收集独特空缺职位再匹配
另一种思路是先从空缺职位列表中提取所有不重复的职位,再遍历求职者,只要其职位在独特列表中就输出一次,从根源避免重复触发输出逻辑。
代码示例:
#include <iostream> #include <fstream> #include <iomanip> #include <string> #include <sstream> #include <stdlib.h> #include <vector> using namespace std; void FindMatches(string candidates[][6], int cand_size, string openings[][7], int opening_size) { cout << setw(15) << "F. NAME:" << setw(15) << "L. NAME" << setw(15) << "POSITION" << setw(15) << "SALARY" << setw(15) << "RATING" << setw(15) << "PHO. #" << endl << endl; // 收集所有不重复的空缺职位 vector<string> unique_positions; for(int i = 0; i < opening_size; i++) { string current_pos = openings[i][1]; bool exists = false; // 检查职位是否已在列表中 for(string pos : unique_positions) { if(pos == current_pos) { exists = true; break; } } if(!exists) unique_positions.push_back(current_pos); } // 遍历求职者,匹配独特职位列表 for(int j = 0; j < cand_size; j++) { string candidate_pos = candidates[j][2]; for(string pos : unique_positions) { if(candidate_pos == pos) { for(int k = 0; k < 6; k++) cout << setw(15) << left << candidates[j][k]; cout << endl; break; // 匹配成功后停止,避免重复输出 } } } }
内容的提问来源于stack exchange,提问作者Michael Werle
相关产品推荐
相关产品推荐

