#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

 

struct Weapon
{
   string name;
   int power;
   int max_use;
   Weapon() {}
   Weapon(string name, int power, int max_use)
      :name(name), power(power), max_use(max_use) {}
   void print() const {
      cout << name << "\t" << power << "\t" << max_use << endl;
   }
};

template  <typename T>
struct SortPower
{
   bool operator()(const T& l, const T& r) const {
      return l.power < r.power;
   }
};

 

template  <typename T>
struct BinWSearch
{
   bool operator()(const T& w1, const T& w2) const{
      cout << w1.power << ", " << w2.power << endl;
      return w1.powerw2.power;
   }
};

 

int main()
{

   vector wVec;
   wVec.push_back(Weapon("Sword   ", 45, 50));
   wVec.push_back(Weapon("X-Caliber", 64, 80));
   wVec.push_back(Weapon("Bow     ", 72, 20));
   wVec.push_back(Weapon("Revolver", 87, 20));
   wVec.push_back(Weapon("Shotgun ", 92, 10));

   sort(wVec.begin(), wVec.end(), SortPower());
   for (auto iter = wVec.begin(); iter != wVec.end(); iter++) {  //정렬상태 확인
      iter->print();
   }

   bool found = binary_search(wVec.begin(), wVec.end(), 
   Weapon("X-Caliber", 64, 80), BinWSearch());

   cout << (found ? "검색성공" : "검색실패") << endl;

   cout << "프로그램 종료" << endl;
   return 0;
}

Posted by cwisky