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

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

  • <legend id='eoLU9'><style id='eoLU9'><dir id='eoLU9'><q id='eoLU9'></q></dir></style></legend>

        <tfoot id='eoLU9'></tfoot>
      1. 按对象属性搜索对象向量

        时间:2023-09-19

            <tbody id='2MlNE'></tbody>

            <legend id='2MlNE'><style id='2MlNE'><dir id='2MlNE'><q id='2MlNE'></q></dir></style></legend>

          1. <small id='2MlNE'></small><noframes id='2MlNE'>

            • <tfoot id='2MlNE'></tfoot>
            • <i id='2MlNE'><tr id='2MlNE'><dt id='2MlNE'><q id='2MlNE'><span id='2MlNE'><b id='2MlNE'><form id='2MlNE'><ins id='2MlNE'></ins><ul id='2MlNE'></ul><sub id='2MlNE'></sub></form><legend id='2MlNE'></legend><bdo id='2MlNE'><pre id='2MlNE'><center id='2MlNE'></center></pre></bdo></b><th id='2MlNE'></th></span></q></dt></tr></i><div id='2MlNE'><tfoot id='2MlNE'></tfoot><dl id='2MlNE'><fieldset id='2MlNE'></fieldset></dl></div>
                • <bdo id='2MlNE'></bdo><ul id='2MlNE'></ul>
                  本文介绍了按对象属性搜索对象向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我试图找出一种在向量中查找某个对象的索引的好方法 - 通过将字符串与对象中的成员字段进行比较.

                  I'm trying to figure out a nice way to find the index of a certain object in a vector - by comparing a string to a member field in the object.

                  像这样:

                  find(vector.begin(), vector.end(), [object where obj.getName() == myString])
                  

                  我搜索没有成功 - 也许我不完全明白要寻找什么.

                  I have searched without success - maybe I don't fully understand what to look for.

                  推荐答案

                  您可以使用 std::find_if 带有合适的函子.在此示例中,使用了 C++11 lambda:

                  You can use std::find_if with a suitable functor. In this example, a C++11 lambda is used:

                  std::vector<Type> v = ....;
                  std::string myString = ....;
                  auto it = find_if(v.begin(), v.end(), [&myString](const Type& obj) {return obj.getName() == myString;})
                  
                  if (it != v.end())
                  {
                    // found element. it is an iterator to the first matching element.
                    // if you really need the index, you can also get it:
                    auto index = std::distance(v.begin(), it);
                  }
                  

                  如果您没有 C++11 lambda 支持,函子可以工作:

                  If you have no C++11 lambda support, a functor would work:

                  struct MatchString
                  {
                   MatchString(const std::string& s) : s_(s) {}
                   bool operator()(const Type& obj) const
                   {
                     return obj.getName() == s_;
                   }
                   private:
                     const std::string& s_;
                  };
                  

                  这里,MatchString 是一种类型,它的实例可以用单个 Type 对象调用,并返回一个布尔值.例如,

                  Here, MatchString is a type whose instances are callable with a single Type object, and return a boolean. For example,

                  Type t("Foo"); // assume this means t.getName() is "Foo"
                  MatchString m("Foo");
                  bool b = m(t); // b is true
                  

                  然后你可以将一个实例传递给 std::find

                  then you can pass an instance to std::find

                  std::vector<Type>::iterator it = find_if(v.begin(), v.end(), MatchString(myString));
                  

                  这篇关于按对象属性搜索对象向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用 OpenSSL 的 SHA256 函数 下一篇:这是 C++11 正则表达式错误我还是编译器?

                  相关文章

                  最新文章

                  <legend id='D9qAl'><style id='D9qAl'><dir id='D9qAl'><q id='D9qAl'></q></dir></style></legend>
                      <bdo id='D9qAl'></bdo><ul id='D9qAl'></ul>

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

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

                    1. <tfoot id='D9qAl'></tfoot>