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