Untitled
unknown
plain_text
a year ago
1.1 kB
8
Indexable
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int main() {
// Khai báo unordered_map với key là int và value là vector của pair<int,int>
unordered_map<int, vector<pair<int,int>>> mp;
// Tạo ma trận 2D ví dụ
vector<vector<int>> matrix = {
{1, 2, 3, 1},
{4, 1, 5, 2},
{6, 2, 1, 3}
};
int n = matrix[0].size(); // Số cột trong ma trận
// Duyệt qua ma trận và cập nhật unordered_map
for (int row = 0; row < matrix.size(); ++row) {
for (int col = 0; col < n; ++col) {
int key = matrix[row][col];
// Thêm vị trí hiện tại vào vector của key tương ứng
mp[key].push_back({row, col});
}
}
// In kết quả
for (const auto& pair : mp) {
cout << "Giá trị " << pair.first << " xuất hiện tại các vị trí: ";
for (const auto& position : pair.second) {
cout << "(" << position.first << "," << position.second << ") ";
}
cout << endl;
}
return 0;
}Editor is loading...
Leave a Comment