Skip to content

Standard Template Library

표준 템플릿 라이브러리(STL: Standard Template Library)는 C++을 위한 라이브러리로서 C++ 표준 라이브러리의 많은 부분에 영향을 끼쳤다. 이것은 알고리즘, 컨테이너, 함수자 그리고 반복자라고 불리는 네가지의 구성 요소를 제공한다.

STL은 컨테이너와 연관 배열 같은 C++을 위한 일반 클래스들의 미리 만들어진 집합을 제공하는데, 이것들은 어떤 빌트인 타입과도 그리고 어떤 사용자 정의 타입과도 같이 사용될 수 있다. STL 알고리즘들은 컨테이너들에 독립적인데, 이것은 라이브러리의 복잡성을 눈에 띄게 줄여주었다.

STL은 결과를 템플릿의 사용을 통해 달성한다. 이 접근법은 전통적인 런타임 다형성에 비해 훨씬 효과적인 컴파일 타임 다형성을 제공한다. 현대의 C++ 컴파일러들은 STL의 많은 사용에 의해 야기되는 어떤 추상화 패널티도 최소화하도록 튜닝되었다.

STL은 제네릭 알고리즘과 C++을 위한 데이터 구조체들의 첫 번째 라이브러리로서 만들어졌다. 이것은 다음의 네가지를 기초로 한다. 제네릭 프로그래밍, 효율성을 잃지 않은 추상화, 폰 노이만 구조 그리고 밸류 시멘틱스(value semantics)가 그것이다.

Algorithms

The header <algorithm> defines a collection of functions especially designed to be used on ranges of elements.

A range is any sequence of objects that can be accessed through iterators or pointers, such as an array or an instance of some of the STL containers. Notice though, that algorithms operate through iterators directly on the values, not affecting in any way the structure of any possible container (it never affects the size or storage allocation of the container).

Non-modifying sequence operations

  • all_of: Test condition on all elements in range
  • any_of: Test if any element in range fulfills condition
  • none_of: Test if no elements fulfill condition
  • for_each: Apply function to range
  • find: Find value in range
  • find_if: Find element in range
  • find_if_not: Find element in range (negative condition)
  • find_end: Find last subsequence in range
  • find_first_of: Find element from set in range
  • adjacent_find: Find equal adjacent elements in range
  • count: Count appearances of value in range
  • count_if: Return number of elements in range satisfying condition
  • mismatch: Return first position where two ranges differ
  • equal: Test whether the elements in two ranges are equal
  • is_permutation: Test whether range is permutation of another
  • search: Search range for subsequence
  • search_n: Search range for elements

Modifying sequence operations

  • copy: Copy range of elements
  • copy_n: Copy elements
  • copy_if: Copy certain elements of range
  • copy_backward: Copy range of elements backward
  • move: Move range of elements
  • move_backward: Move range of elements backward
  • swap: Exchange values of two objects
  • swap_ranges: Exchange values of two ranges
  • iter_swap: Exchange values of objects pointed by two iterators
  • transform: Transform range
  • replace: Replace value in range
  • replace_if: Replace values in range
  • replace_copy: Copy range replacing value
  • replace_copy_if: Copy range replacing value
  • fill: Fill range with value
  • fill_n: Fill sequence with value
  • generate: Generate values for range with function
  • generate_n: Generate values for sequence with function
  • remove: Remove value from range
  • remove_if: Remove elements from range
  • remove_copy: Copy range removing value
  • remove_copy_if: Copy range removing values
  • unique: Remove consecutive duplicates in range
  • unique_copy: Copy range removing duplicates
  • reverse: Reverse range
  • reverse_copy: Copy range reversed
  • rotate: Rotate left the elements in range
  • rotate_copy: Copy range rotated left
  • random_shuffle: Randomly rearrange elements in range
  • shuffle: Randomly rearrange elements in range using generator

Partitions

  • is_partitioned: Test whether range is partitioned
  • partition: Partition range in two
  • stable_partition: Partition range in two - stable ordering
  • partition_copy: Partition range into two
  • partition_point: Get partition point

Sorting

  • sort: Sort elements in range
  • stable_sort: Sort elements preserving order of equivalents
  • partial_sort: Partially sort elements in range
  • partial_sort_copy: Copy and partially sort range
  • is_sorted: Check whether range is sorted
  • is_sorted_until: Find first unsorted element in range
  • nth_element: Sort element in range

Binary search (operating on partitioned/sorted ranges)

  • lower_bound: Return iterator to lower bound
  • upper_bound: Return iterator to upper bound
  • equal_range: Get subrange of equal elements
  • binary_search: Test if value exists in sorted sequence

Merge (operating on sorted ranges)

  • merge: Merge sorted ranges
  • inplace_merge: Merge consecutive sorted ranges
  • includes: Test whether sorted range includes another sorted range
  • set_union: Union of two sorted ranges
  • set_intersection: Intersection of two sorted ranges
  • set_difference: Difference of two sorted ranges
  • set_symmetric_difference: Symmetric difference of two sorted ranges

Heap

  • push_heap: Push element into heap range
  • pop_heap: Pop element from heap range
  • make_heap: Make heap from range
  • sort_heap: Sort elements of heap
  • is_heap: Test if range is heap
  • is_heap_until: Find first element not in heap order

Min/max

  • min: Return the smallest
  • max: Return the largest
  • minmax: Return smallest and largest elements
  • min_element: Return smallest element in range
  • max_element: Return largest element in range
  • minmax_element: Return smallest and largest elements in range

Other

  • lexicographical_compare: Lexicographical less-than comparison
  • next_permutation: Transform range to next permutation
  • prev_permutation: Transform range to previous permutation

See also

Favorite site

References


  1. About_stl-cpp_stl_programming.pdf