如何搜索对象的容器的数据成员的值? [英] How can I search a container of objects for a data member value?

查看:96
本文介绍了如何搜索对象的容器的数据成员的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象类型如下:

 结构T
{
   INT X;
   布尔ÿ;
};

和他们这样的容器:

 的std ::矢量< T>伏;

和一个强烈的愿望,确定—的在一个语句的—任何元素是否具有Ÿ==真。这可能涉及到的std :: find_if

我的理解是,的std ::绑定的boost ::绑定是成员的功能的,不能适用于成员数据。


由于我不喜欢他们,我想避免:


  • 比较函数/仿函数

  • 循环

由于我的环境是C ++ 03,下面是不可用:


  • lambda表达式


解决方案

  

我的理解是,的std ::绑定的boost ::绑定是成员的功能的,不能适用于成员数据。


值得注意的是,这并非如此! 的boost ::绑定将愉快地绑定到数据成员,并让您的内联重复期间执行操作就可以了。

文档:


  

指针到成员函数指针和数据成员无法正常工作对象,因为它们不支持操作符()。为方便起见,绑定接受成员指针作为第一个参数,并且行为是因为如果的升压::的mem_fn 的已被用于将构件指针转换成一个功能对象。换句话说,前pression

 绑定(安培; X ::楼参数)


  
  

等同于

 绑定< R>(的mem_fn(安培; X :: f)项,参数)

其中的研究是返回类型的 X ::˚F(成员函数)或成员的类型(数据成员)。


另外,从文档的boost ::的mem_fn


  

的mem_fn也被他们当作函数不接受参数并返回一个(常量)参考成员支持指针数据成员。


所以,你可以这样做:

 的#include<矢量>
#包括LT&;串GT;
#包括LT&;&算法GT;
#包括LT&;&iostream的GT;
#包括LT&;升压/ bind.hpp>结构T
{
   INT X;
   布尔ÿ;
};无效F(常量的std ::字符串和放大器;名,常量的std ::矢量< T>&安培; 5)
{
   常量布尔发现=的std :: find_if(
      v.begin(),
      鬻(),
      提高::绑定(& T公司:: Y,_1)==真
   )= v.end()!;   性病::法院LT&;<命名<< :&所述;&下; (发现发现:找不到)<<的'\\ n';
}诠释的main()
{
   T A = {0,}假的;
   T B = {1,}假的;
   T C = {2,真实};   的std ::矢量< T>伏;
   F(一,ⅴ);   v.push_back(一);
   v.push_back(二);
   F(b的,五);   v.push_back(C);
   F(c的,ⅴ);
}

输出:


  

答:没有发现结果
  A:没有发现结果
  C:FOUND


I have an object type like this:

struct T
{
   int x;
   bool y;
};

and a container of them like this:

std::vector<T> v;

and a burning desire to determine — in a single statement — whether any of the elements of v have y == true. This likely involves std::find_if.

My understanding is that std::bind and boost::bind are for member functions and cannot be applied to member data.


Because I dislike them, I wish to avoid:

  • comparison functions/functors
  • loops

Because my environment is C++03, the following are not available:

  • lambdas

解决方案

My understanding is that std::bind and boost::bind are for member functions and cannot be applied to member data.

Remarkably, this is not the case! boost::bind will happily bind to member data and allow you to perform operations on it during an "inline iteration".

From the documentation:

Pointers to member functions and pointers to data members are not function objects, because they do not support operator(). For convenience, bind accepts member pointers as its first argument, and the behavior is as if boost::mem_fn has been used to convert the member pointer into a function object. In other words, the expression

bind(&X::f, args)

is equivalent to

bind<R>(mem_fn(&X::f), args)

where R is the return type of X::f (for member functions) or the type of the member (for data members.)

Also, from the documentation for boost::mem_fn:

mem_fn also supports pointers to data members by treating them as functions taking no arguments and returning a (const) reference to the member.

So, you can do this:

#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <boost/bind.hpp>

struct T
{
   int x;
   bool y;
};

void f(const std::string& name, const std::vector<T>& v)
{
   const bool found = std::find_if(
      v.begin(),
      v.end(),
      boost::bind(&T::y, _1) == true
   ) != v.end();

   std::cout << name << ": " << (found ? " FOUND" : " not found") << '\n';
}

int main()
{
   T a = { 0, false };
   T b = { 1, false };
   T c = { 2, true  };

   std::vector<T> v;
   f("a", v);

   v.push_back(a);
   v.push_back(b);
   f("b", v);

   v.push_back(c);
   f("c", v);
}

Output:

a: not found
b: not found
c: FOUND

这篇关于如何搜索对象的容器的数据成员的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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