본문 바로가기
IT, 컴퓨터

[C++] 자료구조 std::list의 특징 및 사용법에 대해서

by 별찌파파 2024. 1. 17.
728x90
반응형
SMALL

 

728x90
반응형

Photo by Markus Spiske on Unsplash

list의 특징

 

C++ 표준 라이브러리(STL)에서 제공하는 std::list는 이중 연결 리스트(double-linked list)를 구현한 자료구조입니다. 이중 연결 리스트는 각 원소가 이전 원소와 다음 원소에 대한 포인터를 가지고 있는 구조입니다. std::list는 다음과 같은 특징을 가지고 있습니다:

  1. 데이터 구조:
    • 이중 연결 리스트로 구현되어 있어 각 노드가 이전 노드와 다음 노드에 대한 포인터를 가지고 있습니다.
  2. 헤더 파일:
    • #include <list>를 통해 사용할 수 있습니다.
  3. 선언 및 초기화:
    • std::list는 다양한 초기화 방법을 지원합니다.
      #include <list>
      
      // 비어있는 리스트 선언
      std::list<int> myList;
      
      // 초기값을 가지는 리스트 선언
      std::list<int> myList = {1, 2, 3, 4, 5};​
  4. 원소의 삽입 및 삭제:
    • push_back, push_front, pop_back, pop_front, insert 등을 사용하여 원소를 삽입하거나 삭제할 수 있습니다.
      myList.push_back(6);    // 리스트의 뒤쪽에 6 추가
      myList.push_front(0);   // 리스트의 앞쪽에 0 추가
      myList.pop_back();      // 리스트의 뒤쪽에서 원소 제거
      myList.pop_front();     // 리스트의 앞쪽에서 원소 제거
      
      // 특정 위치에 원소 삽입
      auto it = std::find(myList.begin(), myList.end(), 3);
      myList.insert(it, 7);​
  5. 임의 접근 및 반복자:
    • begin(), end(), rbegin(), rend() 등의 반복자를 통해 원소에 접근하고 순회할 수 있습니다.
      for (auto it = myList.begin(); it != myList.end(); ++it) {
          // 반복자를 사용한 리스트 순회
      }​
  6. 기타 멤버 함수:
    • size(), empty(), clear(), front(), back() 등 다양한 멤버 함수를 제공합니다.

이중 연결 리스트의 특성상 임의의 위치에서의 삽입과 삭제가 빠르지만, 임의 접근에는 비효율적일 수 있습니다. 리스트는 중간에 원소를 추가하거나 삭제해야 하는 경우에 유용하게 사용됩니다.

 

list의 예제

예제 1: 리스트에 원소 추가하고 출력하기

#include <iostream>
#include <list>

int main() {
    // 리스트 생성 및 초기화
    std::list<int> myList = {1, 2, 3, 4, 5};

    // 리스트 뒤쪽에 원소 추가
    myList.push_back(6);

    // 리스트 앞쪽에 원소 추가
    myList.push_front(0);

    // 리스트 출력
    std::cout << "List elements: ";
    for (const auto& element : myList) {
        std::cout << element << " ";
    }
    std::cout << std::endl;

    return 0;
}

 

예제 2: 리스트에서 특정 원소 제거하기

#include <iostream>
#include <list>

int main() {
    // 리스트 생성 및 초기화
    std::list<int> myList = {1, 2, 3, 4, 5};

    // 리스트에서 특정 값 제거
    myList.remove(3);

    // 리스트 출력
    std::cout << "List elements: ";
    for (const auto& element : myList) {
        std::cout << element << " ";
    }
    std::cout << std::endl;

    return 0;
}

 

예제 3: 리스트에서 홀수값만 남기고 짝수값 제거하기

#include <iostream>
#include <list>

int main() {
    // 리스트 생성 및 초기화
    std::list<int> myList = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    // 홀수값만 남기고 짝수값 제거
    myList.remove_if([](int x) { return x % 2 == 0; });

    // 리스트 출력
    std::cout << "List elements: ";
    for (const auto& element : myList) {
        std::cout << element << " ";
    }
    std::cout << std::endl;

    return 0;
}

 

예제 4: 리스트를 사용한 문자열 역순 출력

#include <iostream>
#include <list>
#include <string>

int main() {
    // 문자열을 문자 단위로 리스트에 저장
    std::list<char> charList;
    std::string myString = "Hello, World!";
    for (char c : myString) {
        charList.push_back(c);
    }

    // 리스트를 사용하여 문자열 역순 출력
    std::cout << "Reversed String: ";
    for (auto it = charList.rbegin(); it != charList.rend(); ++it) {
        std::cout << *it;
    }
    std::cout << std::endl;

    return 0;
}

 

예제 5: 리스트를 사용한 합계 계산

#include <iostream>
#include <list>

int main() {
    // 리스트 생성 및 초기화
    std::list<int> myList = {1, 2, 3, 4, 5};

    // 리스트 원소의 합계 계산
    int sum = 0;
    for (const auto& element : myList) {
        sum += element;
    }

    std::cout << "Sum of List Elements: " << sum << std::endl;

    return 0;
}

 

예제 6: 두 리스트를 합치기

#include <iostream>
#include <list>

int main() {
    // 두 리스트 생성 및 초기화
    std::list<int> list1 = {1, 2, 3};
    std::list<int> list2 = {4, 5, 6};

    // 두 리스트 합치기
    list1.insert(list1.end(), list2.begin(), list2.end());

    // 합쳐진 리스트 출력
    std::cout << "Merged List: ";
    for (const auto& element : list1) {
        std::cout << element << " ";
    }
    std::cout << std::endl;

    return 0;
}

 

728x90
반응형
LIST