STL은 standard template library. 즉 표준 템플릿 라이브러리의 줄임말로서
크게 컨테이너, 반복자, 알고리즘로 구성되어 있다.
STL에서 제공하는 컨테이너에는 pair, vector, list, queue, stack, map 등이 있다.
list는 연결리스트, map은 2진트리 등을 생각하면 된다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <list> list<int> intList; // 맨뒤에 삽입 intList.push_back(10); // 맨앞에 삽입 intList.push_front(5); // iterator는 리스트 검색시 temp라고 보면된다. // begin()은 head, end()는 널값이라고 보면 됨. list<int>::iterator iter = intList.begin(); while(iter != intList.end()) { int dat = *iter; cout << dat << endl; // 연산자 오버로딩이 되어 이렇게 써도된다. iter++ } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <map> // 맵은 키와, 데이터값을 가진다. map<int, string> datas; datas[5] = "abcd"; datas[15] = "ffff"; // 키와 데이터는 템플릿으로 되어 있어 무엇을 넣어도 됨. map<string, int> data2; data2["hp"] = 50; data2["mp"] = 60; cout << datas[5].c_str() << endl; // find()를 이용해 검색도 가능하다. map<string, int>::iterator iterFind = data2.find("hp"); if(iterFind != data2.end()) // first는 키값, second int d = iterFind->second; | cs |
list와 map은 이런식으로 사용이 가능하다 (예시)
'프로그래밍 공부 > C++' 카테고리의 다른 글
부동 소수점(Floating Point) 표현 (0) | 2018.12.27 |
---|---|
A* 알고리즘 / 최단경로탐색 (0) | 2018.07.10 |
템플릿(template) (0) | 2018.03.21 |
2중 포인터 (0) | 2018.03.21 |
콘솔 미니 RPG게임 (상속 예제) (1) | 2018.03.21 |