¸®½ºÆ®¿¡ ±¸Á¶Ã¼¸¦ ÀÔ·ÂÇÒ¶§ °Ë»öÇÏ´Â ¹æ¹ý¿¡ ´ëÇؼ ¾Ë¾Æº¸ÀÚ.ÇÊ¿äÇÑ Çì´õ ÆÄÀÏ#include <string>
#include <list> #include <algorithm> #include <functional> Item ±¸Á¶Ã¼struct Item
{ std::string _name; float _price; Item(std::string name, float price) { _name = name; _price = price; } bool operator==(const Item& n) const { if (this->_name.compare( n._name) == 0) return true; else return false; } }; typedef std::list<Item> ItemList; ¸®½ºÆ® ÃʱâÈ ItemList list = { Item("sword1", 100)
, Item("sword2", 200) , Item("sword3", 300) , Item("sword4", 400) }; find_if¿Í ¶÷´Ù¸¦ ÀÌ¿ëÇÑ ¹æ½Ä std::string key = "sword3";
auto it = std::find_if(list.begin(), list.end(), [&key](Item const& item) -> bool { return item._name.compare(key) == 0; }); if (it != list.end()) std::cout << key << " : " << it->_price << std::endl; find_if¿Í comparator¸¦ ÀÌ¿ëÇÑ ¹æ½Äbool CompareName(Item& item, std::string key)
{ if (item._name.compare(key) == 0) return true; else return false; } auto it2 = std::find_if(list.begin(), list.end(), std::bind(CompareName, std::placeholders::_1, "sword4")); if (it2 != list.end()) std::cout << it2->_name << " : " << it2->_price << std::endl; std::find¿Í== operator¸¦ ÀÌ¿ëÇÑ ¹æ½Ästruct Item
{ std::string _name; float _price; Item(std::string name, float price) { _name = name; _price = price; } bool operator==(const Item& n) const { if (this->_name.compare( n._name) == 0) return true; else return false; } }; typedef std::list<Item> ItemList; void main() { auto it1 = std::find(list.begin(), list.end(), Item("sword4", 0)); if (it1 != list.end()) std::cout << it1->_name << " : " << it1->_price << std::endl; } ¿©·¯°¡Áö list Ãâ·Â
|