gpt4 book ai didi

boost - 找到 map 中显示的 vector 元素

转载 作者:行者123 更新时间:2023-11-28 07:37:21 24 4
gpt4 key购买 nike

我需要找到 map 中显示的 vector 元素。困难的部分是 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;
}

提前致谢

最佳答案

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

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)
  • ) token 在!= 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)
);

否则,自定义谓词类型可能会 boost 可读性,同时为不同类型的重用提供一些额外的灵 active 。例如,考虑以下适用于各种类型的关联容器的谓词类型:

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)));

关于boost - 找到 map 中显示的 vector 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16520833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com