1. <i id='IPnvH'><tr id='IPnvH'><dt id='IPnvH'><q id='IPnvH'><span id='IPnvH'><b id='IPnvH'><form id='IPnvH'><ins id='IPnvH'></ins><ul id='IPnvH'></ul><sub id='IPnvH'></sub></form><legend id='IPnvH'></legend><bdo id='IPnvH'><pre id='IPnvH'><center id='IPnvH'></center></pre></bdo></b><th id='IPnvH'></th></span></q></dt></tr></i><div id='IPnvH'><tfoot id='IPnvH'></tfoot><dl id='IPnvH'><fieldset id='IPnvH'></fieldset></dl></div>
    2. <tfoot id='IPnvH'></tfoot>
        <bdo id='IPnvH'></bdo><ul id='IPnvH'></ul>

        <legend id='IPnvH'><style id='IPnvH'><dir id='IPnvH'><q id='IPnvH'></q></dir></style></legend>

        <small id='IPnvH'></small><noframes id='IPnvH'>

        如何在 std::set 中查找具有特定字段值的对象?

        时间:2024-08-14
        <i id='2Kyj1'><tr id='2Kyj1'><dt id='2Kyj1'><q id='2Kyj1'><span id='2Kyj1'><b id='2Kyj1'><form id='2Kyj1'><ins id='2Kyj1'></ins><ul id='2Kyj1'></ul><sub id='2Kyj1'></sub></form><legend id='2Kyj1'></legend><bdo id='2Kyj1'><pre id='2Kyj1'><center id='2Kyj1'></center></pre></bdo></b><th id='2Kyj1'></th></span></q></dt></tr></i><div id='2Kyj1'><tfoot id='2Kyj1'></tfoot><dl id='2Kyj1'><fieldset id='2Kyj1'></fieldset></dl></div>

        • <bdo id='2Kyj1'></bdo><ul id='2Kyj1'></ul>

          1. <tfoot id='2Kyj1'></tfoot>
              <tbody id='2Kyj1'></tbody>

            <small id='2Kyj1'></small><noframes id='2Kyj1'>

                <legend id='2Kyj1'><style id='2Kyj1'><dir id='2Kyj1'><q id='2Kyj1'></q></dir></style></legend>
                • 本文介绍了如何在 std::set 中查找具有特定字段值的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我调用一个返回 std::set 的方法;const& 其中 T 是类类型.我想要实现的是检查该集合是否包含类型为 T 的对象,该对象具有自动化测试中断言的特定字段值.应该对多个对象进行此检查.

                  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.

                  这是一个简单的例子:假设类型 TCar 所以一个例子 set 包含一堆汽车.现在我想在该集合中找到具有特定颜色特定门数特定最高速度的汽车.如果找到那辆汽车,则第一个断言为真,应找到具有其他字段值的下一辆汽车.

                  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.

                  我不允许更改 T 的实现.使用Boost就可以了.

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

                  你会怎么做?

                  推荐答案

                  这取决于T的实现.让我们继续使用 Car 类的示例.假设这个类看起来像这样:

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

                  operator< 应根据所有属性Car 的实例进行排序,以便搜索所有属性.否则你会得到错误的结果.

                  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.

                  所以基本上,您可以仅使用这些属性来构建汽车实例.在这种情况下,您可以使用 std::set::find 并提供具有您要查找的属性的 Car 临时实例:

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

                  如果您想搜索 Car 的实例,仅指定其属性的一个子集,就像所有绿色汽车一样,您可以使用带有自定义的 std::find_if谓词:

                  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
                  }
                  

                  请注意,第二个解决方案具有线性复杂性,因为它不能依赖于您使用的谓词可能存在或不存在的任何排序.然而,第一个解决方案具有对数复杂性,因为它可以从 std::set 的顺序中受益.

                  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.

                  如果,正如@Betas 对您的问题的评论所建议的那样,您想在运行时组合谓词,则必须编写一些辅助类来组合不同的谓词.

                  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 中查找具有特定字段值的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 std::deque 或 std::priority_queue 线程安全吗? 下一篇:插入 STL 映射是否会使其他现有迭代器无效?

                  相关文章

                  <i id='GcopY'><tr id='GcopY'><dt id='GcopY'><q id='GcopY'><span id='GcopY'><b id='GcopY'><form id='GcopY'><ins id='GcopY'></ins><ul id='GcopY'></ul><sub id='GcopY'></sub></form><legend id='GcopY'></legend><bdo id='GcopY'><pre id='GcopY'><center id='GcopY'></center></pre></bdo></b><th id='GcopY'></th></span></q></dt></tr></i><div id='GcopY'><tfoot id='GcopY'></tfoot><dl id='GcopY'><fieldset id='GcopY'></fieldset></dl></div>
                  1. <small id='GcopY'></small><noframes id='GcopY'>

                    <tfoot id='GcopY'></tfoot>

                      • <bdo id='GcopY'></bdo><ul id='GcopY'></ul>

                    1. <legend id='GcopY'><style id='GcopY'><dir id='GcopY'><q id='GcopY'></q></dir></style></legend>