Vector series in practice c ++ -- use std: find and std: find_if algorithms for vector (custom class)

Source: Internet
Author: User

Vector series in practice c ++ -- use std: find and std: find_if algorithms for vector (custom class)

Previously, my blog talked about some usage of std: find and std: find _ if, but not about the custom class stored in the vector. How to Use std:: find and std: find_if?

First define a class:

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
  
    getItemList(){    std::vector
   
     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;}
   
  

Next we use the std: find algorithm:

Int main () {std: vector
  
   
VecOfItems = getItemList (); std: vector
   
    
: 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;} // output: found with Price: 28 Count: 6
   
  

However, if = is not available, we can use find_if to solve the problem:
Add function:

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

This is the case:

#include
  
   #include
   
    #include
    
     #include#include
     
      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
      
        getItemList(){ std::vector
       
         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
        
          vecOfItems = getItemList(); std::vector
         
          ::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;}
         
        
       
      
     
    
   
  

Finally, you can use the lambda expression:

std::vector
  
    vecOfItems = getItemList();std::vector
   
    ::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 ::"<
    
     getPrice()<<" Count :: "<
     
      getCount()<
      
     
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.