Abuse

I have learned abusing auto and auto&& in C++!!!!!!!!!

我学会了怎么滥用 autoauto&& !!!

class Solution {
public:
    static auto topKFrequent(const std::vector<std::string>& words, int k) -> std::vector<std::string> {
        std::unordered_map<std::string, int> m;
        std::for_each(words.begin(), words.end(), [&](auto&& x) { ++m[x]; });

        auto cmp = [](auto&& x, auto&& y) {
            return (x.second != y.second) ? x.second > y.second : x.first < y.first;
        };

        using PSI = std::pair<std::string, int>;
        std::priority_queue<PSI, std::vector<PSI>, decltype(cmp)> q;
        std::for_each(m.begin(), m.end(), [&](auto&& x) {
            q.push(x);
            if (q.size() > k)
                q.pop();
        });

        std::vector<std::string> ret;
        ret.reserve(k);
        while (!q.empty()) {
            ret.push_back(q.top().first);
            q.pop();
        }

        return ret;
    }
};

来和我一起滥用吧!!哈哈哈哈哈哈哈哈哈哈哈哈


Last modified on 2022-10-20