• Tistory
    • 태그
    • 위치로그
    • 방명록
    • 관리자
    • 글쓰기
Carousel 01
Carousel 02
Previous Next

STL List

Programming/C, C++, MFC 2014. 6. 9. 20:12





난 List 를 사용할 줄 몰랐다.


주변에서 보니 List 를 사용하는 모습을 종종 보게 되는데 


잘 사용하면 편리할 것 같다.


잘 정리 해 둔 블로그가 있어 옮겨 적어놓기.


[출처] http://blog.naver.com/wono77/140041557617



  1. #include <stdio.h>
  2. #include <list>
  3. #include <iostream>
  4. #include <algorithm> //find()를 위해 필요
  5.  
  6. using namespace std;
  7.  
  8. //STL LIST 테스트를 위한 함수
  9. void stl()
  10. {
  11.     list<char>  list1;
  12.  //리스트의 반복을 가리키는 반복자 선언
  13.     list<char>::iterator itor;// = list1.begin();      // 따로, 또는 이렇게 ..
  14.  itor=list1.begin(); //시작을 가리키도록 한다.
  15.       //이 부분이 없으면 동작안함.
  16.  
  17.  //-----------------------------------------
  18.  //값 삽입
  19.  //-----------------------------------------
  20.  cout<< "--- c, b, a 값 삽입 ---" << endl;
  21.     list1.push_front('c');
  22.     list1.push_back('b');
  23.     list1.push_back('a');
  24.  //처음부터 끝까지 값 출력
  25.  for(itor=list1.begin(); itor != list1.end(); itor++)
  26.  {
  27.   cout<< *itor << endl;
  28.  }
  29.  //-----------------------------------------
  30.  //포인터  rewind  연습
  31.  //-----------------------------------------
  32.  /*
  33.  itor=list1.begin(); //앞으로 다시 감기
  34.  //끝에 도달할때까지 데이터 출력
  35.  while(list1.end()!=itor)
  36.  {
  37.   cout<< *itor << endl;
  38.   itor++;
  39.  }
  40.  */
  41.  
  42.  //-----------------------------------------
  43.  //find -처음부터 끝까지 뒤져서 index를 던지고 있나 본다.
  44.  //단 include <algorithm>
  45.  //반환값은 반복자, 즉 *itor이다.
  46.  //-----------------------------------------
  47.     itor=find(list1.begin(), list1.end(), 'a');
  48.  cout<< "----- find (a를 찾아 그 위치에 d를 삽입한다.) ----" << endl;
  49.  //찾아낸 값이 있는 앞 위치에 집어 넣기
  50.  //없으면 맨 뒤에 들어간다.
  51.  list1.insert(itor,'d');
  52.  
  53.  //처음부터 끝까지 값 출력
  54.  for(itor=list1.begin(); itor != list1.end(); itor++)
  55.  {
  56.   cout<< *itor << endl;
  57.  }
  58.  //-----------------------------------------
  59.  //소팅
  60.  //-----------------------------------------
  61.  cout<< "----- sorting ----- " << endl;
  62.  list1.sort();
  63.  //처음부터 끝까지 값 출력
  64.  for(itor=list1.begin(); itor != list1.end(); itor++)
  65.  {
  66.   cout<< *itor << endl;
  67.  }
  68.  //-----------------------------------------
  69.  //b 삭제하기
  70.  //-----------------------------------------
  71.  cout<< "----- remove b ----- " << endl;
  72.  itor=find(list1.begin(), list1.end(), 'b');
  73.  list1.erase(itor);
  74.  for(itor=list1.begin(); itor != list1.end(); itor++)
  75.  {
  76.   cout<< *itor << endl;
  77.  }
  78.  //-----------------------------------------
  79.  //STL에서 해당 index 위치에 값을 넣는 방법
  80.  //STL의 index는 맨처음이 0이다.
  81.  //-----------------------------------------
  82.  int i=0;
  83.  int index=2;
  84.  cout<< "----- 해당 index 위치(2)에 값 f 넣기  ----- " << endl;
  85.  for(itor=list1.begin(); itor!= list1.end(); itor++, i++)
  86.  {
  87.   if(i==index) break;
  88.   //cout<< *itor << endl;
  89.  }
  90.  //삽입
  91.  list1.insert(itor,'f');
  92.  //전체 출력
  93.  for(itor=list1.begin(); itor != list1.end(); itor++)
  94.  {
  95.   cout<< *itor << endl;
  96.  }
  97.  //-----------------------------------------
  98.  // 갯수 세기
  99.  //-----------------------------------------
  100.  cout<< "----- 전체 리스트의 갯수 세기   ----- " << endl;
  101.  cout<< list1.size()<< endl;;
  102.  //-----------------------------------------
  103.  // 모두 삭제하기
  104.  //-----------------------------------------
  105.  
  106.  cout<< "----- 리스트 모두 삭제하기   ----- " << endl;
  107.  for(itor=list1.begin(); itor != list1.end();)
  108.  {
  109.   //if(list1.empty()) break;
  110.   list1.erase(itor++); //erase의 경우 후위형 연산자로만 증가시켜야함
  111.  }
  112.  
  113.  list1.clear();
  114.  //전체 출력
  115.  for(itor=list1.begin(); itor != list1.end(); itor++)
  116.  {
  117.   //if(list1.empty()) break;
  118.   cout<< *itor << endl;
  119.  }
  120.  
  121. }//End of stl()
  122.  
  123. void main(){
  124.  stl();
  125. }


실행결과는 다음과 같다.




저작자표시 비영리 (새창열림)

'Programming > C, C++, MFC' 카테고리의 다른 글

변수의 메모리 할당과 #pragma pack()  (0) 2014.07.23
함수의 파라미터로 포인터를 사용  (0) 2014.07.02
[Function][MFC] 폴더 내 모든파일 삭제하기  (0) 2014.03.25
[Function][MFC] 폴더 내 파일 갯수 확인하기  (0) 2014.03.25
프로그램 실행시간을 측정 해 보자!  (0) 2014.02.12
블로그 이미지

매직블럭

작은 지식들 그리고 기억 한조각

,

카테고리

  • 살다보니.. (449)
    • 주절거림 (3)
    • 취미생활 (36)
      • 지식과 지혜 (3)
      • 풍경이 되어 (4)
      • Memories (17)
      • 엥겔지수를 높여라 (2)
    • mathematics (6)
      • Matrix Computation (2)
      • RandomProcesses (3)
    • English.. (8)
    • Programming (147)
      • C, C++, MFC (51)
      • C# (1)
      • OpenCV (17)
      • Python (58)
      • Git, Docker (3)
      • Matlab (4)
      • Windows (3)
      • Kinect V2 (2)
      • 기타 etc. (8)
    • 전공관련 (80)
      • Algorithm (6)
      • Deep Learning (54)
      • 실습 프로그램 (4)
      • 주워들은 용어정리 (8)
      • 기타 etc. (8)
    • Computer (118)
      • Utility (21)
      • Windows (31)
      • Mac (4)
      • Ubuntu, Linux (58)
      • NAS (2)
      • Embedded, Mobile (2)
    • IT, Device (41)
      • 제품 사용기, 개봉기 (14)
      • 스마트 체험단 신청 (27)
    • Wish List (3)
    • TISTORY TIP (5)
    • 미분류. 수정중 (1)

태그목록

  • utility
  • 오봉자싸롱
  • random variable
  • CStdioFile
  • ColorMeRad
  • matlab function
  • 스마트체험단
  • 딥러닝
  • Deep Learning
  • 갤럭시노트3
  • ReadString
  • DeepLearning
  • 후쿠오카
  • 칼로리 대폭발
  • portugal
  • 포르투갈
  • review
  • matlab
  • Computer Tip
  • 에누리닷컴
  • 매트랩 함수
  • Convolutional Neural Networks
  • 매트랩
  • 일본
  • SVM
  • function
  • LIBSVM
  • 큐슈
  • DSLR
  • 크롬

달력

«   2025/06   »
일 월 화 수 목 금 토
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
06-12 22:04

LATEST FROM OUR BLOG

RSS 구독하기

BLOG VISITORS

  • Total :
  • Today :
  • Yesterday :

Copyright © 2015 Socialdev. All Rights Reserved.

티스토리툴바