Search
πŸ“¦

ft_containers

Created
2022/07/25 04:29
Updated
2022/08/25 08:36
Tags
Circle 04
container
CPP
Data Structure
template
Author
Created
Updated
mcha(Min-jae Cha)
2022. 07. 25.
2022. 08. 25.
C++ containers, EASY MODE The standard C++ containers have all a specific usage. To make sure you understand them, let’s re-implement them!

Β Intro

CPP Module을 μˆ˜ν–‰ν•˜λ‹€λ³΄λ©΄ 데이터λ₯Ό μ €μž₯ν•  수 μžˆλŠ” CPP의 μ—¬λŸ¬ Containerλ₯Ό μ ‘ν•  수 μžˆλ‹€.
λ³Έ 과제의 μˆ˜ν–‰ λͺ©μ μ€ CPP container의 μš©λ„μ™€ νŠΉμ§•μ„ νŒŒμ•…ν•˜λ©° μž¬κ΅¬μ„±, κ΅¬ν˜„ ν•˜λŠ” 것이닀.

Β Before to go

1. Compile time polymorphism

CPPμ—λŠ” λ‹€ν˜•μ„±(Polymorphism)μ΄λΌλŠ” κ°œλ…μ΄ μ‘΄μž¬ν•œλ‹€. μ•„λ§ˆ CPP Module 과제λ₯Ό μˆ˜ν–‰ν•˜λ©° 이 λ‹€ν˜•μ„± κ°œλ…μ„ κ³΅λΆ€ν•œ κ²½ν—˜μ΄ μžˆμ„ 것이닀.
λ‹€ν˜•μ„±μ„ 톡해 ν•¨μˆ˜λ₯Ό κ²°μ •ν•˜λŠ” μ‹œμ μ΄ 두 κ°œκ°€ μ‘΄μž¬ν•˜λŠ”λ° 첫 λ²ˆμ§ΈλŠ” 컴파일(Compile) νƒ€μž„ λ•Œ, 두 λ²ˆμ§ΈλŠ” λŸ°νƒ€μž„(Runtime) λ•Œμ΄λ‹€.
같은 μ΄λ¦„μ˜ ν•¨μˆ˜λ₯Ό μ—¬λŸ¬ 개 λ§Œλ“€μ–΄ 후보ꡰ을 λ‘λŠ” ν•¨μˆ˜ μ˜€λ²„λ‘œλ”©(Overloading)은 컴파일(Compile) λ•Œ, λΆ€λͺ¨ 클래슀의 ν•¨μˆ˜λ₯Ό μž¬μ •μ˜ ν•œ ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λŠ” μ˜€λ²„λΌμ΄λ”©(Overriding)λŠ” λŸ°νƒ€μž„(Runtime) λ•Œ κ²°μ • λœλ‹€.
μ˜ˆμ‹œ
// Compile time int add(int, int); int add(int, int, int); //number of parameters different double add(double, double); //type of parameters different // Runtime #include <iostream> using namespace std; class base { public: virtual void display() { cout<<"Function of base class"<<endl; } }; class derived : public base { public: void display() { cout<<"Function of derived class"<<endl; } };
C++
볡사
λ³Έ 과제λ₯Ό μˆ˜ν–‰ν•˜λ‹€λ³΄λ©΄ 컴파일 νƒ€μž„ λ•Œ ν•¨μˆ˜ μ˜€λ²„λ‘œλ”© 뿐만 μ•„λ‹ˆλΌ ν…œν”Œλ¦Ώμ„ ν™œμš©ν•œ νƒ€μž… μΆ”λ‘ , ν…œν”Œλ¦Ώ 메타 ν•¨μˆ˜ λ“± λ§Žμ€ μž‘μ—…μ΄ 이루어지고 κ·Έ μ½”λ“œκ°€ λ§Žμ€ 것을 μ•Œ 수 μžˆλŠ”λ°, 컴파일 νƒ€μž„μ— μœ„μ™€ 같은 μž‘μ—…μ΄ 수반될 λ•Œ λ‚˜νƒ€λ‚˜λŠ” μž₯점이 무엇이 μžˆλŠ”μ§€ μ•Œμ•„λ³΄μ•˜λ‹€.
컴파일 νƒ€μž„ λ•Œ μž‘μ—…μ„ μˆ˜ν–‰ν•˜λ©΄ μ‹€ν–‰ μ‹œκ°„μ΄ 크게 μ€„μ–΄λ“œλŠ” μž₯점이 μžˆλ‹€. staticκ³Ό 같은 정적 값은 컴파일 νƒ€μž„ λ•Œ κ³„μ‚°λ˜κΈ° λ•Œλ¬Έμ— λŸ°νƒ€μž„ λ•Œ μ΄λŸ¬ν•œ 계산 μž‘μ—…μ„ 크게 쀄일 수 μžˆλ‹€. λ”ν•΄μ„œ λ§Œμ•½ 였랜 μ‹œκ°„μ΄ κ±Έλ¦¬λŠ” 루프 싀행문을 컴파일 νƒ€μž„ λ•Œ μˆ˜ν–‰ν•œλ‹€λ©΄ λ§ˆμ°¬κ°€μ§€λ‘œ λŸ°νƒ€μž„μ„ 쀄일 수 μžˆλ‹€. μ΄λŸ¬ν•œ 방법은 κ°•λ ₯ν•œ μ΅œμ ν™” λ°©λ²•μœΌλ‘œ μ†Œκ°œλ˜κ³  μžˆλ‹€. ν•˜μ§€λ§Œ 이 같은 경우 쀄어든 λŸ°νƒ€μž„ 만큼 컴파일 νƒ€μž„μ΄ κΈΈμ–΄μ§ˆ 수 μžˆλ‹€λŠ” 단점도 μ‘΄μž¬ν•œλ‹€.
λŸ°νƒ€μž„ λ•Œ λ°œμƒν•  수 μžˆλŠ” μ˜ˆμΈ‘ν•˜μ§€ λͺ»ν•œ 였λ₯˜λ₯Ό 컴파일 νƒ€μž„ λ•Œ μž‘μ„ 수 μžˆλ‹€λŠ” μž₯점도 μ‘΄μž¬ν•œλ‹€. CPPλŠ” ν…œν”Œλ¦Ώμ„ ν†΅ν•œ λ‹€ν˜•μ„±(Polymorphism)을 κ΅¬ν˜„ν•˜λŠ”λ° 이 ν…œν”Œλ¦Ώ ν”„λ‘œκ·Έλž˜λ°μ€ 컴파일 νƒ€μž„ λ•Œ μ½”λ“œλ₯Ό μž‘μ„±ν•΄μ£Όμ–΄ λŸ°νƒ€μž„ λ•Œ λ©”λͺ¨λ¦¬μ™€ μˆ˜ν–‰ μ‹œκ°„μ„ 크게 쀄일 수 μžˆλŠ” μž₯점이 μžˆλ‹€. ν•˜μ§€λ§Œ λ§Œμ•½ 컴파일 νƒ€μž„ λ•Œ ν…œν”Œλ¦Ώ μž‘μ—…μ„ μˆ˜ν–‰ν•˜μ§€ μ•ŠλŠ”λ‹€λ©΄ μ μ ˆν•˜μ§€ μ•Šμ€ νƒ€μž…μ΄ 듀어왔을 λ•Œ 예기치 λͺ»ν•œ 상황이 λ°œμƒν•  수 μžˆλ‹€.
μ˜ˆμ‹œ
template <typename _Type> void example(void) { _Type &a; a.fnc(); // λ§Œμ•½ _Type 내에 fncλΌλŠ” ν•¨μˆ˜κ°€ μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ”λ‹€λ©΄? }
C++
볡사

2. SFINAE

Substitution failure is not an error의 μ•½μžλ‘œ, β€˜λŒ€μ²΄ μ‹€νŒ¨λŠ” 였λ₯˜κ°€ μ•„λ‹ˆλ‹€β€™λΌλŠ” λœ»μ΄λ‹€.
ν•˜λ‚˜μ˜ ν•¨μˆ˜ 이름을 μ‚¬μš©ν•˜μ—¬ μ—¬λŸ¬ μž‘μ—…μ„ μˆ˜ν–‰ν•˜κ³ μž ν•  λ•Œ ν•¨μˆ˜ μ˜€λ²„λ‘œλ“œ 후보ꡰ을 λ§Œλ“ λ‹€. 그리고 후보ꡰ이 ν…œν”Œλ¦ΏμœΌλ‘œ 생성 λ˜μ—ˆλ‹€λ©΄ κ·Έ ν…œν”Œλ¦Ώ μΈμžμ— μžλ£Œν˜•μ΄λ‚˜ 값이 λŒ€μž…μ΄ λ˜λŠ” κ²½μš°κ°€ μžˆμ„ 수 μžˆλ‹€.
κ·Έ ν›„ 컴파일 νƒ€μž„ λ•Œ μ μ ˆν•œ 후보ꡰ을 μ°ΎλŠ” 과정이 μˆ˜ν–‰ λ˜λŠ”λ° λ§Œμ•½ λŒ€μ²΄ κ³Όμ • 쀑 였λ₯˜κ°€ λ°œμƒν•΄λ„ C++ ν‘œμ€€μ΄ ν—ˆμš©ν•œλ‹€λ©΄ μ»΄νŒŒμΌλŸ¬λŠ” 컴파일 였λ₯˜λ₯Ό λ‚˜νƒ€λ‚΄μ§€ μ•Šκ³  κ·Έ 후보λ₯Ό ν›„λ³΄κ΅°μ—μ„œ μ œμ™Έν•˜λŠ” μž‘μ—…λ§Œ μˆ˜ν–‰ν•œλ‹€.
μ˜ˆμ‹œ
#include <iostream> struct TEST { // rename int as test typedef int test; }; // 01 template <typename T> void fnc(typename T::test) { std::cout << "Type in the structure" << std::endl; } // 02 // λ§Œμ•½ 이 ν•¨μˆ˜κ°€ μ—†λ‹€λ©΄, μ•„λž˜μ˜ main문은 λ°˜λ“œμ‹œ 였λ₯˜λ₯Ό 좜λ ₯ν•œλ‹€. template <typename T> void fnc(T) { std::cout << "Only typename T function" << std::endl; } int main(void) { fnc<TEST>(10); // TEST μ•ˆμ— 'test'κ°€ 있기 λ•Œλ¬Έμ— μ •μƒμ μœΌλ‘œ μˆ˜ν–‰ fnc<int>(10); // int μ•ˆμ— 'test'κ°€ μ—†κΈ° λ•Œλ¬Έμ— 'typename T::test' // μ—μ„œ 였λ₯˜κ°€ λ°œμƒν•˜λ‚˜, SFINAE에 따라 ν›„λ³΄μ—μ„œ μ œμ™Έν•¨ // 이후 λŒ€μ²΄ ν•¨μˆ˜κ°€ λ‚¨μ•„μžˆμ–΄ 였λ₯˜ 없이 정상 μˆ˜ν–‰ return (0); }
C++
볡사
μ•„λž˜μ˜ κ²½μš°λŠ” SFINAE κ°œλ…μ΄ 잘 적용 된 것 처럼 보인닀. ν•˜μ§€λ§Œ 3W(-Wall -Werror -Wextra) μ˜΅μ…˜κ³Ό 같이 μ»΄νŒŒμΌμ„ 해보면 μ—λŸ¬κ°€ λ°œμƒν•˜λŠ” 것을 μ•Œ 수 μžˆλ‹€.
μ˜ˆμ‹œ
#include <iostream> struct TEST { // rename int as test typedef int test; }; template <typename T> void fnc(typename T::test) { std::cout << "Type in the structure" << std::endl; std::cout << sizeof(typename T::test) << std::endl; // diff } template <typename T> void fnc(T) { std::cout << "Only typename T function" << std::endl; std::cout << sizeof(typename T::test) << std::endl; // diff } int main(void) { fnc<TEST>(10); fnc<int>(10); return (0); }
C++
볡사
μ™œλƒν•˜λ©΄ SFINAEκ°€ μ μš©λ˜λŠ” μ‹œμ μ„ 벗어났기 λ•Œλ¬ΈμΈλ°, 이 λ¬Έμ„œμ˜ SFINAE ν•­λͺ©μ„ 보면 λ‹€μŒκ³Ό 같이 κΈ°μˆ λ˜μ–΄ μžˆλ‹€.
Only the failures in the types and expressions in theΒ immediate context of the function type or its template parameter typesΒ or itsΒ explicit specifier (since C++20)Β are SFINAE errors. If the evaluation of a substituted type/expression causes a side-effect such as instantiation of some template specialization, generation of an implicitly-defined member function, etc, errors in those side-effects are treated as hard errors.
ν•¨μˆ˜ μœ ν˜• λ˜λŠ” ν…œν”Œλ¦Ώ νŒŒλΌλ―Έν„° μœ ν˜• λ˜λŠ” λͺ…μ‹œμ  μ§€μ •μžμ˜ 직접적인 λ§₯λ½μ—μ„œ λ°œμƒν•˜λŠ” 였λ₯˜λ§Œ SFINAE μ—λŸ¬λ‘œ μ·¨κΈ‰ν•œλ‹€. 그리고 λŒ€μ²΄ 이후에 λ°œμƒλ˜λŠ” μ—λŸ¬λŠ” ν•˜λ“œ μ—λŸ¬λ‘œ μ·¨κΈ‰ν•œλ‹€.
λ”°λΌμ„œ void fnc(T) ν•¨μˆ˜ λ‚΄μ˜ 일반 싀행문인 sizeof(typename T::test) λŠ” SFINAE의 λ²”μ£Όμ—μ„œ λ²—μ–΄λ‚˜ μ—λŸ¬κ°€ λ°œμƒν•œλ‹€.

3. ν…œν”Œλ¦Ώ 메타 ν•¨μˆ˜

Β ν•œ 번 읽어보면 맀우 쒋을 κΈ€
λ‹€μŒμœΌλ‘œ μ§„ν–‰ν•˜κΈ° μ „ ν…œν”Œλ¦Ώ 메타 ν•¨μˆ˜κ°€ 무엇인지 μ•Œ ν•„μš”κ°€ μžˆλ‹€. ν…œν”Œλ¦Ώ 메타 ν•¨μˆ˜λŠ” 컴파일 νƒ€μž„μ— ν•¨μˆ˜λŠ” μ•„λ‹ˆμ§€λ§Œ 마치 ν•¨μˆ˜μ²˜λŸΌ λ™μž‘ν•˜λŠ” ν…œν”Œλ¦Ώ ν΄λž˜μŠ€λ“€μ„ λ§ν•œλ‹€. 일반 ν•¨μˆ˜λŠ” κ°’(Value)에 λŒ€ν•΄ μ—°μ‚°ν•˜μ§€λ§Œ ν…œν”Œλ¦Ώ 메타 ν•¨μˆ˜λŠ” νƒ€μž…(Type)에 λŒ€ν•΄ μ—°μ‚°ν•œλ‹€.
예제 01
#include <iostream> bool is_negative(int nbr) { if (nbr < 0) return true; return false; } int main(void) { if (is_negative(1)) { std::cout << "negative" << std::endl; } else { std::cout << "positive" << std::endl; } return (0); }
C++
볡사
μœ„μ˜ μ˜ˆμ œλŠ” 일반 ν•¨μˆ˜λ₯Ό 톡해 νŒŒλΌλ―Έν„°κ°€ μŒμˆ˜μΈμ§€ μ–‘μˆ˜μΈμ§€ κ΅¬λΆ„ν•˜λŠ” μ½”λ“œμ΄λ‹€.
예제 02
#include <iostream> template <typename _Tp> void catch_type() { if (std::is_void<_Tp>::value) // ν…œν”Œλ¦Ώ 메타 ν•¨μˆ˜ { std::cout << "void" << std::endl; } else { std::cout << "not void" << std::endl; } } int main(void) { catch_type<void>(); catch_type<int>(); return (0); }
C++
볡사
예제 02λŠ” catch_type을 ν…œν”Œλ¦Ώμ„ μ‚¬μš©ν•΄ κ΅¬ν˜„ν•˜κ³  ifλ¬Έ λ‚΄μ˜ is_void ν…œν”Œλ¦Ώ 메타 ν•¨μˆ˜λ₯Ό 톡해 type이 void인지 μ•„λ‹Œμ§€λ₯Ό 좜λ ₯ν•˜λŠ” μ˜ˆμ‹œμ΄λ‹€.
μ—¬κΈ°μ„œ is_voidλŠ” type_traits 라이브러리 내에 μžˆλŠ” ꡬ쑰체둜 ν•¨μˆ˜μ²˜λŸΌ λ™μž‘ν•˜λŠ” ν…œν”Œλ¦Ώ 클래슀(ꡬ쑰체)이닀.
type_traits λŠ” νƒ€μž…μ— 따라 μ—¬λŸ¬ 연산을 μˆ˜ν–‰ν•  수 μžˆλŠ” ν…œν”Œλ¦Ώ 메타 ν•¨μˆ˜λ₯Ό μ œκ³΅ν•˜λŠ” λΌμ΄λΈŒλŸ¬λ¦¬μ΄λ‹€.
// λͺ¨λ“  νƒ€μž…μ— λŒ€μ‘ν•˜λŠ” ν…œν”Œλ¦Ώ 메타 ν•¨μˆ˜ template <class _Tp> struct __libcpp_is_void : public false_type {}; // void νƒ€μž…μ—λ§Œ λŒ€μ‘ν•˜λŠ” ν…œν”Œλ¦Ώ 메타 ν•¨μˆ˜ template <> struct __libcpp_is_void<void> : public true_type {}; // μœ„μ˜ __libcpp_is_voidλ₯Ό μƒμ†λ°›λŠ” is_void template <class _Tp> struct _LIBCPP_TEMPLATE_VIS is_void : public __libcpp_is_void<typename remove_cv<_Tp>::type> {};
C++
볡사
 과제λ₯Ό μˆ˜ν–‰ν•˜λ©΄μ„œ 특히 λˆˆμ— λ„λŠ” ν…œν”Œλ¦Ώ 메타 ν•¨μˆ˜κ°€ μžˆμ—ˆλŠ”λ° 정리λ₯Ό μœ„ν•΄μ„œ μ•„λž˜μ— 기둝을 남긴닀.
template <class _Tp, _Tp __v> struct _LIBCPP_TEMPLATE_VIS integral_constant { static _LIBCPP_CONSTEXPR const _Tp value = __v; // __vλ₯Ό static으둜 κ°€μ§€λŠ” ν…œ.λ©”.함 typedef _Tp value_type; typedef integral_constant type; _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR operator value_type() const _NOEXCEPT { return value; } #if _LIBCPP_STD_VER > 11 _LIBCPP_INLINE_VISIBILITY constexpr value_type operator()() const _NOEXCEPT { return value; } #endif };
C++
볡사
integral_constantλŠ” μ—¬λŸ¬ ν…œν”Œλ¦Ώ 메타 ν•¨μˆ˜λ₯Ό μ‘°ν•©ν•΄ 쑰건을 λ”°μ§ˆ λ•Œ 많이 μ‚¬μš©ν–ˆλŠ”λ° μ˜ˆμ‹œλŠ” λ‹€μŒκ³Ό κ°™λ‹€.
template <class _Tp> struct _LIBCPP_TEMPLATE_VIS is_class : public integral_constant<bool, __is_class(_Tp)> {};
C++
볡사
μœ„μ—μ„œ μ‚¬μš©ν–ˆλ˜ is_void처럼 is_classλŠ” ν΄λž˜μŠ€μΈμ§€ μ•„λ‹Œμ§€λ₯Ό λ‚˜νƒ€λ‚΄λŠ” 메타 ν•¨μˆ˜μ΄λ‹€.
if (std::is_class<_Tp>::value) // μ΄ν•˜ μƒλž΅
C++
볡사
μœ„μ™€ 같이 valueκ°€ true인지 false인지λ₯Ό λΉ„κ΅ν•˜λŠ”λ° 이 valueλŠ” integral_constant의 value이닀.
integral_constant의 valueλŠ” __v와 같은데, is_class의 상속 ν˜•νƒœλ₯Ό 보면 __is_class(_Tp)와 같은 것을 μ•Œ 수 μžˆλ‹€. 결둠적으둜 __is_class(_Tp)의 μ‹€ν–‰ ꡬ쑰가 μ–΄λ–»κ²Œ λ˜μ–΄ μžˆλŠ”μ§€λŠ” λͺ¨λ₯΄μ§€λ§Œ 이 결과값이 integral_constant의 valueλ₯Ό κ²°μ •μ§“λŠ” 것을 μ•Œ 수 μžˆλ‹€.

4. enable_if

ν…œν”Œλ¦Ώ 메타 ν•¨μˆ˜λ‘œ SFINAE κ°œλ…μ„ ν™œμš©ν•΄ 쑰건에 λ§žμ§€ μ•ŠλŠ” μ˜€λ²„λ‘œλ”© ν•¨μˆ˜λ“€μ„ ν›„λ³΄κ΅°μ—μ„œ μ œμ™Έμ‹œμΌœμ£ΌλŠ” 역할을 μˆ˜ν–‰ν•œλ‹€. μ²˜μŒμ— enable_ifκ°€ SFINAE와 κ°™λ‹€κ³  μƒκ°ν•˜μ˜€μ§€λ§Œ, enable_ifλŠ” SFINAEλ₯Ό 옳게 κ΅¬ν˜„ν•˜λŠ” 방법 쀑 ν•˜λ‚˜μ΄λ‹€. enable_ifλŠ” μ—¬λŸ¬ ν…œν”Œλ¦Ώ λ©”νƒ€ν•¨μˆ˜λ₯Ό μ‘°ν•©ν•΄ 쑰건을 λ§Œμ‘±ν•˜λŠ” νƒ€μž…μ„ 인자둜 λ°›λŠ” 것을 λͺ©μ μœΌλ‘œ ν•œλ‹€.
Prototype
template <bool, class _Tp = void> struct enable_if {}; template <class _Tp> struct enable_if<true, _Tp> {typedef _Tp type;};
C++
볡사
enable_if의 ν”„λ‘œν†  νƒ€μž…μ€ 두 가지가 μ‘΄μž¬ν•œλ‹€. 결둠만 λ¨Όμ € λ§ν•˜λ©΄ λ§Œμ•½ enable_if의 첫 번째 ν…œν”Œλ¦Ώ 인자인 bool이 true라면 enable_if::type의 νƒ€μž…μ€ _Tpκ°€ λœλ‹€. λ‹€μ‹œ λ§ν•΄μ„œ bool이 true라면 type이 μ‘΄μž¬ν•œλ‹€λŠ” 것이닀.
예제
#include <iostream> #include <type_traits> template <typename _Tp> void catch_type(typename std::enable_if<std::is_void<_Tp>::value>::type * = 0) { if (std::is_void<_Tp>::value) { std::cout << "void" << std::endl; } else { std::cout << "not void" << std::endl; } } int main(void) { catch_type<void>(); catch_type<int>(); // μ§€μš°λ©΄ 컴파일 μ •μƒμ μœΌλ‘œ 됨 return (0); }
C++
볡사
enable_ifλŠ” SFINAEλ₯Ό μ μš©ν•œ 방법 쀑 ν•˜λ‚˜λΌκ³  ν–ˆλ‹€. λ”°λΌμ„œ ν…œν”Œλ¦Ώ μΈμžμ—λ„ ν•¨μˆ˜ μΈμžμ—λ„ enable_ifλ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€. μœ„λŠ” ν•¨μˆ˜ νŒŒλΌλ―Έν„°μ— enable_ifλ₯Ό μ μš©ν•œ λͺ¨μŠ΅μ΄λ‹€.
enable_if μ•ˆμ— is_void 메타 ν•¨μˆ˜λ₯Ό μ μš©ν–ˆλŠ”λ°, λ§Œμ•½ _Tpκ°€ void라면 valueκ°€ trueκ°€ λ˜μ–΄ enable_if의 type이 μ‘΄μž¬ν•˜κ²Œ 될 것이닀. μœ„μ˜ μ½”λ“œλ₯Ό 컴파일 ν–ˆμ„ λ•Œ 였λ₯˜κ°€ λ‚˜κ²Œ λ˜λŠ”λ°, μ™œλƒν•˜λ©΄ catch_type<int>() μ‹€ν–‰ κ΅¬λ¬Έμ—μ„œ int νƒ€μž…μ€ voidκ°€ μ•„λ‹ˆκΈ° λ•Œλ¬Έμ΄λ‹€.
좔가적인 예둜 λ²‘ν„°μ˜ μƒμ„±μžλ₯Ό 듀어보겠닀.
// 1. vector (size_type n, const value_type& val); // 2. template <class InputIterator> vector (InputIterator first, InputIterator last); // μž‘μ„±ν•œ μ‹€ν–‰λ¬Έ vector<int> _v(10, 3);
C++
볡사
λ²‘ν„°μ˜ μƒμ„±μžκ°€ 1번과 2번 두 κ°œκ°€ 있고 λ§ˆμ§€λ§‰μ— 벑터 _vλ₯Ό μƒμ„±ν•˜λŠ” 싀행문을 μž‘μ„±ν•˜μ˜€λ‹€.
μœ„μ˜ 싀행문은 1번 μƒμ„±μžλ₯Ό μ‹€ν–‰ν• κΉŒ μ•„λ‹ˆλ©΄ 2번 μƒμ„±μžλ₯Ό μ‹€ν–‰ν• κΉŒ? 정닡은 2번 μƒμ„±μžμ΄λ‹€. μš°λ¦¬λŠ” λΆ„λͺ… 10개의 곡간에 3μ΄λΌλŠ” 값을 넣고싢은데 μ™œ 2번 μƒμ„±μžλ₯Ό μ‹€ν–‰ν• κΉŒ?
κ·Έ μ΄μœ λŠ” size_typeκ³Ό ν…œν”Œλ¦Ώ 인자인 InputIterator에 μžˆλ‹€. λ³Έ μ˜ˆμ œμ—λŠ” λ‚˜μ™€μžˆμ§€ μ•Šμ§€λ§Œ 벑터 μ½”λ“œλ₯Ό μ‚΄νŽ΄λ³΄λ©΄ size_type은 λΆ€ν˜Έ μ—†λŠ” μ •μˆ˜ νƒ€μž…μ΄λ‹€. λ”°λΌμ„œ 10은 λΆ€ν˜Έκ°€ μžˆλŠ” μ •μˆ˜μ΄κΈ° λ•Œλ¬Έμ— 1번 μƒμ„±μžμ—λ„ 맞긴 ν•˜μ§€λ§Œ μ™„μ „νžˆ λ“€μ–΄λ§žλŠ” μƒμ„±μžλŠ” 2번 μƒμ„±μžμ΄λ‹€. λ§Œμ•½ InputIterator에 intκ°€ λ“€μ–΄μ˜¨λ‹€λ©΄ 2번 μƒμ„±μžλŠ” λ‹€μŒκ³Ό 같이 ν‘œν˜„ν•  수 μžˆμ„ 것이닀.
vector (int first, int last);
C++
볡사
μ»΄νŒŒμΌλŸ¬λŠ” μ—¬λŸ¬ 후보ꡰ 쀑 κ°€μž₯ μ ν•©ν•œ 후보λ₯Ό μ°Ύμ•„ μ½”λ“œλ₯Ό μƒμ„±ν•˜κ³  μ»΄νŒŒμΌν•œλ‹€. λ”°λΌμ„œ μœ„μ™€ 같이 InputIterator에 iterator νƒ€μž…μ΄ λ“€μ–΄μ˜€μ§€ μ•Šμ•„μ„œ λ°œμƒν•˜λŠ” 였λ₯˜λ‚˜ λͺ¨ν˜Έν•¨μ„ λ°©μ§€ν•˜κΈ° μœ„ν•΄ enable_ifλ₯Ό μ‚¬μš©ν•œλ‹€.
μ˜ˆμ‹œ
template <typename InputIterator> vector(InputIterator first, enable_if<std::is_iterator<InputIterator>::value>::type last);
C++
볡사
μœ„μ™€ 같이 enable_ifλ₯Ό μ‚¬μš©ν•˜λ©΄ ν…œν”Œλ¦Ώ νŒŒνƒ€λ―Έν„° InputIterator에 iterator νƒ€μž…μ΄ 듀어왔을 λ•Œλ§Œ 2번 μƒμ„±μžκ°€ μ‹€ν–‰ 될 것이닀.

5. VSTD

β€’ _VSTDΒ is now an alias forΒ stdΒ instead ofΒ std::_LIBCPP_ABI_NAMESPACE. This is technically not a functional change, except for folks that might have been usingΒ _VSTDΒ in creative ways (which has never been officially supported).
_VSTDλŠ” std::_LIBCPP_ABI_NAMESPACEλ₯Ό λŒ€μ‹ ν•΄μ„œ μ‚¬μš©λ˜λŠ” λ§€ν¬λ‘œμ΄λ‹€.
λŒ€μΆ© std와 λΉ„μŠ·ν•œ μ „μ—­ namespace라고 μƒκ°ν•˜λ©΄ λ˜μ§€ μ•Šμ„κΉŒ μ‹Άλ‹€.

Β Container

Containerλž€ μ΄λ¦„μ—μ„œ μ•Œ 수 μžˆλ“―μ΄ λ³Έ κ³Όμ œλŠ” 데이터λ₯Ό μ €μž₯ν•  수 μžˆλŠ” 객체λ₯Ό κ΅¬ν˜„ν•˜λŠ” 것을 λͺ©μ μœΌλ‘œ ν•œλ‹€.
ContainerλŠ” λ°μ΄ν„°μ˜ μ—¬λŸ¬ νƒ€μž…μ„ μˆ˜μš©ν•  수 μžˆλŠ” λŠ₯λ ₯을 μ§€λ‹Œλ‹€. μ΄λŠ” ν…œν”Œλ¦Ώ(template)을 톡해 κ΅¬ν˜„λ˜κ³ 
μ •μˆ˜ν˜•, μ‹€μˆ˜ν˜•, λ¬Έμžν˜• 심지어 κ°μ²΄ν˜•νƒœ κΉŒμ§€μ˜ μ—¬λŸ¬ 데이터 νƒ€μž…μ„ ν¬μš©ν•˜λŠ” μœ μ—°μ„±μ„ κ°–κ³  μžˆλ‹€.
μ»¨ν…Œμ΄λ„ˆλŠ” 객체에 μ§μ ‘μ μœΌλ‘œ μ ‘κ·Όν•˜μ—¬ κ΄€λ¦¬ν•˜κ±°λ‚˜ μ‚¬μš©ν•  수 μžˆλŠ” κΈ°λŠ₯을 μ œκ³΅ν•˜λŠ”λ° μ΄λŠ” iteratorλΌλŠ” 반볡자λ₯Ό 톡해 μ œκ³΅λœλ‹€.
λ‹€μŒμ€ 크게 μ„Έ 개둜 κ΅¬λΆ„λ˜λŠ” μ»¨ν…Œμ΄λ„ˆμ˜ μ’…λ₯˜μ΄λ‹€.

1. Sequence containers ( μ‹œν€€μŠ€ μ»¨ν…Œμ΄λ„ˆ, 순차적 μ»¨ν…Œμ΄λ„ˆ )

Sequence containerλŠ” 데이터에 순차적으둜 μ ‘κ·Όν•  수 μžˆλŠ” ꡬ쑰(container)이닀.
Template
Container
Description
Class template
vector
동적 연속 λ°°μ—΄
Class template
deque
μ–‘λ°©ν–₯ 큐 (double-ended queue)
Class template
list
이쀑 μ—°κ²° 리슀트 (doubly-linked-list)
Class template
array (C++11)
정적 연속 λ°°μ—΄
Class template
forward_list (C++11)
단일 μ—°κ²° 리슀트 (singly-linked list)

2. Associative containers ( μ—°κ΄€ μ»¨ν…Œμ΄λ„ˆ )

Associative containerλŠ” 데이터에 λΉ λ₯΄κ²Œ μ ‘κ·Όν•˜κ³  검색할 수 μžˆλŠ” ꡬ쑰이닀.
Template
Container
Description
Class template
map
킀에 μ˜ν•΄ μ •λ ¬λœ ν‚€-κ°’ 쌍의 λͺ¨μŒ 이 λ•Œ ν‚€λŠ” 고유(Unique)함
Class template
set
ν‚€λ‘œ μ •λ ¬ 된 고유 ν‚€μ˜ λͺ¨μŒ
Class template
multimap
킀에 μ˜ν•΄ μ •λ ¬ 된 ν‚€-κ°’ 쌍의 λͺ¨μŒ
Class template
multiset
ν‚€λ‘œ μ •λ ¬ 된 ν‚€μ˜ λͺ¨μŒ

3. Unordered associative containers

데이터λ₯Ό λΉ λ₯΄κ²Œ 검색할 수 μžˆλŠ” ν•΄μ‹± 된 ꡬ쑰
Template
Container
Description
Class template
unordered_map (C++11)
킀에 μ˜ν•΄ hashed 된 ν‚€-κ°’ 쌍의 λͺ¨μŒ 이 λ•Œ ν‚€λŠ” 고유(unique)함
Class template
unordered_set (C++11)
킀에 μ˜ν•΄ hashed 된 고유 ν‚€μ˜ λͺ¨μŒ
Class template
unordered_multimap (C++11)
킀에 μ˜ν•΄ hashed 된 ν‚€-κ°’ 쌍의 λͺ¨μŒ
Class template
unordered_multiset (C++11)
킀에 μ˜ν•΄ hashed 된 ν‚€μ˜ λͺ¨μŒ

4. Container adaptors

κΈ°μ‘΄ μ»¨ν…Œμ΄λ„ˆμ˜ μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ œν•œν•˜μ—¬ κΈ°λŠ₯이 λ³€ν™˜λ˜κ±°λ‚˜ μ„±λŠ₯이 μ œν•œ 된 μ»¨ν…Œμ΄λ„ˆλ₯Ό λ§ν•œλ‹€. νŠΉμ • ν˜•νƒœμ˜ λ™μž‘λ§Œμ„ μˆ˜ν–‰ν•˜λ„λ‘ 기쑴의 μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ œν•œν•˜κ³  반볡자(iterator)λ₯Ό μ§€μ›ν•˜μ§€ μ•ŠλŠ”λ‹€. λ”°λΌμ„œ μ •λ ¬, μ›μ†Œ 탐색과 같은 STL (Standard Template Library) μ•Œκ³ λ¦¬μ¦˜μ„ μ‚¬μš©ν•  수 μ—†λ‹€.
Template
Container
Description
Class template
stack
LIFO(Last In - First Out) ꡬ쑰
Class template
queue
FIFO(First In - First Out) ꡬ쑰
Class template
priority_queue
μš°μ„  μˆœμœ„ 큐 데이터 ꡬ쑰

Β Iterator

반볡자(Iterator)
반볡자(Iterator)λŠ” λ°°μ—΄ λ˜λŠ” μ»¨ν…Œμ΄λ„ˆμ˜ μš”μ†Œλ₯Ό κ°€λ¦¬ν‚€λŠ” λͺ¨λ“  객체둜 μ—°μ‚°μž 집합을 μ‚¬μš©ν•˜μ—¬ ν•΄λ‹Ή λ²”μœ„μ˜ μš”μ†Œλ₯Ό λ°˜λ³΅ν•  수 μžˆλŠ” 객체이닀. (i.e. 증뢄 μ—°μ‚°(++) λ˜λŠ” μ—­μ°Έμ‘°(*))
λ°˜λ³΅μžμ™€ κ°€μž₯ λΉ„μŠ·ν•œ 것은 포인터라고 ν•  수 μžˆλ‹€. 포인터 λ˜ν•œ 증뢄연산을 톡해 배열을 λ°˜λ³΅ν•˜λ©° 순회λ₯Ό ν•  수 μžˆλ‹€. λ”ν•΄μ„œ vector와 같은 container에 λ”°λ‘œ 섀계 된 λ°˜λ³΅μžλ„ μ‘΄μž¬ν•˜λŠ”λ° 이도 ν¬μΈν„°λ‚˜ λ°˜λ³΅μžμ™€ 같은 역할을 μˆ˜ν–‰ν•œλ‹€.
ν•˜μ§€λ§Œ λͺ¨λ“  λ°˜λ³΅μžκ°€ ν¬μΈν„°μ˜ λͺ¨λ“  κΈ°λŠ₯을 μˆ˜ν–‰ν•  수 μžˆλŠ” 것은 μ•„λ‹ˆκ³  λ°˜λ³΅μžκ°€ μ§€μ›ν•˜λŠ” νŠΉμ„±κ³Ό 속성에 λ”°λΌμ„œ 5개의 λ‹€λ₯Έ λ²”μ£Όλ‘œ λΆ„λ₯˜λœλ‹€.
Type
Description
μž…λ ₯ 반볡자 (Input iterator)
read-only μ»¨ν…Œμ΄λ„ˆλ‘œλΆ€ν„° 값을 μ½λŠ” 데 μ“°μ΄λŠ” 반볡자둜 값을 읽을 μˆ˜λŠ” μžˆμ§€λ§Œ λ³€κ²½ν•  μˆ˜λŠ” μ—†λ‹€. 증가 μ—°μ‚°μž(++)λ₯Ό μ‚¬μš©ν•΄μ„œ 순방ν–₯으둜만 이동이 κ°€λŠ₯ν•˜λ‹€.
좜λ ₯ 반볡자 (Output iterator)
write-only 값을 λ³€κ²½ν•˜λŠ” 데 μ‚¬μš©λ˜λŠ” 반볡자둜 μž…λ ₯ λ°˜λ³΅μžμ™€ λ°˜λŒ€λ‘œ 값을 λ³€κ²½ν•  μˆ˜λŠ” μžˆμ§€λ§Œ 읽을 μˆ˜λŠ” μ—†λ‹€. 순방ν–₯(++)으둜만 이동이 κ°€λŠ₯ν•˜λ‹€.
순방ν–₯ 반볡자 (Forward iterator)
read-write 증가 μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•˜μ—¬ 순방ν–₯으둜만 이동이 κ°€λŠ₯ν•˜λ‹€.
μ–‘λ°©ν–₯ 반볡자 (Bidirectional iterator)
read-write 증가 μ—°μ‚°μž(++) λ˜λŠ” κ°μ†Œ μ—°μ‚°μž(--)λ₯Ό μ‚¬μš©ν•˜μ—¬ 순방ν–₯ λ˜λŠ” μ—­λ°©ν–₯으둜 이동이 κ°€λŠ₯ν•˜λ‹€.
μž„μ˜ μ ‘κ·Ό 반볡자 (Random access iterator)
read-write μ΅œμƒμœ„ 레벨의 반볡자둜 κ°€μž₯ λ§Žμ€ κΈ°λŠ₯을 μ œκ³΅ν•œλ‹€. μ–‘λ°©ν–₯ 반볡자의 κΈ°λŠ₯ 및 μ²¨μžμ—°μ‚°μž([])λ₯Ό 톡해 μž„μ˜μ˜ μš”μ†Œμ— μ ‘κ·Όν•  수 μžˆλ‹€. 일반 ν¬μΈν„°μ˜ κΈ°λŠ₯을 거의 λ‹€ μ‚¬μš©ν•  수 μžˆλ‹€.
νŠΉμ„±κ³Ό 속성에 따라 λ²”μ£Όκ°€ λΆ„λ₯˜ λ˜μ–΄ μžˆλ“―μ΄ μ»¨ν…Œμ΄λ„ˆμ— 따라 μ‚¬μš©ν•˜λŠ” iterator의 μ’…λ₯˜ λ˜ν•œ λ‹€λ₯΄λ‹€.
Container type
Iterator type
vector
μž„μ˜ μ ‘κ·Ό 반볡자 (Random access iterator)
map
μ–‘λ°©ν–₯ 반볡자 (Bidirectional iterator)
set
μ–‘λ°©ν–₯ 반볡자 (Bidirectional iterator)
stack
none
포인터(pointer)와 반볡자(Iterator)의 비ꡐ Pointer와 IteratorλŠ” 곡톡점이 μžˆμœΌλ©΄μ„œ λΆ„λͺ…ν•œ 차이점이 μ‘΄μž¬ν•œλ‹€. ν¬μΈν„°λŠ” λ³€μˆ˜κ°€ λ©”λͺ¨λ¦¬μ— μœ„μΉ˜ν•œ μ£Όμ†Œ 즉 λ³€μˆ˜μ˜ λ©”λͺ¨λ¦¬ μ£Όμ†Œλ₯Ό μ €μž₯ν•˜λŠ” λ³€μˆ˜μ΄κ³  iteratorλŠ” λ°°μ—΄μ΄λ‚˜ μ»¨ν…Œμ΄λ„ˆ λ‚΄μ˜ λ²”μ£Όμ˜ μš”μ†Œλ₯Ό 가리킀고 반볡적으둜 μˆœνšŒν•  수 μžˆλŠ” 객체이닀. λ”ν•΄μ„œ ν¬μΈν„°λŠ” 증가와 κ°μ†Œ 그리고 λ§μ…ˆμ˜ κ°„λ‹¨ν•œ μˆ˜ν•™μ μΈ 연산을 μˆ˜ν–‰ν•  수 μžˆμ§€λ§Œ iteratorλŠ” μœ„μ—μ„œ μ–ΈκΈ‰ν•œ 바와 같이 범주에 따라 μ—°μ‚° μˆ˜ν–‰ μ—¬λΆ€κ°€ λ‹€λ₯΄λ‹€. 그리고 T* νƒ€μž…μ˜ ν¬μΈν„°λŠ” μ–΄λ–€ 객체든 T νƒ€μž…μ˜ 객체λ₯Ό λ‹€ 가리킬 수 μžˆμ§€λ§Œ iteratorλŠ” μ œν•œμ μœΌλ‘œ ν•΄λ‹Ή μ»¨ν…Œμ΄λ„ˆ λ‚΄μ˜ μš”μ†Œμ—λ§Œ μ ‘κ·Όν•  수 μžˆλ‹€. λ§ˆμ§€λ§‰μœΌλ‘œ ν¬μΈν„°λŠ” 증감 μ—°μ‚° μ‹œ μ—°μ†λœ λ©”λͺ¨λ¦¬ μ£Όμ†Œμ—λ§Œ μ ‘κ·Όν•  수 μžˆλ‹€. ν•˜μ§€λ§Œ μ—°μ†μ μœΌλ‘œ λ©”λͺ¨λ¦¬κ°€ μ—°κ²°λ˜μ–΄ μžˆμ§€ μ•Šκ±°λ‚˜ μƒˆλ‘œμš΄ 객체둜 볡사 μ‹œ μ •μƒμ μœΌλ‘œ 객체에 μ ‘κ·Όν•  수 μ—†λ‹€.

Β Vector

Sequence container둜 동적 연속 λ°°μ—΄ ν˜•νƒœμ΄λ‹€.
Class Prototype
// class prototype template <class T, class Allocator = std::allocator<T> > class vector;
C++
볡사
Member
public: typedef T value_type; typedef Allocator allocator_type; typedef typename allocator_type::reference reference; typedef typename allocator_type::const_reference const_reference; typedef implementation-defined iterator; typedef implementation-defined const_iterator; typedef typename allocator_type::size_type size_type; typedef typename allocator_type::difference_type difference_type; typedef typename allocator_type::pointer pointer; typedef typename allocator_type::const_pointer const_pointer; typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
C++
볡사
member type
definition
notes
value_type
첫 번째 ν…œν”Œλ¦Ώ νŒŒλΌλ―Έν„° (T)
allocator_type
두 번째 ν…œν”Œλ¦Ώ νŒŒλΌλ―Έν„° (Alloc)
κΈ°λ³Έ κ°’ : allocator<value_type>
reference
allocator_type::reference
κΈ°λ³Έ allocator일 λ•Œ : value_type &
const_reference
allocator_type::const_reference
κΈ°λ³Έ allocator일 λ•Œ : const value_type&
pointer
allocator_type::pointer
κΈ°λ³Έ allocator일 λ•Œ : value_type *
const_pointer
allocator_type::const_pointer
κΈ°λ³Έ allocator일 λ•Œ : const value_type *
iterator
value_type에 μž„μ˜(random)둜 μ ‘κ·Όν•˜λŠ” 반볡자
const_iterator둜 λ³€ν™˜ν•  수 있음
const_iterator
const value_type에 μž„μ˜(random)둜 μ ‘κ·Όν•˜λŠ” 반볡자
reverse_iterator
const_reverse_iterator
difference_type
λΆ€ν˜Έκ°€ μžˆλŠ” integral type iterator_traits<iterator>::difference_typeκ³Ό κ°™λ‹€.
보톡 ptrdiff_t와 κ°™λ‹€
size_type
difference_type의 음이 μ•„λ‹Œ 값을 λ‚˜νƒ€λ‚Ό 수 μžˆλŠ” λΆ€ν˜Έ μ—†λŠ” μ •μˆ˜ ν˜•μ‹
보톡 size_t와 κ°™λ‹€
Constructor
β†’ Default constructor ( empty container constructor)
// Description // Elementκ°€ μ—†λŠ” 빈 벑터λ₯Ό κ΅¬μ„±ν•œλ‹€. explicit vector (const allocator_type& alloc = allocator_type());
C++
볡사
β†’ Fill constructor
// Description // val의 볡사본인 n개의 elements둜 μ»¨ν…Œμ΄λ„ˆλ₯Ό κ΅¬μ„±ν•˜λŠ” constructor explicit vector (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type());
C++
볡사
β†’ Range constructor
// Description // Parameter둜 λ“€μ–΄μ˜¨ firstλΆ€ν„° lastκΉŒμ§€μ˜ λ²”μœ„λ§ŒνΌμ˜ μš”μ†Œλ₯Ό 가진 벑터 μ»¨ν…Œμ΄λ„ˆλ₯Ό ꡬ성 // κΈ°μ‘΄ λ²”μœ„μ˜ μ •λ ¬κ³Ό λ™μΌν•˜κ²Œ element의 λ°°μΉ˜κ°€ 이루어진닀. template <class InputIterator> vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
C++
볡사
β†’ Copy constructor
// Description // Parameter x 내에 μžˆλŠ” 각각의 elementλ₯Ό 같은 μ •λ ¬ 순으둜 λ³΅μ‚¬ν•˜μ—¬ containerλ₯Ό κ΅¬μ„±ν•œλ‹€. vector (const vector& x);
C++
볡사
β†’ Parameter
Parameter
Description
alloc
Allocator object둜 μ»¨ν…Œμ΄λ„ˆλŠ” 이 allocatorλ₯Ό μœ μ§€ν•˜κ³  μ‚¬μš©ν•œλ‹€.
n
초기 μ»¨ν…Œμ΄λ„ˆ μ‚¬μ΄μ¦ˆ
val
μ»¨ν…Œμ΄λ„ˆμ˜ μ±„μšΈ κ°’μœΌλ‘œ μ»¨ν…Œμ΄λ„ˆμ— μžˆλŠ” n개의 elementλŠ” 이 κ°’μ˜ λ³΅μ‚¬λ³ΈμœΌλ‘œ μ΄ˆκΈ°ν™” λœλ‹€.
first, last
input iterator둜 λ²”μœ„μ˜ 처음과 끝을 가리킨닀. 주의 ν•΄μ•Όν•  점은 λ²”μœ„λŠ” μ²˜μŒλΆ€ν„° λ§ˆμ§€λ§‰ μ‚¬μ΄μ˜ λͺ¨λ“  μš”μ†Œλ₯Ό ν¬ν•¨ν•˜μ§€λ§Œ λ§ˆμ§€λ§‰ μš”μ†ŒλŠ” ν¬ν•¨ν•˜μ§€ μ•ŠλŠ”λ‹€.
x
vector object
il
초기 리슀트 였브젝트(Initializer_list object) Initializer list μ„ μ–Έμžμ—μ„œ μžλ™μœΌλ‘œ μƒμ„±λœλ‹€.
Iterator
β†’ begin
iterator begin(); const_iterator begin() const;
C++
볡사
Description 벑터 λ‚΄μ˜ 첫 번째 elementλ₯Ό κ°€λ¦¬ν‚€λŠ” 반볡자λ₯Ό λ°˜ν™˜ν•œλ‹€. μš”μ†Œ ν•˜λ‚˜λ§Œμ„ λ°˜ν™˜ν•˜λŠ” vector::frontμ™€λŠ” λ‹€λ₯΄κ²Œ random access iteratorλ₯Ό λ°˜ν™˜ν•œλ‹€. λ§Œμ•½ containerκ°€ λΉ„μ–΄μžˆλ‹€λ©΄, λ°˜ν™˜ 된 iterator 값은 μ—­μ°Έμ‘° λ˜μ§€ μ•ŠλŠ”λ‹€. 그리고 λ§Œμ•½ vector 객체가 constν™” λ˜μ–΄μžˆλ‹€λ©΄, ν•¨μˆ˜λŠ” const_iteratorλ₯Ό λ°˜ν™˜ν•œλ‹€.
β†’ end
iterator end(); const_iterator end() const;
C++
볡사
Description past-the-elementλ₯Ό μ°Έμ‘°ν•˜λŠ” 반볡자λ₯Ό λ°˜ν™˜ν•˜λŠ” iterator past-the-elementλŠ” 이둠적으둜 vector의 λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό 보톡 λ§ν•œλ‹€. 이 λ°˜λ³΅μžλŠ” 아무 것도 가리킀지 μ•ŠκΈ° λ•Œλ¬Έμ— μ—­μ°Έμ‘° λ˜μ–΄μ„œλŠ” μ•ˆλœλ‹€. 보톡 beginκ³Ό 같이 μ‚¬μš©λ˜μ–΄ νŠΉμ • λ²”μœ„λ₯Ό λ‚˜νƒ€λ‚Έλ‹€. λ§Œμ•½ containerκ°€ λΉ„μ–΄μžˆλ‹€λ©΄ beginκ³Ό 같은 값을 λ°˜ν™˜ν•œλ‹€.
Β rbegin, rend은 μœ„μ™€ λ°˜λŒ€μ˜ κ°œλ…μ„ 가진 iterator이닀.
Capacity
β†’ size
// size_type : unsigned integral type size_type size() const;
C++
볡사
vector μ•ˆμ— μžˆλŠ” μš”μ†Œλ“€μ˜ 개수λ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜μ΄λ‹€.
capacity ν•¨μˆ˜μ™€λŠ” λ‹€λ₯΄λ©° μ‹€μ œλ‘œ vectorμ•ˆμ— μ‘΄μž¬ν•˜λŠ” element의 개수λ₯Ό λ°˜ν™˜ν•œλ‹€.
β†’ max_size
size_type max_size() const;
C++
볡사
vectorκ°€ κ°€μ§ˆ 수 μžˆλŠ” μ΅œλŒ€μ˜ element 개수λ₯Ό λ°˜ν™˜ν•œλ‹€.
β†’ resize
// size_type n : μƒˆλ‘œμš΄ μ»¨ν…Œμ΄λ„ˆ μ‚¬μ΄μ¦ˆ // value_type val : n이 ν˜„μž¬ μ»¨ν…Œμ΄λ„ˆμ˜ μ‚¬μ΄μ¦ˆλ³΄λ‹€ 큰 경우 λ‚΄μš©μ΄ μΆ”κ°€λœ vector에 볡사 λ˜λŠ” 객체 //μ§€μ •ν•˜μ§€ μ•Šμ€ 경우 κΈ°λ³Έ μƒμ„±μžκ°€ λŒ€μ‹  μ‚¬μš©λœλ‹€. void resize (size_type n, value_type val = value_type());
C++
볡사
containerκ°€ n개의 elementsλ₯Ό κ°€μ§ˆ 수 μžˆλ„λ‘ container의 μ‚¬μ΄μ¦ˆλ₯Ό μ‘°μ •ν•œλ‹€. λ§Œμ•½, νŒŒλΌλ―Έν„°λ‘œ 주어진 n이 ν˜„μž¬ container의 μ‚¬μ΄μ¦ˆλ³΄λ‹€ μž‘μ„ 경우, ν˜„μž¬ μ»¨ν…Œμ΄λ„ˆμ˜ 첫 λ²ˆμ§ΈλΆ€ν„° n개 만큼의 element둜 μ‘°μ •λœλ‹€(쀄어든닀). κ·Έ λ’€μ˜ element듀은 μ‚­μ œλ˜κ³  νŒŒκ΄΄λœλ‹€(removing and destroying them).
β†’ capacity
size_type capacity() const;
C++
볡사
벑터에 ν˜„μž¬ ν• λ‹Ή 된 μ €μž₯ κ³΅κ°„μ˜ 크기λ₯Ό μš”μ†Œ λ‹¨μœ„λ‘œ λ°˜ν™˜ν•œλ‹€.
β†’ empty
bool empty() const;
C++
볡사
벑터가 ν˜„μž¬ λΉ„μ–΄μžˆλŠ” μƒνƒœμΈμ§€(sizeκ°€ 0인지)의 μ—¬λΆ€λ₯Ό λ°˜ν™˜ν•œλ‹€.
β†’ reserve
void reserve(size_type n);
C++
볡사
vector capacityκ°€ 적어도 n개의 elementλ₯Ό μˆ˜μš©ν•  수 μžˆλ„λ‘ μš”μ²­ν•œλ‹€. λ§Œμ•½, n이 ν˜„μž¬ λ²‘ν„°μ˜ 크기보닀 클 경우 이 ν•¨μˆ˜λŠ” container의 storageλ₯Ό n λ˜λŠ” κ·Έ μ΄μƒμœΌλ‘œ reallocate μ‹œν‚¨λ‹€.
Element access
β†’ operator[]
reference operator[] (size_type n); const_reference operator[] (size_type n) const;
C++
볡사
벑터 λ‚΄μ˜ n번째 μœ„μΉ˜μ— μžˆλŠ” element의 referenceλ₯Ό λ°˜ν™˜ vector::at ν•¨μˆ˜μ™€ μœ μ‚¬ν•˜λ©° vector::at ν•¨μˆ˜λŠ” bound-checked ν•˜κ³  λ§Œμ•½ λ²”μœ„λ₯Ό λ²—μ–΄λ‚œλ‹€λ©΄ out_of_range μ˜ˆμ™Έλ₯Ό λ˜μ§€λŠ” 점만 μ œμ™Έν•˜λ©΄ λ™μΌν•œ κΈ°λŠ₯을 μ§€λ‹Œλ‹€.
β†’ at
reference at (size_type n); const_reference at (size_type n) const;
C++
볡사
벑터 λ‚΄μ˜ n번째 μœ„μΉ˜μ— μžˆλŠ” element의 referenceλ₯Ό λ°˜ν™˜ 이 ν•¨μˆ˜λŠ” operator[]κ³Ό λ‹€λ₯΄κ²Œ μžλ™μ μœΌλ‘œ n이 vector의 μ ν•©ν•œ λ²”μœ„ 내에 μžˆλŠ”μ§€ ν™•μΈν•œλ‹€. λ§Œμ•½ n이 size와 κ°™κ±°λ‚˜ 더 큰 것과 같이 μ ν•©ν•œ 값이 μ•„λ‹ˆλΌλ©΄ out_of_range exception을 λ˜μ§„λ‹€.
β†’ front
reference front(); // normal one const_reference front() const; // if vector qualified as const
C++
볡사
vector 내에 μžˆλŠ” 첫 번째 μš”μ†Œμ˜ referenceλ₯Ό λ°˜ν™˜ν•œλ‹€. vector::begin λ©€λ²„μ™€λŠ” λ‹€λ₯Έ 점이 μžˆλŠ”λ°, vector::begin ν•¨μˆ˜λŠ” iteratorλ₯Ό λ°˜ν™˜ν•˜κ³  이 ν•¨μˆ˜λŠ” 직접적인 referenceλ₯Ό λ°˜ν™˜ν•œλ‹€. λΉ„μ–΄μžˆλŠ” vectorμ—μ„œμ˜ 이 ν•¨μˆ˜ ν˜ΈμΆœμ€ undefined behavior을 μœ λ°œν•œλ‹€.
β†’ back
reference back(); const_reference back() const;
C++
볡사
vector 내에 μžˆλŠ” λ§ˆμ§€λ§‰ μš”μ†Œμ˜ referenceλ₯Ό λ°˜ν™˜
Modifiers
β†’ assign
// (1) range function template <class InputIterator> void assign (InputIterator first, InputIterator last); // (2) fill function void assign (size_type n, const value_type& val);
C++
볡사
vector에 μžˆλŠ” 기쑴의 contentsλ₯Ό μƒˆλ‘œμš΄ contents둜 ν• λ‹Ήν•˜κ³  이에 따라 sizeλ₯Ό μˆ˜μ •ν•˜λŠ” ν•¨μˆ˜ (1) range function의 경우 μƒˆλ‘œμš΄ λ‚΄μš©μ€ firstλΆ€ν„° lastκΉŒμ§€ λ²”μœ„μ˜ 각 μš”μ†Œλ‘œ, λ™μΌν•œ μˆœμ„œλ‘œ ꡬ성 λ˜μ–΄ μžˆλ‹€. (2) fill function의 경우 μƒˆλ‘œμš΄ λ‚΄μš©μ€ n개둜, valμ—μ„œ λ³΅μ‚¬λœ μš”μ†Œλ‘œ μ΄ˆκΈ°ν™” λœλ‹€. 기쑴의 μš”μ†ŒλŠ” 이 assign ν•¨μˆ˜μ˜ 호좜 전에 λͺ¨λ‘ 파괴되고, μƒˆλ‘œμš΄ μš”μ†Œλ‘œ λŒ€μ²΄λœλ‹€. λ‹Ήμ—°ν•˜μ§€λ§Œ μƒˆλ‘œμš΄ μš”μ†Œλ“€μ˜ λ²”μœ„ λ˜λŠ” λ²‘ν„°μ˜ 크기가 ν˜„μž¬ 벑터 μ»¨ν…Œμ΄λ„ˆμ˜ μš©λŸ‰μ„ μ΄ˆκ³Όν•˜λŠ” 경우 μ €μž₯ 곡간이 재 ν• λ‹Ήλœλ‹€.
β†’ push_back
void push_back (const value_type &val);
C++
볡사
μƒˆλ‘œμš΄ elementλ₯Ό vector의 λ§ˆμ§€λ§‰μ— μΆ”κ°€ν•œλ‹€. 이 ν•¨μˆ˜λŠ” 효과적으둜 container의 sizeλ₯Ό ν•˜λ‚˜ μ¦κ°€μ‹œν‚€κ³  κΈ°μ‘΄ vector의 capacity의 크기λ₯Ό λ„˜μ–΄μ„€ 경우 μžλ™μœΌλ‘œ reallocation을 μΌμœΌν‚¨λ‹€.
β†’ pop_back
void pop_back();
C++
볡사
벑터 μ•ˆμ— μžˆλŠ” λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό μ œκ±°ν•˜κ³  νŒŒκ΄΄ν•œλ‹€. 이 경우 container의 sizeκ°€ ν•˜λ‚˜ κ°μ†Œν•œλ‹€. containerκ°€ λΉ„μ–΄μžˆλŠ” 경우 undefined behavior이닀.
β†’ insert
// (1) single element iterator insert (iterator position, const value_type& val); // (2) fill void insert (iterator position, size_type n, const value_type& val); // (3) range template <class InputIterator> void insert (iterator position, InputIterator first, InputIterator last);
C++
볡사
λ²‘ν„°λŠ” 이 insert ν•¨μˆ˜μ— μ˜ν•΄ μ§€μ •λœ μœ„μΉ˜μ— μš”μ†Œκ°€ μΆ”κ°€λ˜κ³ , μΆ”κ°€λœ μš”μ†Œμ˜ 개수 만큼 sizeκ°€ μ¦κ°€ν•œλ‹€. λ§ˆμ°¬κ°€μ§€λ‘œ capacity에 따라 reallocation이 μžλ™μœΌλ‘œ μ§„ν–‰λœλ‹€. λ²‘ν„°λŠ” λ°°μ—΄(array)λ₯Ό κΈ°λ³Έ μ €μž₯μ†Œλ‘œ μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ— μ‚½μž…μ΄ μ΄λ£¨μ–΄μ§€λŠ” 경우 기쑴의 element의 μž¬λ°°μΉ˜κ°€ 이루어진닀.
β†’ erase
iterator erase (iterator position); iterator erase (iterator first, iterator last);
C++
볡사
vectorμ—μ„œ 단일 elementλ₯Ό μ œκ±°ν•˜κ±°λ‚˜ first와 last둜 이루어진 λ²”μœ„λ§ŒνΌμ˜ μš”μ†Œλ₯Ό μ œκ±°ν•œλ‹€.
β†’ swap
void swap (vector& x);
C++
볡사
λ°›μ•„μ˜¨ νŒŒλΌλ―Έν„°μ˜ xx의 λ‚΄μš©μœΌλ‘œ container의 λ‚΄μš©μ„ κ΅ν™˜ν•œλ‹€. ν¬κΈ°λŠ” λ‹€λ₯Ό 수 μžˆμ§€λ§Œ μœ ν˜•μ€ λ™μΌν•˜λ‹€. ν•¨μˆ˜ 호좜 ν›„μ˜ 기쑴의 vector λ‚΄μš©μ€ xx에 있던 λ‚΄μš©μ΄κ³  xx의 λ‚΄μš©μ€ 기쑴의 vector의 λ‚΄μš©κ³Ό κ°™λ‹€.
β†’ clear
void clear();
C++
볡사
vector의 λͺ¨λ“  element듀을 μ œκ±°ν•œλ‹€. κ·Έ λ’€ container의 sizeλŠ” 0이닀. 이 ν•¨μˆ˜λŠ” μž¬ν• λ‹Ήμ„ 보μž₯ν•˜μ§€ μ•ŠμœΌλ©° ν•¨μˆ˜μ˜ 호좜둜 벑터 capacity의 λ³€κ²½ λ˜ν•œ 보μž₯ν•˜μ§€ μ•ŠλŠ”λ‹€. reallocation을 μ‚¬μš©ν•œ λŒ€μ²΄μž¬λŠ” swap ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λŠ” 것이닀.
Allocator
β†’ get_allocator
allocator_type get_allocator() const;
C++
볡사
vector와 μ—°κ΄€λœ allocator 객체의 볡사본을 λ°˜ν™˜ν•œλ‹€.

Β Stack

Container adaptor둜 LIFO 데이터 ꡬ쑰이닀. Stack은 κΈ°μ‘΄ μ»¨ν…Œμ΄λ„ˆμ˜ μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ œν•œν•˜μ—¬ μ„±λŠ₯이 μ œν•œ 된 μ»¨ν…Œμ΄λ„ˆμ΄λ‹€. λ‚΄μž₯ 된 LLVM의 CPP STL은 Stack을 deque둜 κ΅¬ν˜„ν•˜μ˜€λ‹€. ν•˜μ§€λ§Œ μœ„μ—μ„œ vectorλ₯Ό 이미 κ΅¬ν˜„ ν–ˆκΈ° λ•Œλ¬Έμ— vector둜 stack을 κ΅¬ν˜„ν•˜μ˜€λ‹€.
Class prototype
template <class _Tp, class _Container /*= deque<_Tp>*/> class stack;
C++
볡사
Member types
public: typedef Container container_type; typedef typename container_type::value_type value_type; typedef typename container_type::reference reference; typedef typename container_type::const_reference const_reference; typedef typename container_type::size_type size_type; protected: container_type c;
C++
볡사
Member
Description
Container container_type
The second template parameter (Container) 이 μŠ€νƒμ˜ 기반이 λ˜λŠ” underlying container
container_type::value_type value_type
The first template parameter (T) Stack의 elements type
container_type::reference reference
Underlying container의 reference
container_type::const_reference const_reference
Underlying container의 const_reference
container_type::size_type size_type
An unsigned integral type 보톡 size_t와 κ°™μŒ
Member functions
β†’ empty
bool empty() const;
C++
볡사
Stack이 λΉ„μ–΄μžˆλŠ”μ§€μ˜ μ—¬λΆ€λ₯Ό λ°˜ν™˜ν•œλ‹€. λ§Œμ•½ 기반 μ»¨ν…Œμ΄λ„ˆμ˜ μ‚¬μ΄μ¦ˆκ°€ 0이면 true, μ•„λ‹ˆλ©΄ falseλ₯Ό λ°˜ν™˜ν•œλ‹€.
β†’ size
size_type size() const;
C++
볡사
기반 μ»¨ν…Œμ΄λ„ˆμ˜ element 개수λ₯Ό λ°˜ν™˜ν•œλ‹€.
β†’ top
value_type& top(); const value_type& top() const;
C++
볡사
Stack의 제일 μœ— μš”μ†Œμ˜ 레퍼런슀λ₯Ό λ°˜ν™˜ν•œλ‹€.
β†’ push
void push (const value_type& val);
C++
볡사
Stack의 제일 μœ„μ— μƒˆλ‘œμš΄ elementλ₯Ό μΆ”κ°€ν•œλ‹€.
β†’ pop
void pop();
C++
볡사
Stack의 제일 μœ— μš”μ†Œλ₯Ό μ œκ±°ν•œλ‹€.

Β map

STL의 map은 red-black tree둜 κ΅¬ν˜„λ˜μ–΄ μžˆλ‹€.

Β Red-black tree

red-black treeλŠ” 이름 κ·ΈλŒ€λ‘œ λ…ΈνŠΈμ˜ 색이 Redκ±°λ‚˜ Black인 트리 ꡬ쑰λ₯Ό λ§ν•œλ‹€. 일반 μ΄μ§„νŠΈλ¦¬μ™€ λΉ„μŠ·ν•œ 성격을 κ°–μ§€λ§Œ ν•œ μͺ½μœΌλ‘œ μΉ˜μš°μ³μ„œ μƒκΈ°λŠ” μ΄μ§„νŠΈλ¦¬μ˜ 단점을 λ³΄μ™„ν•˜κΈ° μœ„ν•΄ κ· ν˜•(Balanced) 이진 탐색 트리 ν˜•νƒœλ‘œ μ„€κ³„λœ ꡬ쑰이닀.
Red-black tree의 μ„±μ§ˆμ€ μ•„λž˜μ™€ κ°™λ‹€.
1. Root λ…Έλ“œμ˜ 색은 Black이닀. 2. λͺ¨λ“  리프 λ…Έλ“œλŠ” Black이닀. 3. 각 λ…Έλ“œμ˜ 색은 Red λ˜λŠ” Black이닀. 4. Red λ…Έλ“œμ˜ μžμ‹ 색은 λ°˜λ“œμ‹œ Black이닀. ( Red - Red둜 μ΄μ–΄μ§€λŠ” 연결은 μ•ˆ 됨. B - BλŠ” κ°€λŠ₯) 5. 각 λ…Έλ“œμ—μ„œ 리프 λ…Έλ“œμ— λ‹€λ‹€λ₯Ό λ•ŒκΉŒμ§€ Black λ…Έλ“œ κ°œμˆ˜λŠ” λͺ¨λ‘ κ°™λ‹€.
[κ·Έλ¦Ό] Red-black 트리의 μ˜ˆμ‹œ
μœ„ κ·Έλ¦Όμ—μ„œ μ•Œ 수 μžˆλ“―μ΄ Red-black treeλŠ” κ· ν˜• 이진 탐색 트리의 ν˜•νƒœμ΄λ‹€. μ–΄λŠ λ…Έλ“œλ₯Ό κΈ°μ€€μœΌλ‘œ μ™Όμͺ½μ—λŠ” μžμ‹ μ˜ 값보닀 μž‘μ€ 값을 가진 λ…Έλ“œκ°€ 였λ₯Έμͺ½μ—λŠ” μžμ‹ μ˜ 값보닀 큰 값을 가진 λ…Έλ“œκ°€ 이어져 μžˆλ‹€.
μΆ”κ°€μ μœΌλ‘œ 리프 λ…Έλ“œμ˜ 색이 Black이라고 μœ„μ— μ–ΈκΈ‰ν•˜μ˜€λŠ”λ° 그림을 보면 리프 λ…Έλ“œκ°€ 125, 175, 225, 275 값을 μ§€λ‹Œ λ…Έλ“œλΌκ³  생각할 수 μžˆλ‹€. 이 λ…Έλ“œμ˜ 색은 Red라 μ˜³μ§€ μ•Šμ€ RB Tree라고 생각할 수 μžˆμ§€λ§Œ μ•„λ¬΄λŸ° 값을 갖지 μ•Šμ€ NULL(NIL) LEAF도 리프 λ…Έλ“œλ‘œ κ°„μ£Όν•œλ‹€. 그리고 이 NULL LEAF NODE의 색은 Black이닀.
Red-black tree의 νŠΉμ§•μ€ 약속과 κ°™λ‹€. λ…Έλ“œμ˜ μ‚½μž…κ³Ό μ‚­μ œ 연산을 μˆ˜ν–‰ ν•œ 뒀에도 μœ„μ˜ 닀섯가지 νŠΉμ§•μ΄ μœ μ§€λ˜μ–΄μ•Ό ν•œλ‹€λŠ” λœ»μ΄λ‹€. μ•„λž˜λŠ” μ‚½μž…κ³Ό μ‚­μ œ 연산을 μˆ˜ν–‰ ν–ˆμ„ λ•Œ μ–΄λ–»κ²Œ λ…Έλ“œλ“€μ˜ μœ„μΉ˜λ₯Ό μ‘°μ •ν•˜κ³  μœ νš¨μ„±μ„ λ§Œμ‘±μ‹œν‚€λŠ”μ§€μ— λŒ€ν•΄μ„œ μ†Œκ°œν•œλ‹€.

Β Red-black tree insertion - μ‚½μž… μ—°μ‚°

기본적으둜 루트λ₯Ό μ œμ™Έν•œ μ‚½μž…λ˜λŠ” λ…Έλ“œμ˜ 색은 Red이닀. νŠΈλ¦¬κ°€ λΉ„μ–΄μžˆμ„ λ•Œ 처음으둜 μ‚½μž… λ˜λŠ” λ…Έλ“œλŠ” 루트 λ…Έλ“œμ΄λ―€λ‘œ Black이닀. μ‚½μž… μ—°μ‚° μ‹œ 트리의 μœ νš¨μ„±μ„ νŒŒκ΄΄ν•˜λŠ” 쑰건은 맀우 κ°„λ‹¨ν•˜λ‹€. λ°”λ‘œ μ‚½μž…λ˜λŠ” λ…Έλ“œμ™€ λΆ€λͺ¨ λ…Έλ“œμ˜ 색이 λͺ¨λ‘ Red 일 λ•Œμ΄λ‹€. λΆ€λͺ¨ λ…Έλ“œμ™€ μ‚½μž… 된 μžμ‹ λ…Έλ“œμ˜ 색이 λͺ¨λ‘ Red이면 λŒ€ν‘œμ μœΌλ‘œ 4번 νŠΉμ§•(μ„±μ§ˆ)을 μœ„λ°°ν•œλ‹€.
4. Red λ…Έλ“œμ˜ μžμ‹ 색은 λ°˜λ“œμ‹œ Black이닀. ( Red - Red둜 μ΄μ–΄μ§€λŠ” 연결은 μ•ˆ 됨. B - BλŠ” κ°€λŠ₯)
μ‚½μž… 이후에 트리의 μœ νš¨μ„±μ΄ 파괴 λ˜μ—ˆλ‹€λ©΄ μ μ ˆν•˜κ²Œ 트리의 ꡬ쑰λ₯Ό μ‘°μ •ν•΄μ•Όν•œλ‹€. 이 μ‘°μ • 과정은 크게 두 κ°€μ§€λ‘œ λ‚˜λ‰œλ‹€.
1. Recoloring ( 색상 λ³€κ²½ ) 2. Rotation ( νšŒμ „ )

1. Recoloring

λ…Έλ“œμ˜ 색을 λ³€κ²½ν•˜λŠ” 과정을 λ§ν•œλ‹€. 이 색상을 λ³€κ²½ν•˜λŠ” 과정은 λΆ€λͺ¨μ™€ μƒˆλ‘œ λ“€μ–΄μ˜¨ λ…Έλ“œμ˜ 색이 λͺ¨λ‘ Red이고 λΆ€λͺ¨μ˜ ν˜•μ œ 즉 Sibling node의 색이 Red일 λ•Œ 이루어진닀.
νŠΈλ¦¬κ°€ μœ„μ™€ 같이 ꡬ성 λ˜μ–΄μžˆλ‹€κ³  ν•  λ•Œ 150을 μ‚½μž…ν•˜κ³ μž ν•œλ‹€.
μ΄λ ‡κ²Œ 되면 150κ³Ό 175 λ…Έλ“œ λͺ¨λ‘ Redμ΄λ―€λ‘œ λ…Έλ“œλ₯Ό νšŒμ „ν•˜κ±°λ‚˜ 색을 λ‹€μ‹œ μΉ ν•΄ 쀄 ν•„μš”κ°€ μžˆλ‹€. Recoloring을 ν•˜λŠ” κ²½μš°λŠ” Sibling의 색이 Red일 κ²½μš°μ— μˆ˜ν–‰ν•œλ‹€.
이해λ₯Ό 돕기 μœ„ν•΄ Sibling λ…Έλ“œμΈ 225λ₯Ό μΆ”κ°€ν–ˆλ‹€. κ³„μ†ν•΄μ„œ Sibling (μ΄ν•˜ s) λ…Έλ“œκ°€ Red이고 Parent(μ΄ν•˜ p) λ…Έλ“œκ°€ Red라면 p와 s λ…Έλ“œμ˜ 색을 Black으둜, Parent의 Parent λ…Έλ“œμ˜ 색을 Red둜 λ³€κ²½ν•˜λ©΄ λœλ‹€.
μ™„λ²½ν•΄ λ³΄μ΄μ§€λ§Œ μ—¬κΈ°μ„œ 생길 수 μžˆλŠ” 문제점이 ν•œ 가지 더 μžˆλ‹€.
ppκ°€ 루트 λ…Έλ“œλΌλ©΄ pp의 색을 κ°•μ œλ‘œ Black으둜 λ³€κ²½μ‹œμΌœ 트리의 μœ νš¨μ„±μ„ μ™„μ „ν•˜κ²Œ ν•  수 μžˆλ‹€. ν•˜μ§€λ§Œ ppκ°€ 루트 λ…Έλ“œκ°€ μ•„λ‹ˆλΌ μƒμœ„ 트리의 λ…Έλ“œ 쀑 ν•˜λ‚˜μΌ 경우라면 λ¬Έμ œκ°€ λ°œμƒν•  수 μžˆλ‹€.
300의 우츑 λ…Έλ“œ νŠΈλ¦¬λŠ” μƒλž΅ν–ˆλ‹€. 이럴 경우 pp의 λ…Έλ“œλ„ Red, 300의 λ…Έλ“œλ„ Red이기 λ•Œλ¬Έμ— λ‹€μ‹œ 쑰정이 ν•„μš”ν•˜λ‹€. λ§ˆμ°¬κ°€μ§€λ‘œ 200 값이 λ‹΄κΈ΄ pp λ…Έλ“œλ₯Ό κΈ°μ€€μœΌλ‘œ λΆ€λͺ¨ 그리고 sibling을 ν™•μΈν•˜λ©° μž¬κ·€μ μœΌλ‘œ 쑰정을 μˆ˜ν–‰ν•˜λ©΄ ν•΄κ²°λœλ‹€.

2. Rotation

λ…Έλ“œλ“€μ„ νšŒμ „ν•˜λŠ” 과정을 λ§ν•œλ‹€. λ…Έλ“œλ“€μ„ νšŒμ „ν•˜λŠ” κ²½μš°λŠ” Sibling λ…Έλ“œμ˜ 색이 Blackμ΄κ±°λ‚˜ μ—†λŠ” 경우(NIL)에 μˆ˜ν–‰ν•œλ‹€.
Rotation의 첫 번째 κ²½μš°λŠ” μƒˆλ‘œμš΄ λ…Έλ“œ xκ°€ λΆ€λͺ¨μ˜ μ™Όμͺ½μ— μ‚½μž… λ˜λŠ” κ²½μš°μ΄λ‹€.
μƒˆλ‘œ μΆ”κ°€ 된 λ…Έλ“œ xλ₯Ό κΈ°μ€€μœΌλ‘œ pλ…Έλ“œμ˜ ν˜•μ œμΈ sλ…Έλ“œμ˜ 색이 Black인 것을 μ•Œ 수 μžˆλ‹€. Recoloring을 μˆ˜ν–‰ ν•  λ•Œ s의 색이 Red인 κ²½μš°λž‘ λ‹€λ₯΄λ‹€.
이와 같은 경우 pλ₯Ό κΈ°μ€€μœΌλ‘œ right rotateν•˜κ³  p의 색을 pp둜, p의 색을 Black으둜 λ³€κ²½ ν•΄μ£Όλ©΄ λœλ‹€.
Rotation의 두 번째 κ²½μš°λŠ” μƒˆλ‘œ μΆ”κ°€λ˜λŠ” λ…Έλ“œμ˜ μœ„μΉ˜κ°€ pλ…Έλ“œμ˜ 였λ₯Έμͺ½μ— μ‚½μž… 될 λ•Œμ΄λ‹€.
이 κ²½μš°λŠ” Rotation의 첫 번째 경우둜 λ§Œλ“€μ–΄ μ£ΌλŠ” 연산을 μˆ˜ν–‰ν•œλ‹€. κ·Έλ ‡κ²Œ 되기 μœ„ν•΄μ„œλŠ” pλ₯Ό κΈ°μ€€μœΌλ‘œ left rotate μž‘μ—…μ„ μˆ˜ν–‰ν•˜λ©΄ λœλ‹€.
결과적으둜 pλ₯Ό κΈ°μ€€μœΌλ‘œ s의 색이 Black인 첫 번째 경우의 ν˜•νƒœκ°€ λ§Œλ“€μ–΄μ‘Œλ‹€. λ‹€μ‹œ p의 λΆ€λͺ¨μΈ xλ₯Ό κΈ°μ€€μœΌλ‘œ right rotateλ₯Ό μˆ˜ν–‰ν•œλ‹€.

Β Red-black tree insertion - μ‚­μ œ μ—°μ‚°

νŠΈλ¦¬μ— 값을 μΆ”κ°€ν•˜λŠ” μ‚½μž… μ—°μ‚° 외에도 값을 μ‚­μ œν•˜λŠ” 연산도 μ‘΄μž¬ν•œλ‹€. μ‚­μ œ μ—°μ‚°μ—μ„œλŠ” 4번 ν•­λͺ© 뿐만 μ•„λ‹ˆλΌ 5번 ν•­λͺ©μ˜ μœ„λ°˜λ„ 특히 κ³ λ €λ₯Ό 해보아야 ν•œλ‹€.
4. Red λ…Έλ“œμ˜ μžμ‹ 색은 λ°˜λ“œμ‹œ Black이닀. ( Red - Red둜 μ΄μ–΄μ§€λŠ” 연결은 μ•ˆ 됨. B - BλŠ” κ°€λŠ₯) 5. 각 λ…Έλ“œμ—μ„œ 리프 λ…Έλ“œμ— λ‹€λ‹€λ₯Ό λ•ŒκΉŒμ§€ Black λ…Έλ“œ κ°œμˆ˜λŠ” λͺ¨λ‘ κ°™λ‹€.
색이 Red인 λ…Έλ“œλ₯Ό μ‚­μ œν•˜λŠ” 것은 큰 λ¬Έμ œκ°€ λ°œμƒν•˜μ§€ μ•ŠλŠ”λ‹€. ν•˜μ§€λ§Œ 색이 Black인 λ…Έλ“œμ˜ Black μžμ‹λ…Έλ“œλ₯Ό μ‚­μ œν•˜κ²Œ 되면 νŠΈλ¦¬μ—μ„œ Black λ…Έλ“œκ°€ ν•˜λ‚˜ μ‚¬λΌμ§€λŠ” 것이기 λ•Œλ¬Έμ— 5번 νŠΉμ§•μ΄ μœ„λ°°λ˜μ–΄ λ°ΈλŸ°μŠ€μ— 큰 λ¬Έμ œμ μ„ μ•ˆκ²¨μ€„ 수 μžˆλ‹€. λ‹€μ‹œ λ§ν•˜λ©΄ λΆ€λͺ¨μ™€ μžμ‹ λͺ¨λ‘ Black일 λ•Œ Black μžμ‹ λ…Έλ“œλ₯Ό μ‚­μ œν•˜λ©΄ λ¬Έμ œκ°€ λ°œμƒν•œλ‹€.
μœ„μ™€ 같이 μ‚­μ œν•˜λ €κ³  ν•˜λŠ” λ…Έλ“œλ₯Ό m, κ·Έ μžμ‹μ„ x라고 ν–ˆμ„ λ•Œ m을 μ‚­μ œν•˜λŠ” 것은 λ¬Έμ œκ°€ μ—†λ‹€. pp의 색은 μ–΄λŠ 색이 와도 큰 상관이 μ—†λ‹€. μ™œλƒν•˜λ©΄ μ‚­μ œ 연산을 진행 ν•œ λ’€ rotationμ΄λ‚˜ coloring μž‘μ—…μ„ μˆ˜ν–‰ν•˜λ©΄ 되기 λ•Œλ¬Έμ΄λ‹€.
μœ„μ™€ 같은 경우 μ‚­μ œν•˜κ³ μž ν•˜λŠ” λ…Έλ“œμ˜ 색은 Black이고 μžμ‹μ˜ λ…Έλ“œλŠ” Red이닀. 이 경우 λŒ€μ²΄ 된 x의 색을 Black으둜 λ³€ν™˜μ‹œν‚¨λ‹€. λ‹Ήμ—°νžˆ μœ„μ˜ μ˜ˆμ‹œλŠ” 단일 νŠΈλ¦¬κ°€ μ•„λ‹Œ μœ„λ‚˜ μ•„λž˜μ— 좔가적인 νŠΈλ¦¬κ°€ μ‘΄μž¬ν•  수 μžˆλ‹€.
λ¬Έμ œκ°€ μƒκΈ°λŠ” κ²½μš°λŠ” mκ³Ό x λͺ¨λ‘ Black인 κ²½μš°μ΄λ‹€.
λ‹€μŒμ€ mκ³Ό xκ°€ λͺ¨λ‘ Black일 λ•Œ μ§€μš΄ μ§ν›„μ˜ μ‘°μ • 과정을 μ†Œκ°œν•œλ‹€.
λ¨Όμ € m의 Parent, μœ„μ˜ κ·Έλ¦Όμ—μ„œλŠ” ppκ°€ Red일 λ•Œμ˜ κ²½μš°μ΄λ‹€. μ˜ˆμ‹œλ₯Ό 쑰금 μˆ˜μ •ν•˜λ©΄ λ‹€μŒκ³Ό κ°™λ‹€.
λ¨Όμ € ppκ°€ p둜 λ³€ν–ˆκ³ , pκ°€ Red일 λ•Œμ˜ κ²½μš°μ΄λ‹€.
1.
pκ°€ Red이고 Lκ³Ό R이 Black, Black일 λ•Œ ( SλŠ” λ‹Ήμ—°νžˆ Black일 수 밖에 μ—†λ‹€ )
β†’ λ‹¨μˆœνžˆ p와 s의 색을 λ³€κ²½ν•œλ‹€.
2.
pκ°€ Red이고 Lκ³Ό R이 *, Red일 λ•Œ ( SλŠ” λ‹Ήμ—°νžˆ Black일 수 밖에 μ—†λ‹€ )
μ—¬κΈ°μ„œ * 와 λ…Έλ“œμ˜ 색이 White 인 것은 μ–΄λŠ 색이든지 올 수 μžˆλ‹€λŠ” μ˜λ―Έμ΄λ‹€.
pλ₯Ό κΈ°μ€€μœΌλ‘œ left-rotate β†’ p와 s의 색을 λ³€κ²½ β†’ R의 색을 Black으둜 λ³€κ²½
3.
pκ°€ Red이고 Lκ³Ό R이 Red, Black일 λ•Œ ( SλŠ” λ‹Ήμ—°νžˆ Black일 수 밖에 μ—†λ‹€ )
μ—¬κΈ°μ„œ * 와 λ…Έλ“œμ˜ 색이 White 인 것은 μ–΄λŠ 색이든지 올 수 μžˆλ‹€λŠ” μ˜λ―Έμ΄λ‹€.
sλ₯Ό κΈ°μ€€μœΌλ‘œ right-rotate β†’ Lκ³Ό s의 색을 λ³€κ²½ β†’ Case 2둜 이동
pλ₯Ό κΈ°μ€€μœΌλ‘œ left-rotate β†’ p와 L의 색을 λ³€κ²½ β†’ s의 색을 Black으둜 λ³€κ²½
1.
pκ°€ Black이고 S와 Lκ³Ό R이 Black, Black, Black일 λ•Œ
λͺ¨λ“  λ…Έλ“œκ°€ Black일 경우, x의 sibling의 색을 Red둜 λ³€κ²½ν•œλ‹€. 그러면 xμ—μ„œ λ°œμƒ λ˜μ—ˆλ˜ Blackλ…Έλ“œμ˜ λΆ€μ‘± λ¬Έμ œκ°€ p둜 μ „μŠΉλ˜λŠ”λ°, 이 경우 pλ₯Ό κΈ°μ€€ λ…Έλ“œλ‘œ ν•˜μ—¬ μž¬κ·€μ μœΌλ‘œ 문제λ₯Ό ν•΄κ²°ν•œλ‹€.
2.
pκ°€ Black이고 S와 Lκ³Ό R이 Black, *, Red일 λ•Œ
pλ₯Ό κΈ°μ€€μœΌλ‘œ left-rotate β†’ p와 s의 색을 λ°”κΏˆ β†’ R의 색을 Black으둜 λ³€κ²½
3.
pκ°€ Black이고 S와 Lκ³Ό R이 Black, Red, Black일 λ•Œ
sλ₯Ό κΈ°μ€€μœΌλ‘œ right-rotate β†’ Lκ³Ό s의 색을 λ°”κΏˆ
2번 μΌ€μ΄μŠ€λ‘œ μ΄λ™ν•˜μ—¬ 진행 μ‘°μ •
pλ₯Ό κΈ°μ€€μœΌλ‘œ left-rotate β†’ p와 L의 색을 λ°”κΏˆ β†’ s의 색을 Black으둜 λ³€κ²½
4.
pκ°€ Black이고 S와 Lκ³Ό R이 Red, Black, Black일 λ•Œ
pλ₯Ό κΈ°μ€€μœΌλ‘œ left-rotate β†’ p와 s의 색을 λ°”κΏˆ.
μ—¬κΈ°κΉŒμ§€ 쑰정을 λ§ˆμ³€μ„ λ•Œ 아직 xλ₯Ό κ΄€ν†΅ν•˜λŠ” 경둜의 Black λ…Έλ“œλŠ” λΆ€μ‘±ν•˜λ‹€. λ”°λΌμ„œ xλ₯Ό κΈ°μ€€μœΌλ‘œ 쑰정을 κ³„μ†ν•΄μ•Όν•œλ‹€. x와 p 그리고 L을 보면 pκ°€ Red일 λ•Œμ˜ κ²½μš°κ°€ λ– μ˜€λ₯Έλ‹€. λ”°λΌμ„œ μƒλ‹¨μ˜ 1, 2, 3 쀑 ν•˜λ‚˜λ₯Ό μ„ νƒν•˜μ—¬ ν•΄κ²°ν•˜λ©΄ λœλ‹€.
μ‚½μž…κ³Ό μ‚­μ œ μ—°μ‚° λͺ¨λ‘ λ¬Έμ œκ°€ λ°œμƒν•˜λŠ” 지점을 κΈ°μ€€μœΌλ‘œ Recoloring, Rotation λ“±μ˜ 연산을 μˆ˜ν–‰ν•œλ‹€. μ‚­μ œ μ—°μ‚°μ—μ„œλŠ” μ‘°μ • 후에 Black λ…Έλ“œκ°€ λΆ€μ‘±ν•œ 뢀뢄이 전이 될 수 있고 이와 같은 ν˜„μƒμ΄ λ°œμƒ 될 μ‹œ λΆ€μ‘±ν•œ 뢀뢄을 κΈ°μ€€μœΌλ‘œ μž¬κ·€μ μœΌλ‘œ 문제λ₯Ό ν•΄κ²°ν•΄λ‚˜κ°€λ©΄ λœλ‹€.
이제 μ§„μ§œ map의 μ‹œμž‘μ΄λ‹€.
map은 Associative container둜 킀와 κ°’ (Key and Value)둜 이루어져 μžˆλŠ” ꡬ쑰이닀. 이 λ•Œ ν‚€(Key) 값은 κ³ μœ ν•˜λ©° 쀑볡을 ν—ˆμš©ν•˜μ§€ μ•ŠλŠ”λ‹€. λ”ν•΄μ„œ STL의 map은 Red-black tree둜 κ΅¬ν˜„λ˜μ–΄ 있기 λ•Œλ¬Έμ— μžλ™μœΌλ‘œ μ •λ ¬λ˜μ–΄ 있고 λΉ λ₯Έ 검색, μ‚½μž…, μ‚­μ œ μ‹œκ°„μ„ 보μž₯λ°›λŠ”λ‹€.
Prototype
template < class Key, // map::key_type class T, // map::mapped_type class Compare = less<Key>, // map::key_compare class Alloc = allocator<pair<const Key,T> > // map::allocator_type > class map;
C++
볡사
Member types
public: // types: typedef Key key_type; typedef T mapped_type; typedef pair<const key_type, mapped_type> value_type; typedef Compare key_compare; typedef Allocator allocator_type; typedef typename allocator_type::reference reference; typedef typename allocator_type::const_reference const_reference; typedef typename allocator_type::pointer pointer; typedef typename allocator_type::const_pointer const_pointer; typedef typename allocator_type::size_type size_type; typedef typename allocator_type::difference_type difference_type; typedef implementation-defined iterator; typedef implementation-defined const_iterator; typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
C++
볡사
Constructor
β†’ Empty constructor with no element
explicit map (const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type());
C++
볡사
β†’ Range constructor
template <class InputIterator> map (InputIterator first, InputIterator last, const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type());
C++
볡사
β†’ Copy constructor
map (const map& x);
C++
볡사
Parameter
Parameter
Description
comp
비ꡐ μ—°μ‚° 객체. 두 개의 ν‚€λ₯Ό λΉ„κ΅ν•˜μ—¬ 첫 번째 μΈμˆ˜κ°€ 두 번째 μΈμˆ˜λ³΄λ‹€ μž‘κ³  μ•žμ— μœ„μΉ˜ν•œλ‹€λ©΄ trueλ₯Ό, μ•„λ‹ˆλ©΄ falseλ₯Ό λ°˜ν™˜ν•œλ‹€. key_compare()λŠ” container λ‚΄μ—μ„œ μ‚¬μš©λ˜λŠ” 비ꡐ μ—°μ‚° 객체둜 template parameter 쀑 Comp의 λ³„μΉ­μœΌλ‘œ μ‚¬μš©λœλ‹€.
alloc
Allocator object둜 μ»¨ν…Œμ΄λ„ˆλŠ” 이 allocatorλ₯Ό μœ μ§€ν•˜κ³  μ‚¬μš©ν•œλ‹€.
first, last
input iterator둜 λ²”μœ„μ˜ 처음과 끝을 가리킨닀. 주의 ν•΄μ•Όν•  점은 λ²”μœ„λŠ” μ²˜μŒλΆ€ν„° λ§ˆμ§€λ§‰ μ‚¬μ΄μ˜ λͺ¨λ“  μš”μ†Œλ₯Ό ν¬ν•¨ν•˜μ§€λ§Œ λ§ˆμ§€λ§‰ μš”μ†ŒλŠ” ν¬ν•¨ν•˜μ§€ μ•ŠλŠ”λ‹€.
x
λ™μΌν•œ μœ ν˜•μ˜ λ‹€λ₯Έ 맡 개체(λ™μΌν•œ 클래슀 ν…œν”Œλ¦Ώ 인수 Key, T, Compare 및 Allocate)둜, 이 x의 λ‚΄μš©μ΄ λ³΅μ‚¬λ˜κ±°λ‚˜ νšλ“λœλ‹€.
il
초기 리슀트 였브젝트
Iterators
β†’ begin
iterator begin(); const_iterator begin() const;
C++
볡사
map container의 첫 번째 μš”μ†Œλ₯Ό μ°Έμ‘°ν•˜λŠ” 반볡자λ₯Ό λ°˜ν™˜ν•œλ‹€. μ€‘μš”ν•œ 것은 map은 항상 정렬이 λ˜μ–΄ 있고 μ‚½μž…μ΄λ‚˜ μ‚­μ œ μ—°μ‚° μ‹œ 첫 번째 μš”μ†Œκ°€ λ°”λ€” 수 μžˆλ‹€λŠ” 것이닀.
β†’ end
iterator end(); const_iterator end() const;
C++
볡사
map container의 λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό μ°Έμ‘°ν•˜λŠ” 반볡자λ₯Ό λ°˜ν™˜ν•œλ‹€.
β†’ rbegin
reverse_iterator rbegin(); const_reverse_iterator rbegin() const;
C++
볡사
μ»¨ν…Œμ΄λ„ˆμ˜ λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό κ°€λ¦¬ν‚€λŠ” μ—­λ°©ν–₯ Iteratorλ₯Ό λ°˜ν™˜ν•œλ‹€.
β†’ rend
reverse_iterator rend(); const_reverse_iterator rend() const;
C++
볡사
μ»¨ν…Œμ΄λ„ˆμ˜ 첫 번째 μš”μ†Œλ₯Ό κ°€λ¦¬ν‚€λŠ” μ—­λ°©ν–₯ Iteratorλ₯Ό λ°˜ν™˜ν•œλ‹€.
Capacity
β†’ empty
bool empty() const;
C++
볡사
μ»¨ν…Œμ΄λ„ˆκ°€ λΉ„μ–΄μžˆλŠ”μ§€ μ•„λ‹Œμ§€μ˜ μ—¬λΆ€λ₯Ό λ°˜ν™˜ν•œλ‹€.
β†’ size
size_type size() const;
C++
볡사
map container 내에 μžˆλŠ” μš”μ†Œμ˜ 개수λ₯Ό λ°˜ν™˜ν•œλ‹€.
β†’ max_size
size_type max_size() const;
C++
볡사
map containerκ°€ κ°–κ³  μžˆμ„ 수 μžˆλŠ” μš”μ†Œμ˜ μ΅œλŒ€ 개수λ₯Ό λ°˜ν™˜ν•œλ‹€.
Element access
β†’ operator[]
mapped_type& operator[] (const key_type& k);
C++
볡사
kκ°€ μ»¨ν…Œμ΄λ„ˆμ— μžˆλŠ” μš”μ†Œμ˜ 킀와 μΌμΉ˜ν•˜λ©΄ ν•¨μˆ˜λŠ” λ§€ν•‘λœ 값에 λŒ€ν•œ μ°Έμ‘°λ₯Ό λ°˜ν™˜ν•œλ‹€.
kκ°€ μ»¨ν…Œμ΄λ„ˆμ— μžˆλŠ” μš”μ†Œμ˜ 킀와 μΌμΉ˜ν•˜μ§€ μ•ŠμœΌλ©΄ ν•¨μˆ˜λŠ” ν•΄λ‹Ή ν‚€λ‘œ μƒˆ μš”μ†Œλ₯Ό μ‚½μž…ν•˜κ³  λ§€ν•‘λœ 값에 λŒ€ν•œ μ°Έμ‘°λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€. 이 경우 map에 λ”°λ‘œ 값을 μΆ”κ°€ν•˜μ§€ μ•Šμ•„λ„ μƒμ„±μžλ₯Ό 톡해 map에 객체가 ν• λ‹Ήλœλ‹€.
β†’ at
mapped_type& at (const key_type& k); const mapped_type& at (const key_type& k) const;
C++
볡사
k둜 λ§€ν•‘λœ 값에 λŒ€ν•œ μ°Έμ‘°λ₯Ό λ°˜ν™˜ν•œλ‹€. λ§Œμ•½ μ•„λ¬΄λŸ° 값도 맀핑 λ˜μ§€ μ•ŠλŠ”λ‹€λ©΄ out_of_range 였λ₯˜λ₯Ό throwν•œλ‹€.
Modifiers
β†’ insert
//single element pair<iterator,bool> insert (const value_type& val); //with hint iterator insert (iterator position, const value_type& val); //range template <class InputIterator> void insert (InputIterator first, InputIterator last);
C++
볡사
Β νŒŒλΌλ―Έν„° position은 val이 μ‚½μž… 될 μœ„μΉ˜μ— λŒ€ν•œ 힌트둜, val의 ν‚€ 값보닀 μ•žμ˜ μš”μ†Œλ₯Ό κ°€λ¦¬ν‚€κ²Œ 되면 μ‚½μž… μ‹œκ°„μ„ μ΅œμ ν™” ν•  수 μžˆλ‹€.
Β pair<iterator, bool>의 pair::firstλŠ” μƒˆλ‘œ μ‚½μž…λœ μš”μ†Œ λ˜λŠ” λ™μΌν•œ ν‚€λ₯Ό κ°–κ³  μžˆλŠ” μš”μ†Œλ₯Ό κ°€λ¦¬ν‚€λŠ” iterator둜 μ„€μ •λœλ‹€. pair::secondλŠ” λ§Œμ•½ λ™μΌν•œ ν‚€λ₯Ό 가진 μš”μ†Œκ°€ map 내에 μžˆλ‹€λ©΄ falseλ₯Ό, μ •μƒμ μœΌλ‘œ 잘 μ‚½μž…ν•˜μ˜€λ‹€λ©΄ trueλ₯Ό λ°˜ν™˜ν•œλ‹€.
β†’ erase
void erase (iterator position); size_type erase (const key_type& k); void erase (iterator first, iterator last);
C++
볡사
λ²”μœ„ ν˜Ήμ€ νŠΉμ • μœ„μΉ˜μ˜ μš”μ†Œλ₯Ό μ‚­μ œν•œλ‹€.
β†’ swap
void swap (map& x);
C++
볡사
μ»¨ν…Œμ΄λ„ˆ μš”μ†Œμ˜ λ‚΄μš©μ„ λ‹€λ₯Έ μ»¨ν…Œμ΄λ„ˆ μš”μ†Œμ˜ λ‚΄μš©μ„ κ΅ν™˜ν•œλ‹€.
β†’ clear
void clear();
C++
볡사
map container λ‚΄μ˜ λͺ¨λ“  λ‚΄μš©μ„ μ‚­μ œν•œλ‹€.
Observers
β†’ key_comp
key_compare key_comp() const;
C++
볡사
ν‚€λ₯Ό λΉ„κ΅ν•˜κΈ° μœ„ν•΄ μ‚¬μš©λ˜λŠ” 비ꡐ 객체의 볡사본을 λ°˜ν™˜ν•œλ‹€. μ—¬κΈ°μ„œ λ§ν•˜λŠ” 비ꡐ κ°μ²΄λŠ” construct λ•Œ κ²°μ • λ˜λŠ”λ° ν…œν”Œλ¦Ώ λ§€κ°œλ³€μˆ˜μ˜ μ„Έ 번째 μš”μ†Œμ΄λ‹€. κΈ°λ³Έ 값은 less이고, operator<와 같은 κΈ°λŠ₯을 μˆ˜ν–‰ν•œλ‹€.
이 비ꡐ κ°μ²΄λŠ” μš”μ†Œμ˜ μˆœμ„œλ₯Ό κ²°μ •ν•˜κ³  첫 번째 μΈμˆ˜κ°€ 두 번째 μΈμˆ˜λ³΄λ‹€ λ¨Όμ € 였면 True, μ•„λ‹ˆλ©΄ Falseλ₯Ό λ°˜ν™˜ν•œλ‹€. λ§Œμ•½ 두 ν‚€κ°€ κ°™μœΌλ©΄ Falseλ₯Ό λ°˜ν™˜ν•œλ‹€.
β†’ value_comp
value_compare value_comp() const;
C++
볡사
map의 value_type을 λΉ„κ΅ν•˜μ—¬ μš”μ†Œμ˜ μˆœμ„œλ₯Ό λΉ„κ΅ν•˜λŠ” ν•¨μˆ˜. μ—¬κΈ°μ„œ λ§ν•˜λŠ” value_type은 pair<const key_type, mapped_type> 을 λ§ν•œλ‹€. ν•˜μ§€λ§Œ 이 λΉ„κ΅μ—μ„œ mapped_type은 λΉ„κ΅λ˜μ§€ μ•ŠλŠ”λ‹€.

Β Set

Set은 Keyκ°’λ§Œ κ°–κ³  μžˆλ‹€λŠ” μ μ—μ„œ map의 Key와 Value의 쌍으둜 μ΄λ£¨μ–΄μ ΈμžˆλŠ” νŠΉμ§•κ³Ό λ‹€λ₯΄λ‹€.
이 점을 μ œμ™Έν•˜κ³ λŠ” mapκ³Ό set의 νŠΉμ§•μ€ κ°™λ‹€.
Mapκ³Ό Set의 μ—°μ‚° 별 μ‹œκ°„ λ³΅μž‘λ„
Type
Time complexity
Find
O(logn)O(logn)
Insert
O(logn)O(logn)
Delete
O(logn)O(logn)

Β Reference