C ++ STL:std :: find with std :: map [英] C++ STL: std::find with std::map

查看:149
本文介绍了C ++ STL:std :: find with std :: map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python允许你编写如果e在arr中:... 如果在dict中输入密钥:.. 。这很方便。

Python allows you to write if e in arr: ... and if key in dict: ... which is handy.

我们可以使用 std :: find() std :: map ?这将允许我使用单个泛型函数统一处理 std :: array std :: map ,而无需明确处理切换到 std :: map :: find()

Can we do something similar with the latter using std::find() and std::map ? That will allow me to uniformly handle std::array and std::map with a single generic function, without explicitly switching to std::map::find().

但是如果重载运算符==()是唯一的方法,我宁愿放弃这个想法...

But if overloading operator==() is the only way, I'd rather give up this idea...

通过重载运算符==()我的意思是这样的:

By "overloading operator==()" I meant something like this:

template<typename K>
struct KF {
    K&& k;

    template <typename V>
    friend bool operator==(const typename std::pair<const K, V>& pair, const KF<K>& o) {
        return pair.first == o.k;
    }

};

template <typename K>
KF<K> keyFinder(K&& k) { return KF<K>{ std::forward<K>(k) }; }

int main() {
    std::set<int> s{ 1, 2, };
    cout << (std::find(s.begin(), s.end(), 1) == s.end()) << endl; // => 0
    cout << (std::find(s.begin(), s.end(), 3) == s.end()) << endl; // => 1

    std::map<int, int> m{ {1,10}, {2,20}, };
    cout << (std::find(m.begin(), m.end(), keyFinder(1)) == m.end()) << endl; // => 0
    cout << (std::find(m.begin(), m.end(), keyFinder(3)) == m.end()) << endl; // => 1
}




  • http://ideone.com/7ULUe9

    • http://ideone.com/7ULUe9
    • 当我们以通用方式处理非标量 K 时,事情会变得更复杂(完美转发等。?)

      Things get more complicated when we deal with non-scalar K in an universal way (perfect forwarding etc. ?)

      推荐答案

      ...为什么不编写自己的效用函数?

      ...why not write your own utility function?

      template <typename TContainer, typename TValue>
      bool contains(const TContainer& c, const TValue& x);
      

      您可以使用重载来匹配容器:

      You can use overloading to match the containers:

      template <typename TValue, std::size_t N>
      bool contains(const std::array<TValue, N>& c, const TValue& x)
      {
          return std::find(std::begin(c), std::end(c), x) != std::end(c);
      }
      
      template <typename TValue, typename... Ts>
      bool contains(const std::map<Ts...>& c, const TValue& x)
      {
          return c.find(x) != std::end(c);
      }
      

      用法:

      std::array<int, 2> a{1,2};
      std::map<int, int> b{{1,2},{3,4}};
      
      assert(contains(a, 1));
      assert(!contains(a, 42));
      assert(contains(b, 1));
      assert(!contains(b, 42));
      

      wandbox上的实时示例

      如果你想要将来支持其他容器,最好使用 SFINAE 来检查特定表达式是否有效。这种方法效果很好,因为它不关心容器的类型,它只关心可以对它执行什么操作。

      If you want to support additional containers in the future, it's a good idea to use SFINAE to check whether or not a particular expression is valid. This approach works well because it doesn't care about the type of the container, it only cares about what operations can be performed on it.

      检测成语 可能会很容易通过SFINAE检查会员的可用性(它的实现与C ++ 11兼容)

      我还写了一篇关于检查表达式有效性的文章原位使用C ++ 17,这可能是一个有趣的读物。尽管它有标题,但它涵盖了检查表达式有效性的C ++ 11,C ++ 14和C ++ 17技术:

      I also wrote an article about checking expression validity in-situ with C++17, which could be an interesting read. Despite its title, it covers C++11, C++14 and C++17 techniques to check expression validity:

      使用C ++ 17 就地检查表达式有效性

      "checking expression validity in-place with C++17"

      这篇关于C ++ STL:std :: find with std :: map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆