이 포스팅은 Object Oriented Programming 시리즈 23 편 중 19 번째 글 입니다.

  • Part 1 - 01: Introduction
  • Part 2 - 02: Identifier, Variable, constant, Std IO, Operator
  • Part 3 - 03: Functions #1 - Calling (호출)
  • Part 4 - 04: Functions #2 - Local, Global Variable
  • Part 5 - 05: Functions #3 - Recursion Function, Reference Variable (재귀함수)
  • Part 6 - 06: Functions #4 - Reference Variable vs. Pointer
  • Part 7 - 07: Functions #5 - CallbyValue, CallbyReference
  • Part 8 - 08: Selection and Repetition
  • Part 9 - 09: File Input & Output (파일입출력)
  • Part 10 - 10: String library, rand(), srand()
  • Part 11 - 11: Pointer, Function Pointer
  • Part 12 - 12: Array, Vector (정적배열, 동적배열)
  • Part 13 - 13: class, object
  • Part 14 - 14: this, operator overloading
  • Part 15 - 15: friend, static, destructor
  • Part 16 - 16: Inherence (상속)
  • Part 17 - 17: Static Binding, Dynamic Binding, Header File
  • Part 18 - 18: Generic Programming, Template
  • Part 19 - This Post
  • Part 20 - 20: Iterator (반복자)
  • Part 21 - 21: algorithm Library
  • Part 22 - 21: functional, lambda function
  • Part 23 - 22: Exception handling
▼ 목록 보기

목차

▼ 내리기

List

이제껏 vector container 에 대해서 집중적으로 사용했는데, list container 역시 vector와 마찬가지로 많이 사용된다.

Vector

  • 장점 : search가 빠르다.
  • 단점 : pop/ push 가 느리다.

요소의 내용이 바뀔일이 많이 없으면 Vector 사용

List

  • 장점 : search가 느리다.
  • 단점 : pop/ push 가 빠르다.

요소의 내용이 바뀔일이 많으면 List 사용

기본적으로 가지는 method는 같기 때문에, 쉽게 사용가능하다.

#include <iostream>
#include <list>

using namespace std;


int main(){
    list<int> myList{1,2,3,4};
    char command;
    int inputVal;
    bool finished = false;
    while(!finished){
        cout << "I)nput, P)rint, L)ength, E)mpty, Q)uit : ";
        cin >> command;
        switch (command) {
            case 'I':
            case 'i':
                cin >> inputVal;
                myList.push_back(inputVal);
                break;
            case 'P':
            case 'p':
                for(auto elem:myList)
                    cout << elem << '\t';
                cout << endl;
                break;
            case 'L':
            case 'l':
                cout << "Number of items : " << myList.size() << endl;
                break;
            case 'E':
            case 'e':
                myList.clear();
                break;
            case 'Q':
            case 'q':
                finished = true;
                cout << "Exit the program" << endl;
                break;

            default:
                cout << "Wrong command" << endl;
                break;
        }
    }

    return 0;
}
I)nput, P)rint, L)ength, E)mpty, Q)uit : I
1
I)nput, P)rint, L)ength, E)mpty, Q)uit : P
1	2	3	4	1
I)nput, P)rint, L)ength, E)mpty, Q)uit : L
Number of items : 5
I)nput, P)rint, L)ength, E)mpty, Q)uit : E
I)nput, P)rint, L)ength, E)mpty, Q)uit : P

I)nput, P)rint, L)ength, E)mpty, Q)uit : Q
Exit the program
Program ended with exit code: 0