본문 바로가기
프로그래밍 _공부자료./C++ 공부

카카오 공채 알고리즘 캐쉬 메모리

by 대구부자 2019. 12. 8.
반응형

 

vector<int>::iterator cashItr;

 아래의 소스 코드에  위의 빨간글씨 처럼 선언 해 놓은곳 이 있을것이다.

 

Vector 안에 위치 값을 알아야 해서. iterarot 함수를 cashltr로 선언 사용 하였다.

 

포인터 개념과 비슷하다. 

 

iterator 은 해당 인덱스 배열의 주소값을 가지고 있는거 같다. 

 

 

 

 

 

#include <stdio.h>

#include <iostream>



#include <vector>

using namespace std;





int work_cashMemory() {

 int cash;



 cout << " 작업할 캐쉬 수 입력" << endl;

 cin >> cash;

 cout << "===============" << endl;

 cout << "작업할 캐쉬 수 : " << cash << endl;

 cout << "===============" << endl;



 return cash;

}



int jobCount() {

 int num;



 cout << "작업할 수를 입력하세요" << endl;

 cin >> num;

 cout << "============" << endl;

 cout << "작업할 개수는 "<< num << "개 입니다."  << endl;

 cout << "===============" << endl;



 return num;

}





void insertJopList(int nJobCount, vector<int> *job) {



 cout << "작업을 입력해 주세요" << endl;



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

 {

 int nJobValue = 0;

 cin >> nJobValue;

 (*job).insert((*job).begin()+i,nJobValue); // 배열처럼 값을 입력시, 

    //시작인덱스를 begin()함수 호출 필요

 //(*job).push_back(i);

 }

 cout << endl;

}





void jobProcess(vector<int> *job, vector<int> *cashMemory, int nCashSize)

{

 cout << "vector capacity: " << (*job).capacity() << endl;

 cout << "vector size: " << (*job).size() << endl;



 for (vector<int>::iterator itr = (*job).begin(); itr != (*job).end(); itr++)  // 이 for문은 맨 아래에서 설명 하겠습니다.

 {

 vector<int>::iterator cashItr;

 cashItr = find((*cashMemory).begin(), (*cashMemory).end(), *itr);



 if (cashItr != (*cashMemory).end()) //cash memory에 해당 job이 있음

 {

 (*cashMemory).erase(cashItr);

 (*cashMemory).insert((*cashMemory).begin(), *itr);

 }

 else { // cash memory에 해당 job이 없음(cash memory size은 사용자가 입력하는 값)

 if ((*cashMemory).capacity() == nCashSize) {

 // 현재 cash memory에 들어 있는 데이터 갯수가 사용자가 입력한 cash meory size하고 같을때.

 //(*cashMemory).erase((*cashMemory).end()-1);

 (*cashMemory).pop_back(); //벡터의 마지막요소삭제

 (*cashMemory).insert(cashMemory->begin(), *itr); 

 }

 else {

 // 현재 cash memory에 들어 있는 데이터 갯수가 사용자가 입력한 cash meory size하고 다를때.

  (*cashMemory).insert((*cashMemory).begin(), *itr);

 }

 }

 }

}







int main() 

{

 vector <int>* job = new vector<int>();

 vector <int>* cashMemory=new vector<int>();



 int cash_num = work_cashMemory(); // 총 캐쉬 size 

 //(*cashMemory).resize(cash_num);

 int nJobCount= jobCount();         // 작업 개수



 insertJopList(nJobCount,job); //입력한 job수 만큼 job입력 //

 jobProcess(job, cashMemory,cash_num);



 system("pause");

}
 

 

반응형

댓글