13. STL list1
by Qerogram* 자료구조의 LinkedList나 ArrayList를 생각하면 되겠다.
- 추가해야할 헤더는 list헤더. std namespace도 사용해주어야 한다.(사용안하면 std::list로 사용해야되서 불편)
- 정의는 아래와 같다.(사용법)
- list<자료형> 변수명; ex) list<int> list1;
- 동적할당도 가능하다.
- list<자료형>* 변수이름 = new list<자료형>;
- ex) list<int>* list1 = new list<int>;
* iterator(반복자)
- list에 저장된 데이터에 접근을 하려면 반복자를 꼭 이용해야한다.
- 포인터와 유사하다고 보면 된다.(STL 컨테이너에 저장된 데이터를 순회 가능)
- STL의 컨테이너<자료형>::iterator 변수명으로 사용한다.
- 시작은 begin(), rend() 끝은 end(), rbegin()으로 표기한다.
- 시작을 지정한다고 가정해보면 list<int>::iterator iterBegin = list1.begin();
* 주로 이용하는 멤버
- push_front : 첫 번째 위치 데이터 추가
- pop_front : 첫 번째 위치 데이터 삭제
- push_back : 마지막 위치 데이터 추가
- pop_back : 마지막 위치 데이터 삭제
- front : 첫번째 데이터의 참조 리턴
- back : 마지막 데이터의 참조 리턴
- clear : list 비우기
- empty : 저장 데이터 유/무 없으면 true 리턴.
- size : 리스트의 Element 개수 리턴.
- insert : 저장 위치에 삽입.
- erase : 지정된 범위에 있는 데이터 삭제
- remove : 지정된 값과 일치하는 모든 데이터 삭제
- remove : 함수객체의 조건을 만족하는 모든 데이터 삭제
- sort : 데이터 정렬
* 주요 멤버들을 모두 이용해 list를 활용해보면 아래 코드와 같다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <iostream> #include <list> using namespace std; struct Item { Item(int itemCd, int buyMoney){ ItemCd = itemCd; BuyMoney = buyMoney; } int ItemCd; int BuyMoney; }; int main(int argc, char* argv[]) { list<Item> Itemlist; Item item1(1, 2000); Item item2(2, 1000); Item item3(3, 3000); Item item4(4, 4500); Itemlist.push_front(item1); Itemlist.push_front(item2); Itemlist.push_back(item3); Itemlist.push_back(item4); list<Item>::iterator iterEnd = Itemlist.end(); for(list<Item>::iterator iterPos=Itemlist.begin(); iterPos != iterEnd; ++iterPos) { cout << "아이템 코드 : " << iterPos->ItemCd << endl; } cout << endl; Itemlist.pop_front(); Item front_item = Itemlist.front(); cout << "아이템 코드 : " << front_item.ItemCd << endl; Itemlist.pop_back(); Item back_item = Itemlist.back(); cout << "아이템 코드 : " << back_item.ItemCd << endl; if(false == Itemlist.empty()) { list<Item>::size_type Count = Itemlist.size(); cout << "남아있는 아이템 갯수 : " << Count << endl; } Itemlist.clear(); list<Item>::size_type Count = Itemlist.size(); cout << "남아있는 아이템 갯수 : " << Count << endl; return 0; } | cs |
* 결과
아이템 코드 : 2
아이템 코드 : 1
아이템 코드 : 3
아이템 코드 : 4
아이템 코드 : 1
아이템 코드 : 3
남아있는 아이템 갯수 : 2
남아있는 아이템 갯수 : 0
* list insert
- 3가지 방법이있다. 아래의 코드에 각 방법으로 넣어보았다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include <iostream> #include <list> using namespace std; int main(int argc, char* argv[]) { list<int> list1; list1.push_back(20); list1.push_back(30); cout << "삽입 테스트 1" << endl; list<int>::iterator iterInsertPos = list1.begin(); list1.insert(iterInsertPos, 100); list<int>::iterator iterEnd = list1.end(); for(list<int>::iterator iterPos = list1.begin(); iterPos != iterEnd; ++iterPos) { cout << "list 1 : " << *iterPos << endl; } cout << endl << "삽입 테스트2" << endl; iterInsertPos = list1.begin(); ++iterInsertPos; list1.insert(iterInsertPos, 2, 200); iterEnd = list1.end(); for(list<int>::iterator iterPos = list1.begin(); iterPos!=iterEnd; ++iterPos) { cout << "list 1 : " << *iterPos << endl; } cout << endl << "삽입 테스트 3" << endl; list<int> list2; list2.push_back(1000); list2.push_back(2000); list2.push_back(3000); iterInsertPos = list1.begin(); list1.insert(++iterInsertPos, list2.begin(), list2.end()); iterEnd = list1.end(); for(list<int>::iterator iterPos = list1.begin(); iterPos != iterEnd; ++iterPos) { cout << "list 1 : " << *iterPos << endl; } return 0; } | cs |
* 결과
삽입 테스트 1
list 1 : 100
list 1 : 20
list 1 : 30
삽입 테스트2
list 1 : 100
list 1 : 200
list 1 : 200
list 1 : 20
list 1 : 30
삽입 테스트 3
list 1 : 100
list 1 : 1000
list 1 : 2000
list 1 : 3000
list 1 : 200
list 1 : 200
list 1 : 20
list 1 : 30
* 참고 - Thinking About C++ STL Programming
'코딩 > C&C++' 카테고리의 다른 글
15. STL list3 (0) | 2017.04.17 |
---|---|
14. STL list2 (0) | 2017.04.16 |
12. 클래스 템플릿의 이해3 (0) | 2017.04.15 |
11. 클래스 템플릿의 이해2 (0) | 2017.04.15 |
10. 클래스 템플릿의 이해1 (0) | 2017.04.14 |
블로그의 정보
Data+
Qerogram