unordered_set构造 count_if函数

 给你一个字符串 jewels 代表石头中宝石的类型,另有一个字符串 stones 代表你拥有的石头。 stones 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。

字母区分大小写,因此 "a" 和 "A" 是不同类型的石头。

class Solution {
public:
    int numJewelsInStones(string j, string s) {
        unordered_set us(begin(j), end(j));
        return count_if(begin(s), end(s), [&](char c) { return us.count(c); });  
    }
};

// class Solution {
// public:
//     int numJewelsInStones(string jewels, string stones) {
//         int res = 0; 
//         unordered_set<int> sett;
//         for(int i = 0; i<jewels.size(); i++){
//             sett.insert(jewels[i]);
//         }
//         for(int i = 0; i<stones.size(); i++){
//             if(sett.count(stones[i]))
//                 res++;
//         }
//         return res;
//     }
// };
原文地址:https://www.cnblogs.com/qianxunslimg/p/15674251.html