##實戰c++中的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;
~~~
- 前言
- 構造、operator=和assign區別
- 將迭代器轉換為索引
- copy set to vector(別混淆了reserve和resize)
- 使用vector構造二維數組
- 可怕的迭代器失效(vector重新申請內存)
- 可怕的迭代器失效之二(刪除vector中元素)
- vector<unique_ptr<>>初始化(所有權轉移)
- vector<unique_ptr<>>作為函數的參數
- vector<unique_ptr<>>賦值給vector<unique_ptr<>>
- creating vector of local structure、vector of structs initialization
- 知道emplace_back為何優于push_back嗎?
- emplace_back造成的引用失效
- vector的一些異常
- vector的遍歷(stl算法、vector迭代器(不要在循環中判斷不等于end())、operator[])
- 使用sort算法對vector進行排序(對vector<string>排序、使用穩定的排序std::stable_sort())
- vector應用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)
- 使用sort算法對vector<unique_ptr<string>>進行排序(sort函數“應輸入 2 個參數,卻提供了 3 個)
- 對vector<自定義類>使用std::find 和 std::find_if 算法