subject
subject.pdf
๊ณผ์ ๋ชฉํ
STL์ ์๋ ์ปจํ
์ด๋ ๋ง๋ค๊ธฐ
๋ก๋๋งต
{ 1phase / container ์ ํ์ ์์ ๊ฐ์ก๊ธฐ but ์ด๋จ๊ณ์์ ๋ด๊ฐ ๋ญ ๋ง๋๋์ง ๋ชจ๋ฅด๋๊ฒ ๋น์ฐ } // ๊ถ์ฅ ํ์ต ๊ธฐ๊ฐ 2~3์ผ
0. ์ฌ์ ์ง์ ๊ณต๋ถ - SFINAE์ enable_if, allocator, is_integral, iterator traits ๋ฑ
1. // type traits - enable_if, is_integral ๋ค ๊ตฌํ
2. iterator traits
3. algorithm - equal, lexicographical_compare
4. pair
{ 2phase / ์์์ ๋ง๋ ๊ฑธ ์จ๋จน์ผ๋ฉด์ ์ดํด๋ ๋์ด๊ธฐ } // ๊ถ์ฅ ํ์ต ๊ธฐ๊ฐ 2์ผ
5. random access iterator
6. reverse iterator
{ 3phase / ์์์ ๋ง๋ ๊ฑธ ์ฌ์ฉํ๋ฉด์ ์ปจํ
์ด๋๋ฅผ ์ง์ ๊ตฌํ } // ๊ถ์ฅ ํ์ต ๊ธฐ๊ฐ 3์ผ
7. vector ๊ตฌํ
8. vector ๊ตฌํ ์ดํ adaptor container ๋ก stack ๊ตฌํ
{ 4phase / map์ ๋ํด ๊ณต๋ถํ๊ณ tree๋ฅผ ๊ตฌํํ ์ค๋น } // ๊ถ์ฅ ํ์ต ๊ธฐ๊ฐ 2์ผ
9. tree node
10. tree iterator
{ 5phase / map ๊ตฌํ } // ๊ถ์ฅ ํ์ต ๊ธฐ๊ฐ 3์ผ
11. map -> set (์ธํฐํ์ด์ค๋ง ๊ตฌํ)
12. red black tree ์ด๋ avl tree ๋ก์ง์ ์ฌ์ฉํ์ฌ ํธ๋ฆฌ ๊ตฌํ (๋ด๋ถ๋ก์ง ๊ตฌํ)
/* ์ฐธ๊ณ ์ฌํญ
- ๋๋ถ๋ถ ๊ตฌํํด์ผํ ๊ฒ๋ค์ cppreference.com ์ด๋ cplusplus.com ์์ ํ์ธ ๊ฐ๋ฅ
*/
Plain Text
๋ณต์ฌ
C++์ ์ ๋๋ฆญํ ํ๋ก๊ทธ๋๋ฐ์ ์ํด template์ ์ง์ํ๋ค. template์ด๋ ํจ์๋ ํด๋์ค๋ฅผ ๋ค์ํ ์๋ฃํ์ผ๋ก ์ฌ์ฉํ ์ ์๋๋ก ๋ง๋ ํ์ด๋ค. ๋ํ ์ด๋ฐ template์ ์ด์ฉํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ Standard Template Library๋ผ๊ณ ํ๋ค.
โข
์์ ํ์
์ ๊ฐ์ฒด๋ฅผ ๋ณด๊ดํ ์ ์๋ ์ปจํ
์ด๋ (container)
โข
์ปจํ
์ด๋์ ๋ณด๊ด๋ ์์์ ์ ๊ทผํ ์ ์๋ ๋ฐ๋ณต์ (iterator)
โข
๋ฐ๋ณต์๋ค์ ๊ฐ์ง๊ณ ์ผ๋ จ์ ์์
์ ์ํํ๋ ์๊ณ ๋ฆฌ์ฆ (algorithm)
STL์ ๋ณดํต ์ด ์ธ ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์๋ฏธํ๋ค.
์ด STL์ container๋ฅผ ๊ตฌํํ๋ ๊ฒ์ด ์ด ๊ณผ์ ์ ์ฃผ์ ์ด๊ณ , ์ด๋ฅผ ์ํด ํ์ํ iterator์ algorithm๋ฑ์ ๊ตฌํํด์ผ ํ๋ค.
์ฌ์ ์ง์์ธ enable_if, SFINAE๋ฅผ ๊ณต๋ถํ๋ ค๊ณ ๊ฒ์ํ์ ๋ ๋์ค๋ ์ฝ๋๋ ์๋์ ๊ฐ์ ํํ์ด๋ค.
struct T {
enum { int_t, float_t } type;
template <typename Integer,
std::enable_if_t<std::is_integral<Integer>::value, bool> = true
>
T(Integer) : type(int_t) {}
template <typename Floating,
std::enable_if_t<std::is_floating_point<Floating>::value, bool> = true
>
T(Floating) : type(float_t) {} // OK
};
C++
๋ณต์ฌ
๋ฉํ ๋ฐ์ฌ์ญโฆ ๋๋์ฒด ๋ญ์๋ฆฐ์ง ํ๋๋ ๋ชจ๋ฅด๊ฒ ๋ค.
์ฐ์ enable_if ์ is_integral์ c++ 11 ๋ฒ์ ์ ๊ธฐ๋ฅ์ด์ง๋ง, ๊ณผ์ ์์๋ ๊ตฌํํ๋ผ๊ณ ๋์ด ์๊ธฐ ๋๋ฌธ์ ์ฐ์ ์ฐพ์๋ณด์.
์ฐ์ SFINAE๋ ๋งํฌ ๋ก ๋์ถฉ ๊ฐ๋
๋ง ๊ฐ์ ์ก์๋ค. ์๋ฅผ ๋ค์ด, value_type์ด๋ผ๋ ๋ฉค๋ฒํ์
์ ๊ฐ์ง๊ณ ์๋ ํด๋์ค๊ฐ ์
๋ ฅ๋์ด์ผ ํ๋ ํ
ํ๋ฆฟ์์, int ๋ฑ์ ์๋ฃํ์ด ๋ค์ด์์ ๋ ์ค๋ฅ๋ฅผ ๋ฑ์ด๋ด๋ ๊ฒ์ด ์๋๋ผ ๊ทธ๋ฅ ์ค๋ฒ๋ก๋ฉ ํ๋ณด์์ ์ ์ธ์ํค๋ ๊ฒ์ด๋ค.
๋ฌผ๋ก int๋ฅผ ๋ฐ์ ์ ์๋ ํจ์๊ฐ ์์ ์์ผ๋ฉด ๋น์ฐํ no matching error๊ฐ ๋์ง๋ง, ์ฌ๊ธฐ์ ์ค๋ฅ๊ฐ ์๋๋ผ๋ ๋ป์ โ์ปดํ์ผ๋ฌ๊ฐ ์ปดํ์ผ ๊ณผ์ ์์ template ํจ์๋ฅผ ํ์ธํ๋ ๊ณผ์ ์์ ์ค๋ฅ๊ฐ ๋์ง ์๋๋ค' ๋ ๊ฒ์ด๋ค.
#include <iostream>
int negate(int i) {
std::cout << "int" << std::endl;
return -i; }
template <typename T>
typename T::value_type negate(const T& t) {
std::cout << "SFINAE" << std::endl;
return -T(t);
}
int main()
{
negate('c');
}
C++
๋ณต์ฌ
์์ ์ฝ๋๋ฅผ ์คํํ๋ฉด, ์ถ๋ ฅ์ int ๊ฐ ๋์ค๊ฒ ๋๋ค. char ์๋ฃํ์ธ c ๊ฐ ํจ์์ ์ ์ฉ๋์ด์ผ ํ๋๋ฐ, value_type์ด ์๊ธฐ ๋๋ฌธ์ typename T::value_type negate(const T& t) ํ
ํ๋ฆฟ์๋ ์ ์ฉ๋์ง ์๋๋ค. ํ์ง๋ง SFINAE๋ก ์ธํด ์ค๋ฅ๊ฐ ๋์ง ์๊ณ ์ค๋ฒ๋ก๋ฉ ํ๋ณด์์ ์ ์ธ๋์ด char ์๋ฃํ์ด int ์๋ฃํ์ผ๋ก ๋ณํ๋์ด int negate(int i) ํจ์๊ฐ ์คํ๋๋ค.
ํ์ง๋ง ์์ template ํจ์๋ฅผ ์๋ ์ฝ๋๋ก ๋ณ๊ฒฝํ์ฌ ์คํํ๋ฉด ์ปดํ์ผ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
#include <iostream>
int negate(int i) {
std::cout << "int " << std::endl;
return -i; }
template <typename T>
void negate(const T& t) {
typename T::value_type n = -t();
}
int main()
{
negate('c');
}
C++
๋ณต์ฌ
์ด ์ฝ๋๋ ์ปดํ์ผ ๋จ๊ณ์์ error: type 'char' cannot be used prior to '::' because it has no members ์๋ฌ๋ฅผ ๋ฑ๋๋ค. char ์๋ฃํ์๋ ๋ฉค๋ฒ๊ฐ ์๊ธฐ ๋๋ฌธ์ :: ์ ํตํ ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅํ๋ค๋ ์ด์ผ๊ธฐ์ด๋ค. ์ฆ, ์ปดํ์ผ๋จ๊ณ์์ template ํจ์์ char ๋ฅผ ๋ฃ์ด๋ณด๋ ๊ณผ์ ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ ๊ฒ์ด๋ค. ์ด ๊ฒฝ์ฐ๋ SFINAE๊ฐ ์ ์ฉ๋์ง ์์ ๊ฒ์ด๋ค.
์๋ํ๋ฉดย T::value_type๋ ํจ์ ํ์
๊ณผ ํ
ํ๋ฆฟ ํ์
์ธ์์ ์ฆ๊ฐ์ ์ธ ๋งฅ๋ฝ(immediate context) ๋ฐ๊นฅ์ ์๊ธฐ ๋๋ฌธ์
๋๋ค. ๋ฐ๋ผ์ ํ์ค ๊ท์ ์ ๋ฐ๋ผ ์ด๋ย SFINAEย ์ ์ ์ฉ ๋ฒ์๋ฅผ ๋์ด์ญ๋๋ค. -๋ชจ๋์ ์ฝ๋
๋ฐ๋ผ์ ์ฐ๋ฆฌ๊ฐ ์ธ๋ฐ ์๋ ์ปดํ์ผ ์ค๋ฅ๋ฅผ ๋ฑ์ง ์๊ฒ ํ๋ ค๋ฉด, ํจ์ ๋ด๋ถ๊ฐ ์๋ ํจ์์ ์ ์ธ๋ถ์ ํ์
์ ๋ฃ์ด์ผ โ์นํ์คํจ๋ก ์ธํ ์ปดํ์ผ ํ๋ณด ์ ์ธ' ๋ง ์ผ์ด๋๊ณ โ์ปดํ์ผ ์ค๋ฅ' ๋ ์ผ์ด๋์ง ์๊ฒ ๋๋ค. ์ด๋ฐ SFINAE๋ฅผ ์ ์ฌ์ฉํ๋ ํด ์ค ํ๋๊ฐ ๋ฐ๋ก enable_if ์ด๋ค.
template <bool, typename T = void>
struct enable_if {};
template <typename T>
struct enable_if<true, T> {
typedef T type;
};
C++
๋ณต์ฌ
enable_if ๋ ๋ค์๊ณผ ๊ฐ์ด ์ ์ํ ์ ์๋ค. ์ฌ๊ธฐ๋ถํฐ ๋ ํท๊ฐ๋ฆฐ๋ค.
์ฐ์ , C++์์ struct์ class ๋ ๊ฐ๋ค๊ณ ๋ณผ ์ ์๋ค. ๊ทธ๋์ struct๋ฅผ ํด๋์ค๋ผ๊ณ ์นญํ๋ ๋ธ๋ก๊ทธ๋ ๋ ํผ๋ฐ์ค๋ ์์!
template <bool, typename T = void> struct enable_if {};
์์ typename T = void ๋ ์๋ฃํ์ด ์๋ค์ด์ค๋ฉด void๋ผ๊ณ ํ๋ค๋ ๋ป!
enable_if ์ ์๋ฃํ์ผ๋ก true, T ๊ฐ ๋ค์ด์ค๋ฉด T ํ์์ type์ด ์๊ธด๋ค. ๋ฐ๋ผ์,
template <class T>
typename std::enable_if<std::is_integral<T>::value, bool>::type
is_odd (T i) {return bool(i%2);}
C++
๋ณต์ฌ
is_integral<T>::value ๊ฐ true ์ด๋ฉด enable_if์ type์ด๋ผ๋ bool์๋ฃํ์ ํ์
์ด ์๊ธฐ๊ธฐ ๋๋ฌธ์ ์์ ์ฝ๋๊ฐ ์ปดํ์ผ์ด ๊ฐ๋ฅํ๋ค. ํ์ง๋ง ๋ง์ฝ is_integral ์ ๋ค์ด๊ฐ ์๋ฃํ T๊ฐ false, ์ฆ ์ ์ํ์ด ์๋ ์๋ฃํ์ด๋ผ๋ฉด enable_if์์ type์ด๋ผ๋ ๋ฉค๋ฒํ์
์ด ์๊ธฐ์ง ์๊ธฐ ๋๋ฌธ์ ์ปดํ์ผ ์๋ฌ๊ฐ ๋ฌ๋ค.
๊ทธ๋ฆฌ๊ณ ์ฐ๋ฆฌ๋ is_integral ์ญ์ ๊ตฌํํด์ผ ํ๋ค.
template <class T> struct is_integral;
C++
๋ณต์ฌ
is_integral์ ์๋ฃํ T๊ฐ ์ ์ ์ ํ์ธ์ง ์๋ณํ๋ ํด๋์ค์ด๋ค.
T ๊ฐ ์ ์ ์ ํ์ธ์ง์ ๋ฐ๋ผ integral_constant ๋ฅผ true_type ํน์ false_type์ผ๋ก ์์๋ฐ๋๋ค.
template <class T, T v> struct integral_constant {
static constexpr T value = v;
typedef T value_type;
typedef integral_constant<T,v> type;
constexpr operator T() { return v; }
};
C++
๋ณต์ฌ
์ฐ์ , ์ฒ์๋ณด๋ ์ constexpr ์ ๋ํด ์์๋ณด์.
11๋ฒ์ ๋ถํฐ ์ฐ๋ ํํ์ธ๊ฐ๋ณด๋ค.
๊ทผ๋ฐ ์ด์ฐจํผ ์ฐ๋ฆฌ๊ฐ ์ฐ๋ ค๋ ๊ธฐ๋ฅ ๋ด์์๋ ์ constexpr ์ด ๋ถ์ ๋ฉค๋ฒ๋ฅผ ๋ค ๋นผ๋ ํฐ ๋ฌธ์ ๊ฐ ์๋ค๊ณ ํ๋คโฆ (jseo๋์ constexpr์ const ๋ก ํ๋ค๊ณ ํ๋ค.)
๊ทผ๋ฐ ๋ค์ ๋ณด๋ ์์ enable_if ์์์์ template <class T> typename std::enable_if<std::is_integral<T>::value, bool>::type is_odd (T i) {return bool(i%2);} ์ผ๋ก is_integral์ type์ ์ฌ์ฉํ๋ค. ๊ทธ๋ฌ๋ฉด ๋ฉค๋ฒํ์
value ๋ static const ๋ก ๋ฃ์ด์ผ ํ ๊ฒ ๊ฐ๋ค.
๊ทธ๋ผ ์ด์ integral_constant ๊ฐ ๋ค์๊ณผ ๊ฐ์ ํ์์ด ๋๋ค.
template <class T, T v> struct integral_constant {
static const T value = v;
typedef T value_type;
typedef integral_constant<T,v> type;
};
C++
๋ณต์ฌ
v ๋ก ์ด๊ธฐํ๋ static const T ํ์์ value, T ํ์์ value_type๊ณผ integral_constant ํ์์ type์ ๊ฐ์ง๋ค.
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
C++
๋ณต์ฌ
integral_constant ์ bool, true ๊ฐ ๋ค์ด๊ฐ ํด๋์ค๋ฅผ true_type์ผ๋ก, false๊ฐ ๋ค์ด๊ฐ ํด๋์ค๋ฅผ false_type ์ผ๋ก typedef ์์ผ๋๋ค.
์ดํ is_integral ์ ๋ค์ด์จ ์๋ฃํ์ด ์ ์ํ์ด๋ฉด true_type์ ์์๋ฐ๊ณ , ์๋๋ฉด false_type์ ์์๋ฐ๋๋ค.
๊ทธ๋ ๊ฒ ์๋์ฒ๋ผ ์จ๋ดค๋๋ฐ
template <>
struct is_integral<bool> : public true_type{};
template <>
struct is_integral<char> : public true_type {};
template <>
struct is_integral<unsigned char> : public true_type {};
.
.
.
C++
๋ณต์ฌ
๊ทธ๋ฐ๋ฐ ์ฌ๊ธฐ์ is_integral ์ ๋ ํผ๋ฐ์ค๋ฅผ ์ฝ์ด๋ณด๋ค๊ฐ, ์ด๋ฐ ๋ถ๋ถ์ด ์๋ค๋ ๊ฑธ ๋ฐ๊ฒฌํ๋ค.
All fundamental integral types, along with all their aliases (like those inย cstdint), are considered integral types by this class, including theirย const
ย andย volatile qualified variants.
const ์ volatile ์ญ์ ํฌํจํ๋ค๊ณ ์จ์๋ค! volatile ์ด ๋ญ์ง๋ ์ ๋ชจ๋ฅด์ง๋ง, ์ผ๋จ const๋ง ํ๋๋ผ๋ ๋ชจ๋ ์๋ฃํ์ const๋ฅผ ๋ถ์ธ ํจ์๋ฅผ ๋ ์จ์ผ ํ๋คโฆ ์ฌ์ง์ด const volatile ๋ ์๋ค๊ณ ํ๋, int ํ๋์ ์๋ฃํ์ int, const int, volatile int, const volatile int ์ด ๋ค ๊ฐ์ ํ
ํ๋ฆฟ ํด๋์ค๋ฅผ ๋ง๋ค์ด์ผ ํ๋ค.
์ด๋ฅผ ํด๊ฒฐํด์ฃผ๊ธฐ ์ํด, ์ฆ ์๋ฃํ ์์ const์ volatile์ ์์ ์ฃผ๊ธฐ ์ํด <type_traits> ํค๋์๋ remove_cv ๋ผ๋ ํด๋์ค ํ
ํ๋ฆฟ์ด ์กด์ฌํ๋ค. (๋งํฌ)
template <class T> struct remove_cv { typedef T type; };
template <class T> struct remove_cv<const T> { typedef T type; };
template <class T> struct remove_cv<volatile T> { typedef T type; };
template <class T> struct remove_cv<const volatile T> { typedef T type; };
C++
๋ณต์ฌ
์ด๋ ๊ฒ ๋ง๋ ํ remove_cv::type ์ ํธ์ถํ๋ฉด const ์ volatile์ด ๋ชจ๋ ์ญ์ ๋ type ๋ง ํธ์ถ๋๊ฒ ๋๋ค.
๋ค์ ์์ enable_if ์ฝ๋๋ฅผ ๋ณด๊ณ ์ ๋ฆฌํด๋ณด๋ฉด
template <class T>
typename std::enable_if<std::is_integral<T>::value, bool>::type
is_odd (T i) {return bool(i%2);}
C++
๋ณต์ฌ
is_integral ์ ๋ค์ด์จ ์๋ฃํ T ๊ฐ ์ ์๋ผ๋ฉด, SFINAE๊ฐ ์ ์ฉ๋ enable_if์ type ์ ํธ์ถํ ์ ์๊ธฐ ๋๋ฌธ์ ์ค๋ฅ๊ฐ ๋์ง ์๋๋ค. T๊ฐ ์ ์ํ์ด ์๋๋ผ๋ฉด, is_integral<T>::value ๊ฐ false๊ฐ ๋๊ณ , enable_if์ ์ฒซ๋ฒ์งธ ํ์
์ด false ์ด๋ฉด ๋ฉค๋ฒํ์
์ด ์๋ enable_if ๊ฐ ์์ฑ๋๊ธฐ ๋๋ฌธ์ ์ปดํ์ผ ์๋ฌ๊ฐ ๋๊ฒ ๋๋ค!
(์ฐ๋ฉด์๋ ์์ฒญ ํท๊ฐ๋ฆฐ๋ค.)
์ฌ๊ธฐ๊น์ง ์ ์ดํดํด์ ์ ๋ฆฌํ๋ฉด enable_if ์ is_integral ๋ฑ <type_traits> ํค๋๋ ๊ตฌํ๋!
๋ค์์ iterator ์ด๋ค.
allocator
<memory> ํค๋์ ์๋ ํด๋์ค์ด๋ค.
์๋ธ์ ํธ์ ๋ณด๋ฉด std::allocator ๋ฅผ ๋ฐ๋์ ์ฌ์ฉํ๋ผ๋ ๋ฌธ๊ตฌ๊ฐ ์๋ค. ์ด allocator๊ฐ ๋ญ์ง ์์๋ณด์.
vector์๋ ๊ณต๊ฐ์ ํ ๋นํด์ฃผ๋ ๋ฉค๋ฒ ํจ์๋ค์ด ์๋ค. resize ๋ฑ!
c++์์๋ ๊ณต๊ฐ ํ ๋น์ ํ ๋ new ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋๋ฐ, new ์ฐ์ฐ์๋
1.
๊ธฐ๋ณธ ์์ฑ์ ํ์
2.
๋ฉ๋ชจ๋ฆฌ ํ ๋น
3.
์์ ์ด๊ธฐํ
3๊ฐ์ง ์กฐ๊ฑด์ด ํ์ํ๋ค.
ํ์ง๋ง ์ด ์ธ๊ฐ์ง ์กฐ๊ฑด์ด ์ธํธ๋ก ๋ฐ๋ผ๋ค๋๊ฒ ๋์ด์ ์ปดํจํฐ ์์์ ์ฌ์ฉ๋์ด ์๋นํ ์ฆ๊ฐํ๋ค. ๊ทธ๋์ allocator๋ฅผ ์ด์ฉํ์ฌ ์ ๋จ๊ณ๋ค์ ๊ฐ๊ฐ ์ํ ๋ ์ฌ์ฉํ ์ ์๊ฒ ํ๋ค.
ํน์ ์ปจํ
์ด๋์ ์ต์ ํ๋ ์ ์ฐํ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๊ณผ ๊ด๋ฆฌ๋ฅผ ์ํด ๋๋ถ๋ถ์ ์ปจํ
์ด๋๋ค์ allocator๋ฅผ ์ฌ์ฉํ๋ค.
๊ทธ๋ฌ๋๊น, new๋ฅผ ์ฐ๋ฉด ์์ฑ์๊ฐ ํธ์ถ๋๋ฉฐ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํด์ฃผ๊ณ , ํด๋น ์๋ฃํ์ ๋ง๊ฒ ์ด๊ธฐํ๊น์ง ํด์ค๋ค. ์๋ฅผ๋ค์ด vector<int>๋ฅผ new๋ก ํ ๋นํ๋ฉด ๊ณต๊ฐ ํ ๋น์ ํ ํ ์ด๊ธฐํ๊ฐ ๋๊ณ , ์ดํ ์ฌ์ฉ์๊ฐ ์ฌ์ฉํ ๋ ํ๋ฒ ๋ ์ด๊ธฐํ ๋๋ค. ์ด๊ธฐํ๊ฐ ํ๋์ ์์๋ง๋ค ๋๋ฒ์ฉ ์งํ๋๊ธฐ ๋๋ฌธ์ ์์ ๋ญ๋น๊ฐ ์ฌํ๋ค๋ ๊ฒ์ด๋ค.
ํ์ง๋ง allocator ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋ชจ๋ฆฌ ํ ๋น๋ง ํ๊ณ ์ด๊ธฐํ๋ฅผ ํ์ง ์์ ์ ์๋ค. ๋ํ ๊ฐ์ฒด๊ฐ ๋ค์ด์๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ํด์ (deallocate) ํ์ง ์๊ณ (์ฌํ ๋น ํ์ง ์๊ณ ) ์ด๊ธฐ ์ํ๋ก ๋๋ฆด ์ ์๋ค. (๊ฐ์ฒด๋ง ์๋ฉธ ์ํฌ ์ ์๋ค.destroy)
์๋ค๋ ๊ตฌํ๋ std ์ฐ๋๊ฑฐ๋ผ ๊ตฌํ๊น์ง๋ ์ํด๋ ๋๋ค! ๊ทผ๋ฐ ์ดํด๋ ํ์คํ ํด์ผํ๋ค. ๋ฌด์จ ํจ์๊ฐ ์ด๋ป๊ฒ ๋์ํ๋์ง ์์์ผ ๋์ค์ ๋ฒกํฐ์ capacity, resize, size ๋ฑ์ ๊ตฌํํ ๋ ์ ์ธ ์ ์๋ค.
capacity โ allocate
size โ allocate + ์ด๊ธฐํ
(๋ง๋?)
pair
std::pair ๊ณผ std::make_pair ๋ฅผ ๊ตฌํํ๋ผ๊ณ ๋์ด์๋ค.
๋ค๋ฅธ์ ๋ค์ ์๋ธ์ ํธ์ std:: ๊ฐ ์๋ถ์ด์๋๋ฐ ์๋ ์ ๋ถ์ด์์๊น?
std::pair::pair ์ด ์๋๋ฐ ์๋๋ฌธ์ธ๊ฐ
equal
template <class InputIterator1, class InputIterator2>
bool equal (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2);
C++
๋ณต์ฌ
first1~last1 ๋ฒ์์ first2~ ์ ๋ฒ์์ ๊ฐ์๋งํผ์ ๋ฒ์ ์์๊ฐ ๋ชจ๋ ๊ฐ์ผ๋ฉด true ๋ฆฌํด!
lexicographical_compare
template <class InputIterator1, class InputIterator2>
bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2);
C++
๋ณต์ฌ
first1~last1 ๋ฒ์์ ์์๊ฐ first2~last2 ๋ฒ์์ ์์๋ณด๋ค ์์ผ๋ฉด true!
// lexicographical_compare example#include <iostream>// std::cout, std::boolalpha#include <algorithm>// std::lexicographical_compare#include <cctype>// std::tolower// a case-insensitive comparison function:
bool mycomp (char c1,char c2)
{return std::tolower(c1)<std::tolower(c2); }
int main () {
char foo[]="Apple";
char bar[]="apartment";
std::cout << std::boolalpha;
std::cout <<"Comparing foo and bar lexicographically (foo<bar):\n";
std::cout <<"Using default comparison (operator<): ";
std::cout << std::lexicographical_compare(foo,foo+5,bar,bar+9);
std::cout <<'\n';
std::cout <<"Using mycomp as comparison object: ";
std::cout << std::lexicographical_compare(foo,foo+5,bar,bar+9,mycomp);
std::cout <<'\n';
return 0;
}
C++
๋ณต์ฌ
vector ๊ตฌํ
๋ ํผ๋ฐ์ค์ ๋์์๋ ๊ฒ์ ํ ๋๋ก C++11 ๋ฒ์ ์ด์ ์ (98๋ฒ์ ์) ๋ฉค๋ฒ๋ค๊ณผ ํจ์๋ค์ ์์ฑํ๋ฉด ๋๋ค.
๋ฉค๋ฒ ๋ณ์๋ก๋
pointer _begin;
pointer _end;
pointer _end_cap;
allocator_type _alloc;
C++
๋ณต์ฌ
_end๋ ์ดํฐ๋ ์ดํฐ์ end๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ
_end_cap ์ capacity์ end๋ฅผ ๊ฐ๋ฆฌํจ๋ค. ์ฆ, capacity ์ฉ๋์ end ํฌ์ธํฐ ์ธ๋ฏ!












