如何比较vector元素与vector of vectors元素?代码异常求助
问题描述与代码修复
需求
- 将输入的字符对存入向量
temp_pairs后,检查该对的任意元素是否存在于二维向量jumpers的任意元素中 - 若存在任一元素匹配,则调用
.clear()清空temp_pairs,不将该对加入jumpers
当前问题
代码仅前两对数据无法正常执行检查逻辑,后续数据可正常运行
尝试方案
已尝试多种if语句和循环结构,当前代码是能实现的最佳效果
相关代码
#include <iostream> #include <vector> #include <cmath> #include <string> #include <algorithm> #include <cctype> using namespace std; typedef std::vector<int> vector_i; typedef std::vector<char> vector_c; vector_c control {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','x','w','y','z',' ','.'}; std::vector<vector_c> jump_input(); int main() { jump_input(); return 0; } std::vector<vector_c> jump_input() { std::vector<vector_c> jumpers; vector_c temp_pairs(2); int jump_count = 0; char in; vector_c pair; do { for (int i = 0; i < jumpers.size(); i++) { for (int j = 0; j < jumpers[i].size(); j++) { std::cout << jumpers[i][j]; } std::cout << " "; } for (int i = 0; i < 2; i++) { std::cout << "\n" << "Enter the " << i+1 << " of the pair number "<< jumpers.size()+1 << "\n"; std::cin >> in; for (int j = 0; j < control.size(); j++) { if (in == control[j]) { pair.push_back(in); } } } if (pair.size()>1) { for (int i = 0; i < 2; i++) { for (int j = 0; j < control.size(); j++) { if (pair[i] == control[j]) { temp_pairs.push_back(pair[i]); } } } } // where the comparation happens for (int i = 0; i < jumpers.size(); i++) { for (int j = 0; j < 2; j++) { if (jumpers[i][j] == temp_pairs[j]) { temp_pairs.clear(); } } } if (!temp_pairs.empty()) { jumpers.push_back(temp_pairs); jump_count += 1; } temp_pairs.clear(); pair.clear(); in='\0'; } while (jump_count<21); return jumpers; }
问题分析与修复代码
问题根源
temp_pairs初始化为vector_c temp_pairs(2),默认生成两个空字符('\0'),后续调用push_back会让temp_pairs长度变为4,对比时取的是前两个空字符而非输入的合法字符,导致前两次循环的检查逻辑完全错误- 对比逻辑错误:原代码仅对比
jumpers[i][j]和temp_pairs[j](对应位置匹配),不符合“任一元素与jumpers中任意元素相等”的需求
修复后的代码
#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef vector<int> vector_i; typedef vector<char> vector_c; vector_c control {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ','.'}; vector<vector_c> jump_input(); int main() { jump_input(); return 0; } vector<vector_c> jump_input() { vector<vector_c> jumpers; vector_c temp_pairs; // 改为空向量初始化 int jump_count = 0; char in; vector_c pair; do { // 打印已有的jumpers内容 for (const auto& p : jumpers) { for (char c : p) { cout << c; } cout << " "; } // 输入两个合法字符 for (int i = 0; i < 2; i++) { cout << "\n输入第" << jumpers.size()+1 << "对的第" << i+1 << "个字符:\n"; cin >> in; // 用find简化合法字符检查 if (find(control.begin(), control.end(), in) != control.end()) { pair.push_back(in); } } // 仅当输入两个合法字符时,填充temp_pairs if (pair.size() == 2) { temp_pairs = pair; // 直接赋值,避免push_back导致的长度问题 } // 检查temp_pairs的任意元素是否存在于jumpers的所有元素中 if (!temp_pairs.empty()) { bool has_match = false; // 遍历jumpers中的每一对 for (const auto& existing_pair : jumpers) { // 检查当前temp_pairs的第一个元素是否在现有对中 if (find(existing_pair.begin(), existing_pair.end(), temp_pairs[0]) != existing_pair.end()) { has_match = true; break; } // 检查当前temp_pairs的第二个元素是否在现有对中 if (find(existing_pair.begin(), existing_pair.end(), temp_pairs[1]) != existing_pair.end()) { has_match = true; break; } } if (has_match) { temp_pairs.clear(); } } // 若temp_pairs非空则加入jumpers if (!temp_pairs.empty()) { jumpers.push_back(temp_pairs); jump_count++; } // 清空临时变量 temp_pairs.clear(); pair.clear(); } while (jump_count < 21); return jumpers; }
修复说明
- 将
temp_pairs改为空向量初始化,通过直接赋值temp_pairs = pair确保其长度始终为2,避免初始空字符干扰 - 用
std::find替代循环检查合法字符,代码更简洁高效 - 重构对比逻辑:遍历
jumpers中所有已有字符对,检查当前输入的两个字符是否有任意一个出现在已有字符对中,符合需求逻辑 - 优化打印逻辑,使用范围for循环提升可读性
内容的提问来源于stack exchange,提问作者Walber José Neves
相关产品推荐
相关产品推荐

