STL - Algorithm

이승덱·2021년 7월 21일

CPP

목록 보기
59/70
#include <iostream>

#include <vector>

#include <list>

#include <deque>

#include <map>

#include <set>

using namespace std;

// 알고리즘

#include<algorithm>

int main()

{

 // 자료구조 (데이터를 저장하는 구조)

 // 알고리즘 (데이터를 어떻게 사용할 것인가?)

 // find

 // find_if

 // count

 // count_if

 // all_of

 // any_of

 // none_of

 // for_each

 // remove

 // remove_if

 vector<int> v;

 srand(static_cast<unsigned int>(time(nullptr)));

 for (int i = 0;i < 100;i++)

 {

 int randValue = rand() % 100;

 v.push_back(randValue);

 }

 // Q1) number라는 숫자가 벡터에 있는지 체크하는 기능 (bool, 첫 등장 iterator)

 {

 int number = 50;

 bool found = false;

 vector<int>::iterator it;

 // TODO

 // algorithm Ver

 // find() 사용

 vector<int>::iterator itFind = find(v.begin(),v.end(),number);

 if (itFind == v.end()) {

 cout << "못 찾음" << endl;

 }

 else {

 cout << "찾음" << endl;

 found = true;

 }

 int a = 3; //디버깅용 코드

 }

 // Q2) 11로 나뉘는 숫자가 벡터에 있는지 체크하는 기능 (bool, 첫 등장 iterator)

 {

 bool found = false;

 vector<int>::iterator it;

 // TODO

 // algorithm Ver

 // find_if() 사용

 struct CanDivideBy11 {

 bool operator()(int n) {

 return (n % 11) == 0;

 }

 };

 // 세번째 인자에 함수형태의 인자를 넣어줌

 vector<int>::iterator itFind=find_if(v.begin(), v.end(), CanDivideBy11());

 //vector<int>::iterator itFind = find_if(v.begin(), v.end(), [](int n) {return (n % 11) == 0;});

 if (itFind == v.end()) {

 cout << "못 찾음" << endl;

 }

 else {

 cout << "찾음" << endl;

 found = true;

 }

 int a = 3; //디버깅용 코드

 }

 // Q3) 홀수인 숫자의 개수는? (count)

 {

 int count = 0;

 // TODO

 // algorithm Ver

 // count_if() 사용

 // all_of() 사용

 // any_of() 사용

 // none_of() 사용

 struct IsOdd {

 bool operator()(int n) {

 return (n % 2) != 0;

 }

 };

 // 세번째 인자에 함수형태의 인자를 넣어줌

 count = count_if(v.begin(), v.end(), IsOdd());

 cout << count << endl;

 // counting이 아닌 반례를 발견하면 빠져나와야하는 경우

 // NO: 0, YES: 1

 bool b1= all_of(v.begin(), v.end(), IsOdd()); //모든 데이터가 홀수 입니까?

 bool b2 = any_of(v.begin(), v.end(), IsOdd()); //홀수인 데이터가 하나라도 있습니까?

 bool b3 = none_of(v.begin(), v.end(), IsOdd()); //모든 데이터가 홀수가 아닙니까?

 cout << b1 << " " << b2 << " " << b3 << endl;

 int a = 3; //디버깅용 코드

 }

 // Q4) 벡터에 들어가 있는 모든 숫자들에 3을 곱해주세요!

 {

 // TODO

 // algorithm Ver

 // for_each() 사용

 struct MultiplyBy3 {

 void operator()(int n) {

 n= n * 3;

 }

 };

 // 세번째 인자에 함수형태의 인자를 넣어줌

 for_each(v.begin(), v.end(), MultiplyBy3());

 int a = 3; //디버깅용 코드

 }

 // Q5) 홀수인 데이터를 일괄 삭제

 {

 // 벡터의 중간 삭제는 비효율적

 for (vector<int>::iterator it = v.begin();it != v.end();) {

 if ((*it % 2) != 0) {

 it=v.erase(it);

 }

 else {

 ++it;

 }

 }

 v.clear();

 v.push_back(4);

 v.push_back(3);

 v.push_back(5);

 v.push_back(8);

 v.push_back(2);

 //remove(v.begin(), v.end(), 4);

 struct IsOdd {

 bool operator()(int n) {

 return (n % 2) != 0;

 }

 };

 // 지워줘야할 데이터를 지우는 방식이 아니라 남겨야할 데이터를 찾아서 앞당겨줌

 // 나머지 쓸모없는 데이터는 날려줘야 한다!

 // 날려주는 부분을 따로 해줘야함

 vector<int>::iterator it = remove_if(v.begin(), v.end(), IsOdd());

 v.erase(it, v.end()); //이 부분이 remove문과는 필수적으로 같이 사용되야함

 int a = 3; //디버깅용 코드

 }

 return 0;

}
profile
공부 기록용 블로그입니다

0개의 댓글