#include\x3ciostream\x3e\n#include\x3cvector\x3e\n#include\x3cunordered_map\x3e\n#include\x3cfstream\x3e\n\n///定义一个函数,获得每一行不为0的行的列名,将结果存储在一个unordered_map中,unordered_map的键名为样本名,值为行不为0的列名\nstd::vector\x3cstd::string\x3e split_a(const std::string& s, const std::string& delimiter) {\n std::vector\x3cstd::string\x3e tokens;\n size_t pos = 0;\n std::string token;\n std::string str = s;\n while ((pos = str.find(delimiter)) != std::string::npos) {\n token = str.substr(0, pos);\n tokens.push_back(token);\n str.erase(0, pos + delimiter.length());\n }\n tokens.push_back(str);\n return tokens;\n}\n\n/// 创建了一个processMatrix函数,用于处理文件并返回一个分组映射。\nstd::unordered_map\x3cstd::string, std::vector\x3cstd::string\x3e\x3e processMatrix(std::ifstream& file) {\n std::unordered_map\x3cstd::string, std::vector\x3cstd::string\x3e\x3e groups;\n std::string line;\n getline(file, line);\n std::vector\x3cstd::string\x3e colNames = split_a(line, "\t");\n\n while (getline(file, line)) {\n std::vector\x3cstd::string\x3e rowTokens = split_a(line, "\t");\n std::string rowName = rowTokens[0];\n for (size_t i = 1; i \x3c rowTokens.size(); i++) {\n double value = std::stod(rowTokens[i]);\n if (value != 0) {\n groups[rowName].push_back(colNames[i]);\n }\n }\n }\n return groups;\n}\n\n///定义一个函数,将行不为0的列的列名在节点的名字中查询,返回每个样本中在节点中存在的列名\n///结果储存在unordered_map中,通过for循环获得键名(first)和值(second)\nstd::unordered_map\x3cstd::string, std::vector\x3cint\x3e\x3e groupIndex(std::unordered_map\x3cstd::string, std::vector\x3cstd::string\x3e\x3e groups, std::vector\x3cstd::string\x3e node_names)\n{\n std::unordered_map\x3cstd::string, int\x3e name_to_index; /// 用于存储名称到索引的映射\n std::unordered_map\x3cstd::string, std::vector\x3cint\x3e\x3e group_need; /// 将要存储索引位置的map\n\n /// 填充名称到索引的映射\n size_t size = node_names.size();\n for(size_t i = 0; i \x3c size; ++i)\n {\n name_to_index[node_names[i]] = i;\n }\n\n for(const auto& group : groups)\n {\n for(const auto& name : group.second)\n {\n if(name_to_index.find(name) != name_to_index.end())\n {\n group_need[group.first].push_back(name_to_index[name]);\n }\n }\n }\n\n return group_need;\n}\n\n给出的代码似乎不对\n\n我注意到您提到代码似乎有问题,但没有提供具体的错误信息或预期的结果。请提供更多信息,以便我可以帮助您解决问题。

C++ 优化 groupIndex 函数:跳过不存在于 nodes_names 中的值

原文地址: https://www.cveoy.top/t/topic/qgWF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录