TestDome C++恶意代码分析问题求助:末尾元素替换错误
恶意代码分析模拟程序问题排查
我在练习恶意代码分析相关编程题时,模拟恶意程序的最后一步(将指定位置元素替换为0)遇到瓶颈。程序前序逻辑输出均符合预期,但末尾部分本该输出3, 3, 0,实际却得到0, 0, 0。
以下是我的代码:
#include <iostream> #include <iterator> #include <vector> #include <algorithm> // Update: The problematic is that we need to find a way to not eliminate the first two 77 values. std::vector<int> simulate(const std::vector<int>& entries) { std::vector<int> tempEntries{}; std::vector<int> simEntries{ entries }; for (std::vector<int>::const_iterator itX = entries.begin(); itX != entries.end(); itX++) { std::vector<int>::const_iterator itRightT{}; // Iterator of right T position. itRightT = itX + 4; if (itRightT != entries.end()) { if (*itX <= *itRightT) { tempEntries.push_back(*itX); } } else { break; } } for (std::vector<int>::const_reverse_iterator itReverseX = entries.rbegin(); itReverseX != entries.rend(); itReverseX++) { std::vector<int>::const_reverse_iterator itLeftT{}; // Iterator of left T position. itLeftT = itReverseX + 3; if (itLeftT != entries.rend()) { if (*itReverseX <= *itLeftT) { tempEntries.push_back(*itReverseX); } } else { break; } } std::sort(tempEntries.begin(), tempEntries.end()); for (std::vector<int>::iterator it = simEntries.begin(); it != simEntries.end(); it++) { if (std::find(tempEntries.begin(), tempEntries.end(), *it) != tempEntries.end()) { *it = 0; } } // Desired Result: 1, 0, 0, 5, 0, 0, 0, 3, 3, 0 // Actual Result: 1, 0, 0, 5, 0, 0, 0, 0, 0, 0 return simEntries; } #ifndef RunTests int main() { std::vector<int> result = simulate({ 1, 2, 0, 5, 0, 2, 4, 3, 3, 3 }); for (int value : result) { std::cout << value << " "; } // Expected output // 1, 0, 0, 5, 0, 0, 0, 3, 3, 0 } #endif
我尝试过用std::set_intersection这类STL函数完成元素比较,但它无法直接修改目标容器内的元素,希望能得到解决该问题的帮助。
内容的提问来源于stack exchange,提问作者RexyCode
相关产品推荐
相关产品推荐

