Efficient way to build a frequency map from a large vector?
I currently use the following implementation to build a frequency map from a large vector. For each value, I attempt to insert a (value, 1) pair into the map. If the insertion fails because the key is already present, I increment the existing counter instead. Are there any optimizations or alternative approaches? #include <iostream> #include <random> #include <vector> #include <chrono> #include <map> template <typename T> void freqmap (const std::vector<T>& v, std::map<T, int>& m) { m.clear (); if (v.empty ()) return; auto i (v.begin ()); std::pair<T, int> p_; p_.second = 1; std::pair<typename
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.