3 ms·
If you want to store an object that implements operator< but doesn't have a hash function. While working on a leetcode problem I stored vector<int> in a set<vec
by OnionBlender 4y ago
If you want to store an object that implements operator< but doesn't have a hash function. While working on a leetcode problem I stored vector<int> in a set<vector<int>> instead of unordered_set<vector<int>> in order to eliminate duplicates.
- sicp-enjoyer 4y agoDo you know about std::unique? Generally sets are a poor tool for removing duplicates.
- pixelesque 4y agoDoesn't std::unique only generally work on consecutive items, and effectively requires the items to already be in a container (or at the very least have access to iterators to the items)? If so, that sounds fairly limiting to me (i.e. potential extra storage - of duplicates, sorting required, etc).
- sicp-enjoyer 4y agoThis person is already putting sets inside vectors, so I don't think they are worried about storage. But yes, you either need to stop occasionally and remove the duplicates or use a set, if the storage is prohibitive. Otherwise in terms of performance, sorting and unique is much faster than inserting one element at a time in a set.