자료구조 와 알고리즘/c++

std::list

브랑제리 2022. 4. 24. 22:55

std::list

singly-linked list는 std::forward_list 참고

  • 이중 연결 리스트(doubly-linked list) 구조로 되어 있다.
    • 양방향으로 데이터를 추가/삭제 할 수 있다.
  • 정방향, 역방향으로 리스트를 이동할 수 있다.
  • size(), push_back(), pop_back() 등 연산은 O(1) 시간복잡도를 가진다.
  • 임의의 원소를 접근시 O(N) 시간복잡도를 가진다.

https://en.cppreference.com/w/cpp/container/list

list 사용예제

#include <iostream>
#include <list>

using namespace std;

int main()
{
    std::list<int> list1 = {1, 2, 3, 4, 5};
    list1.push_back(6);
    list1.push_front(0);
    list1.insert(list1.end(), 7);

    for(int n : list1)
        cout << n << ", ";
    cout << endl;

    list1.pop_front();
    list1.pop_back();

    for(int n : list1)
        cout << n << ", ";
    cout << endl;
}
반응형

'자료구조 와 알고리즘 > c++' 카테고리의 다른 글

std::vector  (0) 2022.04.23
std::array  (0) 2022.04.23