[Study] 알고리즘 문제풀이

STL 복습 #deque #set #map #find(vector)

미런던 2017. 4. 20. 11:08

예전 프로그래밍 방법론 수업에서 배운 것들

자료들을 재정리 해 보았다.

특히 map은 매우 유용할 것 같다.



deque

push.front, push.back, pop.front, pop.back

-list와 비슷해보이나 random access가 가능하고, 중간에 insertion/deletion은 불가능


set/multiset

단일 원소, 오름/내림차순으로 소팅

내부 구현은 tree구조로 되어있음. priority queue 생각


map/multimap

key-value로 값 저장.

cout<< Employees[5235]; //David.D. 사원번호-이름 저장

multimap<string, int> m;

m.insert(pair<string, int>("A",930903));


Reverse Iterator

vector<int>::reverse_iterator p = container.rbegin(); p!= container.rend(); p++

//container.rbegin() == container.end()-1

//container.rend() == container.begin()-1


Filling Algorithms

(1)전체 Fill

vector<int> a(10);

fill(a.begin(),a.end(),1);

(2)n개 Fill

fill_n(a.begin(),n,);

(3)부분 복사

copy(a.begin(),a.begin()+4,b.begin());

(4)전체 랜덤 Fill

generate(a.begin(),a.end(),RandomInt(1,100));

(5)부분 n개 Fill

generate(a.begin(),10,RandomInt(1,100));


Relocating Algorithms

(1)Sort

sort(a.begin(),a.end(),compare);

bool compare(const int& x, const int& y){

return x>y;

}

(2)Reverse

reverse(a.begin(), a.end());

(3)Random_shuffle

random_shuffle(a.begin(),a.end());


Searching Algorithms

(1)최대, 최소값

vector<int>::iterator = max_element(a.begin(), a.end());

vector<int>::iterator = min_element(a.begin(), a.end());

(2)찾기(Linear search)

find(a.begin(), a.end(), 12345);

(3)있는지만 찾기(Binary search)

bool isFound = binary_search(a.begin(), a.end(), 12345);