참고서적 : 한빛 C++ STL 프로그래밍
기본 사용법 부터 insert 세가지 형식의 사용법까지 포스팅 합니다.
1. 포함 헤더 파일
#include <list>
using namespace std;
2. 형식
list< 자료형 > 변수 이름
list< 자료형 >* 변수 이름 =newlist< 자료형 >;
3. 반복자 (iterator)
STL의 컨테이너 < 자료형 >::iterator 변수 이름
4. list의 주요 맴버
멤버 |
설명 |
begin |
첫 번째 위치를 가리킨다. |
end |
마지막 위치의 다음을 가리킨다. |
rbegin |
역 방향으로 첫번째 위치를 가르킨다. |
rend |
역 방향으로 마지막 위치를 가리킨다. |
push_front |
첫 번째 위치에 데이터 추가 |
pop_front |
첫 번째 위치의 데이터 삭제 |
push_back |
마지막 위치에 데이터 추가 |
pop_back |
마지막 위치의 데이터 삭제 |
front |
첫 번째 데이터의 참조 리턴 |
back |
마지막 데이터의 참조 리턴 |
clear |
저장하고 있는 모든 데이터 삭제 |
empty |
저장 데이터 유/무, 없으면 true 리턴 |
size |
저장하고 있는 데이터의 개수 리턴 |
insert |
지정된 위치에 삽입 |
erase |
지정된 범위에 있는 데이터 삭제 |
remove |
지정된 값과 일치하는 모든 데이터 삭제 |
remove_if |
함수객체의 조건을 만족하는 모든 데이터 삭제 |
sort |
데이터를 정렬한다. |
5. 예제 소스
(1) 기본예제
- #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, const char * argv[])
- {
- // insert code here...
- list<Item> Itemlist;
- // 앞에 데이터 추가
- Item item1(1, 2000);
- Itemlist.push_front(item1);
- Item item2(2, 1000);
- Itemlist.push_front(item2);
- // 뒤에 데이터 추가
- Item item3(3, 3000);
- Itemlist.push_back(item3);
- Item item4(4, 4000);
- Itemlist.push_back(item4);
- // 아이템 코드 번호가 2, 1, 3, 4의 순서로 출력된다.
- list<Item>::iterator iterEnd = Itemlist.end();
- for(list<Item>::iterator iterPos = Itemlist.begin() ; iterPos != iterEnd ; ++iterPos){
- cout << "Item Code : " << iterPos->ItemCd << endl;
- }
- ///////////////////////////////////////////////////////////////////////////
- // 앞에 있는 데이터를 삭제
- Itemlist.pop_front();
- // 앞에 있는 데이터의 참조를 리턴한다.
- Item fron_item = Itemlist.front();
- // 아이템 코드 1이 출력된다.
- cout << "아이템 코드 : " << fron_item.ItemCd << endl;
- ///////////////////////////////////////////////////////////////////////////
- // 마지막에 있는 데이터를 삭제한다.
- Itemlist.pop_back();
- // 마지막에 있는 데이터를 참조를 리턴한다.
- Item back_item = Itemlist.back();
- // 아이템 코드 3이 출력된다.
- 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;
- }
Item Code : 2
Item Code : 1
Item Code : 3
Item Code : 4
아이템 코드 : 1
아이템 코드 : 3
남아 있는 아이템 개수 : 2
(2) insert 예제
원형 3가지:
iterator insert( iterator _Where, const Type& _Val );
void insert( iterator _Where, size_type _Count, const Type& _Val );
template void insert( iterator _Where, InputIterator _First, InputIterator_Last );
- #include <iostream>
- #include <list>
- using namespace std;
- int main(int argc, const char * argv[])
- {
- // insert code here...
- list<int> list1;
- list1.push_back(20);
- list1.push_back(30);
- /////////////////////////////////////////////////////////////
- cout << "삽입 테스트 1" << endl;
- // 첫 번째 위치에 삽입한다.
- list<int>::iterator iterInsertPos = list1.begin();
- list1.insert( iterInsertPos, 100);
- // 100, 20, 30순으로 출력된다.
- 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;
- // 두 번째 위치에 200을 2개 삽입한다.
- iterInsertPos = list1.begin();
- ++iterInsertPos;
- list1.insert( iterInsertPos, 2, 200);
- // 100,200,200,20,30 순으로 출력된다.
- 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 );
- // 두 번째 위치에 list2의 모든 데이터를 삽입한다.
- iterInsertPos = list1.begin();
- list1.insert( ++iterInsertPos, list2.begin(), list2.end());
- // 100, 1000, 2000, 3000, 200, 200, 20, 30 순으로 출력된다.
- iterEnd = list1.end();
- for( list<int>::iterator iterPos = list1.begin() ; iterPos != iterEnd ; ++iterPos ){
- cout << "list 1 : " << *iterPos << endl;
- }
- return 0;
- }
삽입 테스트 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