zoukankan      html  css  js  c++  java
  • vector系列--对vector<自定义类>使用std::find 和 std::find_if 算法

    之前博客讲了一些关于std::find和std::find_ if的一些用法,但是没有讲述对于vector中存储的是自定义的类,那么怎么样使用std::find和std::find_if进行查找呢?

    先定义一个类:

    class Item
    {
    private:
        std::string  m_ItemId;
        int m_Price;
        int m_Count;
    public:
        Item(std::string id, int price, int count):
            m_ItemId(id), m_Count(count), m_Price(price){}
        int getCount() const {
            return m_Count;
        }
        std::string getItemId() const {
            return m_ItemId;
        }
        int getPrice() const {
            return m_Price;
        }
        bool operator==(const Item & obj2) const
        {
            if(this->getItemId().compare(obj2.getItemId()) == 0)
                return true;
            else
                return false;
        }
    };
    std::vector<Item> getItemList()
    {
        std::vector<Item> vecOfItems ;
        vecOfItems.push_back(Item("D121",100,2));
        vecOfItems.push_back(Item("D122",12,5));
        vecOfItems.push_back(Item("D123",28,6));
        vecOfItems.push_back(Item("D124",8,10));
        vecOfItems.push_back(Item("D125",99,3));
        return vecOfItems;
    }
    

      接下来就是使用std::find算法了:

    int main()
    {
        std::vector<Item> vecOfItems = getItemList();
        std::vector<Item>::iterator it;
        it = std::find(vecOfItems.begin(), vecOfItems.end(), Item("D123", 99, 0));
        if (it != vecOfItems.end())
            std::cout << "Found with Price ::" << it->getPrice() << " Count :: " << it->getCount() << std::endl;
        else
            std::cout << "Item with ID :: D126 not Found" << std::endl;
        return 0;
    }
    
    //输出:
    Found with Price ::28 Count :: 6
    

      但是如果不能使用==的情况下,我们就可以使用find_if解决问题了:
    增加函数:

    bool priceComparision(Item & obj, int y)
    {
        if(obj.getPrice() == y)
            return true;
        else
            return false;
    }
    

      就是这样:

    #include<iostream>
    #include<vector>
    #include<string>
    #include<algorithm>
    #include<functional>
    using namespace std;
    class Item
    {
    private:
        std::string  m_ItemId;
        int m_Price;
        int m_Count;
    public:
        Item(std::string id, int price, int count) :
            m_ItemId(id), m_Count(count), m_Price(price) {}
        int getCount() const {
            return m_Count;
        }
        std::string getItemId() const {
            return m_ItemId;
        }
        int getPrice() const {
            return m_Price;
        }
        bool operator==(const Item & obj2) const
        {
            if (this->getItemId().compare(obj2.getItemId()) == 0)
                return true;
            else
                return false;
        }
    };
    
    bool priceComparision(Item & obj, int y)
    {
        if (obj.getPrice() == y)
            return true;
        else
            return false;
    }
    
    std::vector<Item> getItemList()
    {
        std::vector<Item> vecOfItems;
        vecOfItems.push_back(Item("D121", 100, 2));
        vecOfItems.push_back(Item("D122", 12, 5));
        vecOfItems.push_back(Item("D123", 28, 6));
        vecOfItems.push_back(Item("D124", 8, 10));
        vecOfItems.push_back(Item("D125", 99, 3));
        return vecOfItems;
    }
    
    int main()
    {
        std::vector<Item> vecOfItems = getItemList();
        std::vector<Item>::iterator it;
        it = std::find_if(vecOfItems.begin(), vecOfItems.end(), std::bind(priceComparision, std::placeholders::_1, 28));
        if (it != vecOfItems.end())
            std::cout << "Item Price ::" << it->getPrice() << " Count :: " << it->getCount() << std::endl;
        else
            std::cout << "Item not Found" << std::endl;
        return 0;
    }
    

      最后还可以使用lambda表达式:

    std::vector<Item> vecOfItems = getItemList();
    std::vector<Item>::iterator it;
    it = std::find_if(vecOfItems.begin(), vecOfItems.end(), [](Item const& obj){
            return obj.getPrice() == 28;
        } );
    if(it != vecOfItems.end())
        std::cout<<"Item Price ::"<<it->getPrice()<<" Count :: "<<it->getCount()<<std::endl;
    else
        std::cout<<"Item not Found"<<std::endl;
    

      

  • 相关阅读:
    Ceph rbd删除image遭遇watchers异常处理
    Ceph OSD更换硬盘后遭遇PG Inconsistent异常与处理
    Rook Ceph OSD异常,格式化osd硬盘重新挂载
    Count on an IEnumerable<dynamic>
    [原创] [C#] 转换Excel数字列号为字母列号
    [MAC] Load Crypto.Cipher.ARC4 Failed, Use Pure Python Instead.
    转:Chrome调试工具介绍
    转:一组jQuery插件的连接
    动态的链式传递执行
    C#中克隆随机数的三种方法(为什么我想到了茴字的四种写法
  • 原文地址:https://www.cnblogs.com/noticeable/p/14862169.html
Copyright © 2011-2022 走看看