리눅스 프로그래밍/STL

연결 리스트 <list> 기본사용법, insert #1

삽질중 2013. 11. 12. 14:47

참고서적 : 한빛 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) 기본예제

  1. #include <iostream>
  2. #include <list>

  3. using namespace std;

  4. struct Item{
  5.     Item(int itemCd, int buyMoney){
  6.         ItemCd = itemCd;
  7.         BuyMoney = buyMoney;
  8.     }
  9.     
  10.     int ItemCd;
  11.     int BuyMoney;
  12. };

  13. int main(int argc, const char * argv[])
  14. {

  15.     // insert code here...
  16.     list<Item> Itemlist;
  17.     
  18.     // 앞에 데이터 추가
  19.     Item item1(1, 2000);
  20.     Itemlist.push_front(item1);
  21.     
  22.     Item item2(2, 1000);
  23.     Itemlist.push_front(item2);
  24.     
  25.     // 뒤에 데이터 추가
  26.     Item item3(3, 3000);
  27.     Itemlist.push_back(item3);
  28.     
  29.     Item item4(4, 4000);
  30.     Itemlist.push_back(item4);
  31.     
  32.     // 아이템 코드 번호가 2, 1, 3, 4 순서로 출력된다.
  33.     list<Item>::iterator iterEnd = Itemlist.end();
  34.     for(list<Item>::iterator iterPos = Itemlist.begin() ; iterPos != iterEnd ; ++iterPos){
  35.         cout << "Item Code : " << iterPos->ItemCd << endl;
  36.     }
  37.     
  38.     ///////////////////////////////////////////////////////////////////////////
  39.     // 앞에 있는 데이터를 삭제
  40.     Itemlist.pop_front();
  41.     
  42.     // 앞에 있는 데이터의 참조를 리턴한다.
  43.     Item fron_item = Itemlist.front();
  44.     // 아이템 코드 1 출력된다.
  45.     cout << "아이템 코드 : " << fron_item.ItemCd << endl;
  46.     
  47.     ///////////////////////////////////////////////////////////////////////////
  48.     // 마지막에 있는 데이터를 삭제한다.
  49.     Itemlist.pop_back();
  50.     
  51.     // 마지막에 있는 데이터를 참조를 리턴한다.
  52.     Item back_item = Itemlist.back();
  53.     // 아이템 코드 3 출력된다.
  54.     cout << "아이템 코드 : " << back_item.ItemCd << endl;
  55.     
  56.     ///////////////////////////////////////////////////////////////////////////
  57.     // 저장된 데이터가 있는가?
  58.     if( false == Itemlist.empty() ){
  59.         list<Item>::size_type count = Itemlist.size();
  60.         cout << "남아 있는 아이템 개수 : " << count << endl;
  61.     }
  62.     
  63.     ///////////////////////////////////////////////////////////////////////////
  64.     // 모든데이터를 지운다
  65.     Itemlist.clear();
  66.     list<Item>::size_type count = Itemlist.size();
  67.     cout << "남아 있는 아이템 개수 : " << count << endl;
  68.     return 0;
  69. }
결과 : 

Item Code : 2

Item Code : 1

Item Code : 3

Item Code : 4

아이템 코드 : 1

아이템 코드 : 3

남아 있는 아이템 개수 : 2

남아 있는 아이템 개수 : 0
    

   (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 );


  1. #include <iostream>
  2. #include <list>

  3. using namespace std;

  4. int main(int argc, const char * argv[])
  5. {

  6.     // insert code here...
  7.     list<int> list1;
  8.     
  9.     list1.push_back(20);
  10.     list1.push_back(30);
  11.     
  12.     /////////////////////////////////////////////////////////////
  13.     cout << "삽입 테스트 1" << endl;
  14.     
  15.     // 번째 위치에 삽입한다.
  16.     list<int>::iterator iterInsertPos = list1.begin();
  17.     list1.insert( iterInsertPos, 100);
  18.     
  19.     // 100, 20, 30순으로 출력된다.
  20.     list<int>::iterator iterEnd = list1.end();
  21.     for(list<int>::iterator iterPos = list1.begin() ; iterPos != iterEnd ; ++iterPos){
  22.         cout << "list 1 : " << *iterPos << endl;
  23.     }
  24.     
  25.     /////////////////////////////////////////////////////////////
  26.     cout << endl << "삽입 테스트 2" << endl;
  27.     
  28.     // 번째 위치에 200 2 삽입한다.
  29.     iterInsertPos = list1.begin();
  30.     ++iterInsertPos;
  31.     list1.insert( iterInsertPos, 2, 200);
  32.     
  33.     // 100,200,200,20,30 순으로 출력된다.
  34.     iterEnd = list1.end();
  35.     for(list<int>::iterator iterPos = list1.begin() ; iterPos != iterEnd ; ++iterPos){
  36.         cout << "list 1 : " << *iterPos << endl;
  37.     }
  38.     
  39.     /////////////////////////////////////////////////////////////
  40.     cout << endl << "삽입 테스트 3" << endl;
  41.     
  42.     list<int> list2;
  43.     list2.push_back( 1000 );
  44.     list2.push_back( 2000 );
  45.     list2.push_back( 3000 );
  46.     
  47.     // 번째 위치에 list2 모든 데이터를 삽입한다.
  48.     iterInsertPos = list1.begin();
  49.     list1.insert( ++iterInsertPos, list2.begin(), list2.end());
  50.     
  51.     // 100, 1000, 2000, 3000, 200, 200, 20, 30 순으로 출력된다.
  52.     iterEnd = list1.end();
  53.     for( list<int>::iterator iterPos = list1.begin() ; iterPos != iterEnd ; ++iterPos ){
  54.         cout << "list 1 : " << *iterPos << endl;
  55.     }
  56.     return 0;
  57. }
결과 

삽입 테스트 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