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

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

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

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

      如何将 std::find/std::find_if 与自定义类对象的向量一起使用?

      时间:2024-05-12

      <legend id='96jT9'><style id='96jT9'><dir id='96jT9'><q id='96jT9'></q></dir></style></legend>

      <small id='96jT9'></small><noframes id='96jT9'>

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

            <tfoot id='96jT9'></tfoot>
                <tbody id='96jT9'></tbody>
              1. 本文介绍了如何将 std::find/std::find_if 与自定义类对象的向量一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个表示名为 Nick 的用户的类,我想在其上使用 std::find_if,我想在其中查找用户列表向量是否有对象包含在我传入的相同用户名中.我尝试为要测试的用户名创建一个新的 Nick 对象并重载 == 运算符 和然后尝试在对象上使用 find/find_if:

                I have a class representing a user called Nick and I want to use std::find_if on it, where I want to find if the userlist vector has an object included with the same username I pass in. I did a few attempts by trying to create a new Nick object for the username I want to test and overloading the == operator and then trying to use find/find_if on the object:

                    std::vector<Nick> userlist;
                    std::string username = "Nicholas";
                
                if (std::find(userlist.begin(), userlist.end(), new Nick(username, false)) != userlist.end())) {
                    std::cout << "found";
                }
                

                我重载了 == 运算符,所以比较 Nick == Nick2 应该可以工作,但是函数返回 error C2678: binary '==' : no operator found which take a left-'Nick' 类型的手操作数(或没有可接受的转换).

                I have overloaded the == operator so comparing Nick == Nick2 should work, but the function returns error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Nick' (or there is no acceptable conversion).

                这是我的尼克课供参考:

                Here is my Nick class for reference:

                class Nick {
                private:
                    Nick() {
                        username = interest = email = "";
                                    is_op = false;
                    };
                public:
                    std::string username;
                    std::string interest;
                    std::string email;
                    bool is_op;
                
                    Nick(std::string d_username, std::string d_interest, std::string d_email, bool d_is_op) {
                        Nick();
                        username = d_username;
                        interest = d_interest;
                        email = d_email;
                        is_op = d_is_op;
                    };
                    Nick(std::string d_username, bool d_is_op) {
                        Nick();
                        username = d_username;
                        is_op = d_is_op;
                    };
                    friend bool operator== (Nick &n1, Nick &n2) {
                        return (n1.username == n2.username);
                    };
                    friend bool operator!= (Nick &n1, Nick &n2) {
                        return !(n1 == n2);
                    };
                };
                

                推荐答案

                你必须将 operator== 定义为你的类之外的两个对象,作为工具函数,而不是成员.

                You have to define operator== with two Objects outside your class, as a tool function, not a member.

                然后为了让它成为朋友,只需将函数的声明放在类中即可.

                Then to make it friend just put the declaration of the function inside the class.

                尝试这样的事情:

                class Nick {
                
                public:
                    friend bool operator== ( const Nick &n1, const Nick &n2);
                };
                
                
                bool operator== ( const Nick &n1, const Nick &n2) 
                {
                        return n1.username == n2.username;
                }
                

                您的发现也应如下所示:

                Also your find should look like this:

                std::find(userlist.begin(), userlist.end(), Nick(username, false) );
                

                不需要新".

                这篇关于如何将 std::find/std::find_if 与自定义类对象的向量一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:我想将 std::string 转换为 const wchar_t * 下一篇:C++ std::set 线程安全吗?

                相关文章

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

                <tfoot id='wja6Z'></tfoot>

                    <bdo id='wja6Z'></bdo><ul id='wja6Z'></ul>

                1. <small id='wja6Z'></small><noframes id='wja6Z'>