求向量的一个元素,它是在映射中呈现的

Find an element of vector which is presented in the map

本文关键字:映射 元素 向量 一个      更新时间:2023-10-16

我需要找到在地图中呈现的向量元素。困难之处在于vector是由结构体组成的,因此您应该首先调用成员函数从结构体中提取值,以便将其与map元素进行比较。

for循环很简单:

vector<A>::iterator it;
for( it = vec.begin(); it != vec.end(); ++it )
{
    if( mp.count( it->getKey() ) )
    {
        break;
    }
}

我的问题是:有没有办法在一行中完成,比如

//this doesn't work as count accepts key_type
vector<A>::iterator it = find_if( vec.begin(), vec.end(), boost::bind( &map<string, string>::count, mp, boost::bind( &A::getKey, _1 ) )) != 0);

完整示例,用于测试

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <boost/bind.hpp>
#include <boost/assign.hpp>
using namespace std;
class A{
public:
    A( const std::string& key )
    : key( key ) {}
    std::string getKey(){ return key; }
private:
    std::string key;
};
int main(int argc, const char *argv[]) {
    map<string, string> mp = boost::assign::map_list_of( "Key1", "Val1" ) ( "Key2", "Val2" ) ( "Key3", "Val3" );
    vector<A> vec = boost::assign::list_of( "AAA" ) ( "Key2" ) ( "BBB" );
//    vector<A>::iterator it = find_if( vec.begin(), vec.end(), boost::bind( &map<string, string>::count, mp, boost::bind( &A::getKey, _1 ) )) != 0);
    vector<A>::iterator it;
    for( it = vec.begin(); it != vec.end(); ++it )
    {
        if( mp.count( it->getKey() ) )
        {
            break;
        }
    }
    cout << ( it != vec.end() ? "found" : "not found" ) << endl;
    return 0;
}

Thanks in advance

您的解决方案很接近,只是有一个右括号太多了。将每个圆括号放在换行符上,每一层都有缩进,强调无效圆括号:

vector<A>::iterator it = find_if
(
  vec.begin(), vec.end(), boost::bind
  (
    &map<string, string>::count, &mp, boost::bind
    ( 
      &A::getKey, _1 
    )
  )
) // one too many
!= 0);

在其最简单的形式中,这一行变成了iterator = find_if(...) != 0),这将导致编译器失败:

  • 无法找到operator!=(iterator, int)
  • != 0)中的)令牌。

加上正确的括号,!= 0使用boost::bind提供的操作符重载。这一行看起来像:

vector<A>::iterator it = find_if(vec.begin(), vec.end(),
  boost::bind(&map<string, string>::count, &mp,
              boost::bind(&A::getKey, _1)) != 0);

但是,要考虑这样一个简单操作的可读性。如果简单的for循环不够通用和可重用,那么考虑将其隐藏在一个方便的函数中:
template <typename InputIterator,
          typename C,
          typename Fn>
InputIterator find_if_contains(
  InputIterator first,
  InputIterator last,
  const C& container,
  Fn fn)
{
  while (first != last)
  {
    if (0 != container.count(fn(*first))) return first;
    ++first;
  }
  return last;
}
...
vector<A>::iterator it = find_if_contains(
   vec.begin(), vec.end(),
   mp, boost::bind(&A::getKey, _1)
);
否则,自定义谓词类型可以增强可读性,同时为不同类型的重用提供一些额外的灵活性。例如,考虑以下谓词类型,它适用于各种类型的关联容器:
template <typename C,
          typename Fn>
struct contains_predicate
{
  contains_predicate(const C& container, Fn fn)
     : container_(&container), fn_(fn)
  {}
  template <typename T>
  bool operator()(T& t)
  {
    return 0 != container_->count(fn_(t));
  }
  const C* container_;
  Fn fn_;
};
template <typename C,
          typename Fn>
contains_predicate<C, Fn>
contains(const C& container, Fn fn)
{
  return contains_predicate<C, Fn>(container, fn);
}
...
vector<A>::iterator it = find_if(vec.begin(), vec.end(),
  contains(mp, boost::bind(&A::getKey, _1)));

在c++ 11中,使用lambda:

find_if(vec.begin(), vec.end(), [&](A const & a){return mp.count(a.getKey());});

但是因为你在使用Boost。赋值而不是统一初始化,也许你不能那样做。恐怕我不知道如何单独使用bind构造这样的函子