如何在std :: set中找到具有特定字段值的对象? [英] How to find an object with specific field values in a std::set?

查看:287
本文介绍了如何在std :: set中找到具有特定字段值的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调用一个方法返回 std :: set< T> const& 其中 T 是类类型。我想实现的是检查集合是否包含类型 T 的对象与自动化测试中的断言的特定字段值。



这里有一个简单的例子:
让类型 T Car 所以一个例子设置包含一堆汽车。现在,我想要找到一种特定颜色特定门数的特定最高速度的汽车。如果找到 汽车,则第一个断言为true,并且应该找到具有其他字段值的下一个汽车。



我不允许更改 T 的实现。

解决方案

>

这取决于 T 的实现。让我们坚持你的例子 Car 。假设类看起来像这样:

  class Car {
public:
Car string color,unsigned int number_of_doors,
unsigned int top_speed);
//所有这些属性的getter
// operator<根据std :: set
}的要求;

运算符< code> Car 基于所有属性,以便搜索所有可能的属性。否则你会得到不正确的结果。



所以基本上,你可以使用这些属性构造一个汽车的实例。在这种情况下,您可以使用 std :: set :: find 并提供 Car 的临时实例您正在寻找:

  car_set.find(Car(green,4,120)); 

如果要搜索 Car 只指定其属性的子集,像所有绿色汽车,您可以使用 std :: find_if 和自定义谓词:

  struct find_by_color {
find_by_color(const std :: string& color):color(color){}
bool operator & car){
return car.color == color;
}
private:
std :: string color;
};

//在你的代码中

std :: set< Car> :: iterator result = std :: find_if(cars.begin(),cars.end
find_by_color(green));
if(result!= cars.end()){
//我们发现了一些东西
}
else {
//没有匹配
}请注意,第二个解决方案具有线性复杂性,因为它不能依赖于任何可能存在或可能不存在的排序。为您使用的谓词。然而,第一个解决方案具有对数复杂性,因为它可以受益于 std :: set 的顺序。



如果,如@Betas对你的问题的评论,你想在运行时组成谓词,你必须写一些帮助类来组成不同的谓词。


I call a method which returns std::set<T> const& where T is a class type. What I'm trying to achieve is to check whether the set contains an object of type T with specific field values for an assertion in a automated test. This check should be done for multiple objects.

Here is a simple example: Let the type T be Car so an example set contains a bunch of cars. Now I want to find a car with a specific color and a specific number of doors and a specific top speed in that set. If that car is found the first assertion is true and the next car with other field values should be found.

I'm not allowed to change the implementation of T. The usage of Boost would be OK.

How would you do that?

解决方案

This depends on the implementation of T. Let's stick with your example of a class Car. Suppose that class looks something like this:

class Car {
public:
    Car(std::string color, unsigned int number_of_doors,
        unsigned int top_speed);
    // getters for all these attributes
    // implementation of operator< as required for std::set
};

The operator< should order instances of Car based on all attributes in order to make searching for all attributes possible. Otherwise you will get incorrect results.

So basically, you can construct an instance of car using just these attributes. In that case, you can use std::set::find and supply a temporary instance of Car with the attributes you are looking for:

car_set.find(Car("green", 4, 120));

If you want to search for an instance of Car specifying only a subset of its attributes, like all green cars, you can use std::find_if with a custom predicate:

struct find_by_color {
    find_by_color(const std::string & color) : color(color) {}
    bool operator()(const Car & car) {
        return car.color == color;
    }
private:
    std::string color;
};

// in your code

std::set<Car>::iterator result = std::find_if(cars.begin(), cars.end(), 
                                              find_by_color("green"));
if(result != cars.end()) {
    // we found something
}
else {
    // no match
}

Note that the second solution has linear complexity, because it cannot rely on any ordering that may or may not exists for the predicate you use. The first solution however has logarithmic complexity, as it can benefit from the order of an std::set.

If, as suggested by @Betas comment on your question, you want to compose the predicates at runtime, you would have to write some helper-classes to compose different predicates.

这篇关于如何在std :: set中找到具有特定字段值的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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