實戰c++中的vector系列--對vector<自己定義類>使用std::find 和 std::find_if 演算法

來源:互聯網
上載者:User

標籤:length   pac   問題   rip   roc   使用   return   bin   ios   

之前部落格講了一些關於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;

實戰c++中的vector系列--對vector&lt;自己定義類&gt;使用std::find 和 std::find_if 演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.