프로그래밍 언어/C++

C++ 메모장) 구조체 Vector 특정 요소 찾기 (find_if)

DevPing9_ 2021. 4. 26. 17:14
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct cow{
  int cow_no;
  char grade;
  int profit;
};

typedef vector<cow> COW;

struct Find{
  char grade;
  bool operator()(cow meooo)
  {
    return (meooo.grade == grade);
  }
};



cow CREATE_cow(int cow_no, char grade, int profit){
   cow c;
   c.cow_no = cow_no;
   c.grade = grade;
   c.profit = profit;
   return c;
}


int main(){
	COW cows;
  cows.push_back(CREATE_cow(5,'A',400));
  cows.push_back(CREATE_cow(2,'B',500));
  cows.push_back(CREATE_cow(3,'A',200));
  cows.push_back(CREATE_cow(4,'C',150));
  
  Find find;
  find.grade='A';
  vector<cow>::iterator item;
  item = find_if(cows.begin(), cows.end(), find);
  
  cout<< "Frist A grade cow's number : " << item->cow_no <<endl;

  cout<< "Second A grade cow's number : " << item[1].cow_no <<endl;  



}
728x90