자료 저장소

코딩 by 하얀별....(20070820)

문서 수정 20071116 .. main함수 추가, using name space 추가

                       .. 소스를 긁을 수 있도록 수정

문서 수정 20071117 .. STL Vector 사용법 정리 후,참고 자료 링크 추가

 

STL을 한번 써볼려고해도 제대로 쉽게 정리해 놓은 곳이 별로 없더군요

그래서, STL List 사용법을 나름 간단하게 정리해 보았습니다.

STL List에서의 삽입, find, 소팅, 원소삭제, 해당index에 값 넣기, 개수세기, 모든 원소 삭제, 전체 값 출력하기를 어떻게 사용해야할지 코드를 통해 살펴보도록 하겠습니다..

 

실행화면은 아래와 같습니다.

 

 

위 화면 출력을 위한 코드는 아래와 같습니다.

 

 

#include <stdio.h>
#include <list>
#include <iostream>
#include <algorithm> //find()를 위해 필요

 

using namespace std;

 

//STL LIST 테스트를 위한 함수
void stl()
{

    list<char>  list1;
 //리스트의 반복을 가리키는 반복자 선언
    list<char>::iterator itor;// = list1.begin();      // 따로, 또는 이렇게 ..
 itor=list1.begin(); //시작을 가리키도록 한다.
      //이 부분이 없으면 동작안함.
  
 //-----------------------------------------
 //값 삽입
 //-----------------------------------------

 cout<< "--- c, b, a 값 삽입 ---" << endl;
    list1.push_front('c');
    list1.push_back('b');
    list1.push_back('a');

 //처음부터 끝까지 값 출력
 for(itor=list1.begin(); itor != list1.end(); itor++)
 {
  cout<< *itor << endl;
 }

 //-----------------------------------------
 //포인터  rewind  연습
 //-----------------------------------------

 /*
 itor=list1.begin(); //앞으로 다시 감기
 //끝에 도달할때까지 데이터 출력
 while(list1.end()!=itor)
 {
  cout<< *itor << endl;
  itor++;
 }
 */
 
 //-----------------------------------------
 //find -처음부터 끝까지 뒤져서 index를 던지고 있나 본다.
 //단 include <algorithm>
 //반환값은 반복자, 즉 *itor이다.
 //-----------------------------------------

    itor=find(list1.begin(), list1.end(), 'a');
 cout<< "----- find (a를 찾아 그 위치에 d를 삽입한다.) ----" << endl;
 //찾아낸 값이 있는 앞 위치에 집어 넣기
 //없으면 맨 뒤에 들어간다.
 list1.insert(itor,'d');
 
 //처음부터 끝까지 값 출력
 for(itor=list1.begin(); itor != list1.end(); itor++)
 {
  cout<< *itor << endl;
 }
 //-----------------------------------------
 //소팅
 //-----------------------------------------

 cout<< "----- sorting ----- " << endl;
 list1.sort();
 //처음부터 끝까지 값 출력
 for(itor=list1.begin(); itor != list1.end(); itor++)
 {
  cout<< *itor << endl;
 }
 //-----------------------------------------
 //b 삭제하기
 //-----------------------------------------

 cout<< "----- remove b ----- " << endl;
 itor=find(list1.begin(), list1.end(), 'b');
 list1.erase(itor);
 for(itor=list1.begin(); itor != list1.end(); itor++)
 {
  cout<< *itor << endl;
 }

 //-----------------------------------------
 //STL에서 해당 index 위치에 값을 넣는 방법
 //STL의 index는 맨처음이 0이다.
 //-----------------------------------------

 int i=0;
 int index=2;
 cout<< "----- 해당 index 위치(2)에 값 f 넣기  ----- " << endl;
 for(itor=list1.begin(); itor!= list1.end(); itor++, i++)
 {
  if(i==index) break;
  //cout<< *itor << endl;
 }
 //삽입
 list1.insert(itor,'f');
 //전체 출력
 for(itor=list1.begin(); itor != list1.end(); itor++)
 {
  cout<< *itor << endl;
 }

 //-----------------------------------------
 // 갯수 세기
 //-----------------------------------------

 cout<< "----- 전체 리스트의 갯수 세기   ----- " << endl;
 cout<< list1.size()<< endl;;

 //-----------------------------------------
 // 모두 삭제하기
 //-----------------------------------------

 
 cout<< "----- 리스트 모두 삭제하기   ----- " << endl;
 for(itor=list1.begin(); itor != list1.end();)
 {
  //if(list1.empty()) break;
  list1.erase(itor++); //erase의 경우 후위형 연산자로만 증가시켜야함
 }
 
 list1.clear();

 //전체 출력
 for(itor=list1.begin(); itor != list1.end(); itor++)
 {
  //if(list1.empty()) break;
  cout<< *itor << endl;
 }
 
}//End of stl()

 

void main(){
 stl();
}

 

------------------------------------------------------------------------------------------

 
출처 : 네이버 블로그 http://blog.naver.com/wono77/140041557617
댓글 로드 중…

최근에 게시된 글