gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
write a c++ function to convert each array in a 2d array of integers into a hashmaps…
- User
write a c++ function to convert each array in a 2d array of integers into a hashmaps with the values of the arrays as keys and set of the indexes of the keys in the array as the value of each key
- Assistant
Here's the implementation of the function:
code#include<unordered_map> #include<vector> void convertToHashmaps(std::vector<std::vector<int>>& arr) { for(auto& row : arr) { std::unordered_map<int, std::vector<int>> mp; for(int i=0; i<row.size(); i++) { mp[row[i]].push_back(i); } row.clear(); for(auto& entry : mp) { row.push_back(entry.first); row.insert(row.end(), entry.second.begin(), entry.second.end()); } } }