STL - Vector 실습

이승덱·2021년 7월 21일

CPP

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

#include <vector>

#include <list>

#include <deque>

#include <map>

#include <set>

using namespace std;

int main()

{

 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

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

 if ((*findIt) == 50)

 {

 found = true;

 it = findIt;

 break;

 }

 }

 int a = 3;

 }

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

 {

 bool found = false; vector<int>::iterator it;

 // TODO

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

 if ((*findIt) % 11 == 0)

 {

 found = true;

 it = findIt;

 break;

 }

 }

 int a = 3;

 }

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

 {

 int count = 0;

 // TODO

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

 if ((*findIt) % 2 != 0)

 {

 count++;

 }

 }

 int a = 3;

 }

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

 {

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

 (*findIt) = (*findIt) * 3;

 }

 int a = 3;

 }

 // 위 방식들의 몇가지 문제점

 // 가독성이 좋지않다.

 // 다른 자료구조로 변환이 어렵다( vector에 의존적이다)

 return 0;

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

0개의 댓글