|
- #include <vector>
- #include <unordered采用set>
- #include <algorithm>
- struct VectorHash {
- size采用t operator()(const std::vector<double>& vec) const {
- size采用t hash = 0;
- for (double val : vec) {
- hash ^= std::hash<double>{}(val) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
- }
- return hash;
- }
- };
- bool equalVectors(const std::vector<double>& v1, const std::vector<double>& v2) {
- if (v1.size() != v2.size()) return false;
- std::sort(v1.begin(), v1.end());
- std::sort(v2.begin(), v2.end());
- return v1 == v2;
- }
- std::vector<std::vector<double>> removeDuplicateRows(const std::vector<std::vector<double>>& matrix) {
- std::unordered采用set<std::vector<double>, VectorHash, decltype(equalVectors)*> uniqueRows(&equalVectors);
- std::vector<std::vector<double>> result;
- for (const auto& row : matrix) {
- if (uniqueRows.find(row) == uniqueRows.end()) {
- // 如果当前行不在哈希集合中,则添加到结果向量和哈希集合中
- result.push采用back(row);
- uniqueRows.insert(row);
- }
- }
- return result;
- }
- int main() {
- std::vector<std::vector<double>> matrix = {{1.0, 2.0, 2.0, 3.0},
- {1.0, 2.0, 3.0, 1.0},
- {3.0, 2.0, 2.0, 1.0},
- {4.0, 4.0, 5.0},
- {6.0, 6.0, 7.0, 7.0, 7.0}};
- std::vector<std::vector<double>> result = removeDuplicateRows(matrix);
- // 输出结果
- for (const auto& row : result) {
- for (double val : row) {
- std::cout << val << " ";
- }
- std::cout << std::endl;
- }
- return 0;
- }
复制代码 |
|