Description
– Template & STL –
μ£Όμ μ¬ν
β ν΄λμ€ μ μΈ λ° μ μλ₯Ό main.cpp νμΌμ μμ±νλ κ²μ κΈμ§ν©λλ€. κ·Έ μΈμ μ μΈ λ° μ μμ μμΉμ λν μ μ½μ μμ΅λλ€.
β STL λ° shared_ptr μ μ μΈν λͺ¨λ C++ λ¬Έλ²μ΄ μ¬μ© κ°λ₯ν©λλ€.
β λ¬Έμ μμ μ 곡ν νμμ μ€μνκ³ κ° λ¬Έμ λ³ μΆκ°μ μΈ μΈλΆ 쑰건μ λ§μ‘±νμ¬μΌ ν©λλ€.
β λ¬Έμ μ λͺ
μλμ΄ μμ§ μλλΌλ μλ©Έμ(Destructor)μ κ°μ λ©λͺ¨λ¦¬ λμ λ°©μ§λ₯Ό μν΄ νμν λ©€λ²ν¨μλ νμμ μΌλ‘ ꡬνλμ΄μΌ ν©λλ€.
β λ¬Έμ μ‘°κ±΄μ΄ λ³΅μ‘ν©λλ€. λͺ¨λ λ¬Έμ μ μΈλΆ 쑰건μ κΌΌκΌΌν μ½μ΄ 보μκΈ° λ°λλλ€.
κ°μ
β μ μΆ κΈ°νμμ ν루(24 μκ°) λ¦μ λλ§λ€ 20%μ© κ°μ
β 1 μΌ(20%) , 2 μΌ(40%), β¦ 5 μΌ(100%)
β μ»΄νμΌμ΄ μ μμ μΌλ‘ μ΄λ£¨μ΄μ§μ§ μμ κ²½μ° 0 μ
μ μΆλ°©μ
κ³΅ν΅ μ±μ κΈ°μ€
1. νλ‘κ·Έλ¨ κΈ°λ₯
β νλ‘κ·Έλ¨μ΄ μꡬ μ¬νμ λͺ¨λ λ§μ‘±νλ©΄μ μ¬λ°λ‘ μ€νλλκ°?
2. νλ‘κ·Έλ¨ μ€κ³ λ° κ΅¬ν
β μꡬ μ¬νμ λ§μ‘±νκΈ° μν λ³μ λ° μκ³ λ¦¬μ¦ μ€κ³κ° μ λμλκ°?
β λ¬Έμ μμ μ μλ μΈλΆ 쑰건μ λͺ¨λ λ§μ‘±νμλκ°?
β μ€κ³λ λ΄μ©μ΄ μꡬλ μΈμ΄λ₯Ό μ΄μ©νμ¬ μ μ ν ꡬνλμλκ°?
3. νλ‘κ·Έλ¨ κ°λ
μ±
β νλ‘κ·Έλ¨μ΄ μ½κΈ° μ½κ³ μ΄ν΄νκΈ° μ½κ² μμ±λμλκ°?
β λ³μ λͺ
μ΄ λ¬΄μμ μλ―Ένλμ§ μ΄ν΄νκΈ° μ¬μ΄κ°?
β νλ‘κ·Έλ¨μ μμ€ μ½λλ₯Ό μ΄ν΄νκΈ° μ½λλ‘ μ£Όμμ μ λΆμλκ°?
4. λ³΄κ³ μ κ΅¬μ± λ° λ΄μ©, μμ
β λ³΄κ³ μλ μ μ ν λ΄μ©μΌλ‘ μ΄ν΄νκΈ° μ½κ³ 보기 μ’κ² μ μμ±λμλκ°?
β λ³΄κ³ μμ μμμ μ λ°λλκ°?
λ€λ₯Έ μ¬λμ μ½λλ μΈν°λ·μ μλ νλ‘κ·Έλ¨μ 볡μ¬(copy)νκ±°λ κ°λ¨ν μμ ν΄μ μ μΆνλ©΄ 무쑰건 βFβ νμ μ΄ λΆμ¬λ©λλ€. μ΄λ¬ν λΆμ νμκ° λ°κ²¬λλ©΄ νκ³Όμμ μ ν κΈ°μ€μ λ°λΌ μΆκ°μ μΈ λΆμ΄μ΅μ΄ μμ μ μμ΅λλ€.
Shared Pointer & Image Library
Objective
λ³Έ κ³Όμ μμλ Standard Template Library μμ μ 곡νλ κΈ°λ₯ μ€ νλμΈ shared_ptr μ μ§μ ꡬνν΄ λ³΄κ³ μ΄λ₯Ό μ΄μ©ν μμ μ²λ¦¬ ν΄λμ€λ₯Ό ꡬννμ¬ λ΄μΌλ‘μ¨ template κ³Ό STL μ λν μ΄ν΄λ₯Ό λμΈλ€.
λ¬Έμ 1 – SharedPtr
1.1. κ°μ
λ³Έ κ³Όμ μ 1 λ² λ¬Έμ λ shared_ptr μ κ°μνλ λ²μ μΈ SharedPtr ν
νλ¦Ώ ν΄λμ€λ₯Ό ꡬνν΄ λ³Έλ€. μ€λ§νΈ ν¬μΈν°(smart pointer)λ λ©λͺ¨λ¦¬ λμλ‘λΆν° νλ‘κ·Έλ¨μ 보νΈνκΈ° μν΄ C++μ Standard Template Library (STL)μμ μ 곡νλ κΈ°λ₯μ΄λ€. μ€λ§νΈ ν¬μΈν°λ ν¬μΈν°μ²λΌ λμνλ ν΄λμ€ ν
νλ¦ΏμΌλ‘ μ¬μ©μ΄ λλ λ©λͺ¨λ¦¬λ₯Ό μλμΌλ‘ λμ ν΄μ ν΄ μ£Όλ κ²μ΄ νΉμ§μ΄λ€. μΌλ°μ μΈ ν¬μΈν°λ₯Ό μ¬μ©ν λμλ new μ°μ°μλ₯Ό ν΅ν΄ μ€μ λ©λͺ¨λ¦¬λ₯Ό κ°λ¦¬ν€λλ‘ μ μΈνλ©° μ¬μ©μ΄ λλ ν¬μΈν°μ λν΄μλ delete μ°μ°μλ₯Ό ν΅ν΄ λ©λͺ¨λ¦¬λ₯Ό μλμΌλ‘ ν΄μ ν΄μΌ νλ€. μ΄λ¬ν λ°©μμ λ©λͺ¨λ¦¬ κ΄λ¦¬μ μ΄λ €μμ μ λ°νλ©° λ©λͺ¨λ¦¬ λμ νμμ΄λ λ©λͺ¨λ¦¬ λ¬Έμ λ‘ μΈν λ°νμ μλ¬λ₯Ό λΉλ²νκ² λ°μμν€κ² λλ€. μ€λ§νΈ ν¬μΈν°λ κ°λ°μκ° μΌμΌμ΄ delete λ₯Ό ν νμ μμ΄ λ©λͺ¨λ¦¬μ μ¬μ©μ΄ λλλ©΄ μλμΌλ‘ ν΄μ νλ λ°©μμΌλ‘ μ΄λ¬ν μ΄λ €μμ ν΄κ²°ν΄ μ€λ€.
C++μ STL μ μ¬λ¬ μ’
λ₯μ μ€λ§νΈ ν¬μΈν°λ₯Ό μ 곡νλλ° κ·Έ μ€ νλκ° shared_ptr μ΄λ€. shared_ptr μ memory ν€λνμΌμ include νλ©΄ μ¬μ© κ°λ₯νλ€. shared_ptr μ λμ ν λΉλ λ©λͺ¨λ¦¬ μμμ΄ νμ¬ λ μ΄μ μ¬μ©νμ§ μλ μμμΈμ§λ₯Ό νμ
νκΈ° μν΄ μ°Έμ‘° μΉ΄μ΄νΈ (reference count) λ°©μμ μ¬μ©νλ€. μ°Έμ‘° μΉ΄μ΄νΈλ₯Ό μ΄μ©ν μ€λ§νΈ ν¬μΈν° λμ λ°©λ²μ λν΄μλ κ°μ μλ£ β16. The string Class and the Standard Template Library (1)βκ³Ό λ³Έ λ¬Έμμ λΆλ‘μ μ°Έκ³ νκΈ° λ°λλ€.
1.2. κ³Όμ μꡬμ¬ν λ³Έ κ³Όμ μμ ꡬνν΄μΌ νλ SharedPtr μ κΈ°λ₯μ λ€μκ³Ό κ°λ€.
β« SharedPtr μμ±
βΌ SharedPtr μ template class λ‘ μ μ΄λ νλ μ΄μμ template parameter λ₯Ό λ°λλ€. λ§μ½
MyClass λΌλ ν΄λμ€μ κ°μ²΄λ₯Ό κ°λ¦¬ν€λ ν¬μΈν°λ₯Ό μμ±ν κ²½μ° λ€μκ³Ό κ°μ μ½λλ₯Ό μ΄μ©νμ¬ SharedPtr κ°μ²΄λ₯Ό μμ±ν μ μμ΄μΌ νλ€.
β SharedPtr<MyClass> ptr;
βΌ λν μλμ μμ μ½λμ κ°μ΄ SharedPtr κ°μ²΄λ₯Ό μμ±ν λ λμ ν λΉλ λ©λͺ¨λ¦¬ μμμ μ΄μ©νμ¬ SharedPtr κ°μ²΄λ₯Ό μ΄κΈ°νμν¬ μ μλ€.
β SharedPtr<MyClass> ptr(new MyClass());
βΌ SharedPtr κ°μ²΄λ₯Ό κ°μ νμ
μ κ°μ²΄λ₯Ό κ°λ¦¬ν€λ λ€λ₯Έ SharedPtr κ°μ²΄λ₯Ό μ΄μ©νμ¬ μ΄κΈ°νμν¬ μ μλ€.
β SharedPtr<MyClass> ptr(new MyClass());
SharedPtr<MyClass> ptr2(ptr);
β« SharedPtr λμ
μ°μ°
βΌ SharedPtr κ°μ²΄λ₯Ό λ€λ₯Έ SharedPtr κ°μ²΄μ λμ
ν μ μλ€. μ΄ κ²½μ° λ SharedPtr κ°μ²΄λ 곡ν΅λ λμ ν λΉλ λ©λͺ¨λ¦¬λ₯Ό κ°λ¦¬ν€κ² λλ€.
β SharedPtr<MyClass> ptr(new MyClass()); SharedPtr<MyClass> ptr2; ptr2 = ptr;
βΌ SharedPtr κ°μ²΄μ μλ‘μ΄ λμ ν λΉλ λ©λͺ¨λ¦¬ μ£Όμλ₯Ό λ°λ‘ λμ
ν μλ μλ€. λ§μ½ μ΄λ―Έ λ§λ€μ΄μ§ SharedPtr κ°μ²΄μ μλ‘κ² λμ ν λΉλ λ©λͺ¨λ¦¬ μ£Όμλ₯Ό λμ
νκΈ° μν΄μλ SharedPtr μ μμ±μλ₯Ό μ΄μ©ν΄ κ°μ²΄λ₯Ό μλ‘ λ§λ€κ³ λμ
ν΄μΌ νλ€.
β SharedPtr<MyClass> ptr; ptr = new MyClass(); // must raise a compile error!!
ptr = SharedPtr<MyClass>(ptr); // μ΄λ κ² ν΄μΌ ν¨.
β« SharedPtr κ°μ²΄μ μ΄μ©
βΌ SharedPtr κ°μ²΄λ μμ μ΄ κ°λ¦¬ν€λ κ°μ²΄λ₯Ό μ΄μ©ν μ μκ² νκΈ° μν΄ λ€μκ³Ό κ°μ λ κ°μ§ μ°μ°μλ₯Ό μ§μν΄μΌ νλ€ (*, ->). λ μ°μ°μ λͺ¨λ const λ²μ κ³Ό non-const λ²μ μ μ§μν΄μΌ νλ€.
β SharedPtr<MyClass> ptr(new MyClass); ptr->some_method(); (*ptr).some_method();
const SharedPtr<MyClass> const_ptr(new MyClass); const_ptr->some_const_method();
(*const_ptr).some_const_method();
βΌ SharedPtr κ°μ²΄λ νμμ μΌλ° ν¬μΈν°λ‘ λ³νλ μ μμ΄μΌ νλ€.
β SharedPtr<MyClass> ptr(new MyClass); MyClass* ptr2 = (MyClass*)ptr; const MyClass* ptr3 = (const MyClass*)ptr;
β« μλ λμ ν΄μ
βΌ SharedPtr κ°μ²΄λ‘ λ μ΄μ μ°Έμ‘°λμ§ μλ λ©λͺ¨λ¦¬ μμμ μλμΌλ‘ λμ ν΄μ λλ€. μλλ μ΄ λμμ λν μμ΄λ€.
β SharedPtr<MyClass> ptr(new MyClass(1)); // 첫λ²μ§Έ κ°μ²΄ λμ ν λΉ SharedPtr<MyClass> ptr2(ptr);
ptr = SharedPtr<MyClass>(new MyClass(2)); // λλ²μ§Έ κ°μ²΄ λμ ν λΉ. 첫λ²μ§Έ
κ°μ²΄λ ptr2 μμ κ°λ¦¬ν€κ³ μμΌλ―λ‘ κ³μ μ μ§λ¨
ptr2 = SharedPtr<MyClass>(new MyClass(3)); // μΈλ²μ§Έ κ°μ²΄ λμ ν λΉ. 첫λ²μ§Έ κ°μ²΄λ λ μ΄μ μ무 SharedPtr κ°μ²΄κ° κ°λ¦¬ν€μ§ μμΌλ―λ‘ μλμΌλ‘ λμ ν΄μ .
β« λμ ν λΉλ λ°°μ΄μ μ§μ
βΌ λ³Έ κ³Όμ μμ μμ±νλ SharedPtr κ°μ²΄λ λ°°μ΄μ λμ ν λΉλ μ§μνλ€. λ°°μ΄μ κ²½μ° λμ ν λΉμ μν΄ new μ delete λμ new[]μ delete[] μ°μ°μλ₯Ό μ¬μ©ν΄μΌ νλ€. μ΄λ₯Ό μν΄ SharedPtr ν
νλ¦Ώμ λλ²μ§Έ template parameter λ‘ deallocation μ μν ν¨μλ₯Ό μ
λ ₯λ°μμ μΌλ° κ°μ²΄μ λ°°μ΄μ λ©λͺ¨λ¦¬ ν΄μ λ₯Ό λ€λ₯΄κ² μ²λ¦¬νλ κ²μ μ§μνλ€. μ΄μ λν ꡬνμ λ³Έ κ³Όμ μ κ°μ΄ μ 곡λ skeleton code λ₯Ό μ°Έκ³ νκΈ° λ°λλ€.
β template<typename T> void ArrayDeallocator(T* ptr) { delete[] ptr; } SharedArray<int,ArrayDeallocator> ptr(new int[N]); // λλ²μ§Έ template
parameter λ‘ Array λ₯Ό ν΄μ νκΈ° μν ν¨μλ₯Ό νλΌλ―Έν°λ‘ μ§μ ν μ μλ€. λλ²μ§Έ template parameter κ° μ§μ λμ§ μλ κ²½μ°μλ μΌλ° κ°μ²΄λ₯Ό ν΄μ νκΈ° μν ν¨μκ° default parameter λ‘ μ§μ λλ€.
βΌ κ°μ΄ μ 곡λ skeleton code μμλ λ°°μ΄μ μν΄ λ€μκ³Ό κ°μ΄ μ μλ SharedArray λΌλ νμ
μ μ 곡νλ€.
β Template<typename T>
using SharedArray = SharedPtr<T,ArrayDeallocator<T> >;
βΌ λ³Έ κ³Όμ μμ μμ±ν΄μΌ νλ λΆλΆμ λμ ν λΉλ λ°°μ΄μ μ§μμ μν λ°°μ΄ μμ μ κ·Ό μ°μ°μμ΄λ€. ([ ])
β SharedArray<MyClass> ptr(new MyClass[10]); ptr[0].some_method();
const SharedArray<MyClass> ptr(new MyClass[10]); ptr[2].some_const_method();
β« κΈ°ν μꡬ μ¬ν
βΌ SharedPtr ν
νλ¦Ώ ν΄λμ€λ₯Ό κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ°μ΄λ μ λ€λ¦ νλ‘κ·Έλλ° κ΄μ λλ λ€λ₯Έ μΈ‘λ©΄μμ κ°μ ν μ μλ€λ©΄ μ΄λ€ μμΌλ‘ κ°μ ν μ μμμ§ λ³΄κ³ μμ κΈ°μ ν κ².
⼠첨λΆλ μ½λ μ€ SharedPtr_test.cpp μ SharedPtr ν΄λμ€ ν
νλ¦Ώμ΄ μ λλ‘ κ΅¬νλμλμ§ νμμ΄ μ€μ€λ‘ νμΈν μ μλ κ°λ¨ν μ½λλ€μ΄ μμ±λμ΄ μλ€. κ·Έλ¬λ λ³Έ μ½λλ μ°Έκ³ μ©μΌλ‘ μ 곡λ κ²μΌ λΏ λ³ΈμΈμ ν΄λμ€κ° μ¬λ¬κ°μ§ μν©μ κ³ λ €νμ¬ μ ꡬνλμλμ§ μ¬λΆλ μ§μ νμΈν΄ λ³Ό κ²μ μΆμ²νλ€. μ‘°κ΅λ ν΄λΉ sharedPtr_test.cpp κ° μλ λ 볡μ‘ν μν©μ ν
μ€νΈν΄ λ³Ό κ²μ΄λ€. μ°Έκ³ λ‘ SharedPtr ν΄λμ€ ν
νλ¦Ώμ΄ μ λλ‘ κ΅¬νλμλ€λ©΄ SharedPtr_test.cpp μ»΄νμΌ ν κ²°κ³Όλ λ€μκ³Ό κ°μμΌ νλ€.
test_SharedPtr()
MyClass object(100) created: 1
MyClass object(200) created: 2
============= ptr1: 200 ptr2: 100 ptr3: 100 =============
Dealloc Object
MyClass object(100) destroyed: 1
============= ptr1: 200 ptr2: 200 ptr3: 200 =============
MyClass object(300) created: 2
============= const_ptr: 300 const_ptr: 300 ============= pp: 200 Dealloc Object
MyClass object(300) destroyed: 1
Dealloc Object
MyClass object(200) destroyed: 0
test_SharedArray() ============= arr1[0]: 1 arr2[0]: 1 arr3[0]: 1 ============= ============= arr1[0]: 2 arr2[0]: 3 arr3[0]: 3 =============
Dealloc Array ============= arr1[0]: 2 arr2[0]: 2 arr3[0]: 2 =============
Dealloc Array
λ¬Έμ 2 – Image
2.1. κ°μ
λ³Έ κ³Όμ μ 2 λ² λ¬Έμ λ μ΄λ―Έμ§ μ²λ¦¬λ₯Ό μν κΈ°μ΄μ μΈ ν
νλ¦Ώ ν΄λμ€ μμ±μ΄λ€. μ΄λ―Έμ§λ μ»΄ν¨ν°μμ μΌλ°μ μΌλ‘ ν½μ
λ€μ 2 μ°¨μ λ°°μ΄λ‘ ννλλ©°, κ°κ°μ ν½μ
μλ λ°κΈ° μ 보λ μμμ λν μ λ³΄κ° μ μ₯λλ€. μ»¬λ¬ μ΄λ―Έμ§μ κ²½μ°μλ κ° ν½μ
μ Red, Green, Blue μ μΈκ°μ§ μμμ λν μ λ³΄κ° μ μ₯λλ©° κ·Έλ μ΄μ€μΌμΌ (grayscale) μ΄λ―Έμ§μ κ²½μ°μλ κ° ν½μ
μ λ°κΈ° μ λ³΄κ° μ μ₯λλ€. μΌλ°μ μΌλ‘ μμ μ 보λ λ°κΈ° μ 보λ 0 λΆν° 255 μ¬μ΄μ κ°μ κ°λ 8 bit unsigned integer νμ
μ μ΄μ©νμ¬ μ μ₯λλ€. λ°λΌμ κ·Έλ μ΄μ€μΌμΌ μ΄λ―Έμ§λ ν½μ
λ§λ€ 8 λΉνΈλ₯Ό μ¬μ©νλ©° μ»¬λ¬ μ΄λ―Έμ§λ ν½μ
μ κ° μμλ§λ€ 8 λΉνΈλ₯Ό μ¬μ©νμ¬ μ΄ 24 λΉνΈλ₯Ό μ¬μ©νλ€. μ΄λ―Έμ§μ ν½μ
λ§λ€ μμΉ μ°μ°μ μ μ©νλ κ²½μ°μλ 8 λΉνΈ μ μν νμ
μ κ°μ’
μμΉ μ°μ°μ μ ν©νμ§ μκΈ° λλ¬Έμ float μ΄λ double κ³Ό κ°μ λΆλμμμ μ€μν νμ
μ μ΄μ©νκΈ°λ νλ€.
λ³Έ κ³Όμ μμλ μ΄λ¬ν μ΄λ―Έμ§λ₯Ό λ€λ£¨κΈ° μν ν
νλ¦Ώ ν΄λμ€λ₯Ό μμ±νλ€. μ΄λ₯Ό ν΅ν΄ μ΄λ―Έμ§ νμΌ ν¬λ§· μ€ νλμΈ BMP νμΌλ‘λΆν° μ΄λ―Έμ§λ₯Ό μ½μ΄μ κ°λ¨ν μμ μ²λ¦¬λ₯Ό κ±°μΉ ν λμ¨ κ²°κ³Όλ₯Ό BMP νμΌλ‘ μ μ₯ν μ μλ νλ‘κ·Έλ¨μ μμ±ν΄ λ³Έλ€. λν BMP νμΌλ‘ μ½μ κ·Έλ¦Όμ λ¬Έμλ‘ λ³ννμ¬ μΆλ ₯νλ μ½λλ μμ±νλ€.
2.2. κ³Όμ μꡬμ¬ν λ³Έ κ³Όμ μμ ꡬνν΄μΌ νλ Image ν΄λμ€μ κΈ°λ₯μ λ€μκ³Ό κ°λ€.
β« Image μμ±
βΌ Image ν΄λμ€ ν
νλ¦Ώμ λ€μν ν½μ
νμ
μ μ§μνλ€. μ΄λ₯Ό μν΄ ν½μ
νμ
μ ν
νλ¦Ώ νλΌλ―Έν°λ‘ λ°λλ€. μμ μ½λλ λ€μκ³Ό κ°λ€.
β Image<uint8_t> img; // ν½μ
κ°μΌλ‘ 8 λΉνΈ unsigned integer νμ
μ μ¬μ©νλ κ·Έλ μ΄μ€μΌμΌ μ΄λ―Έμ§ κ°μ²΄ μμ±. uint8_t λ 8 λΉνΈ unsigned integer νμ
μΌλ‘ cstdint μ μ μΈλμ΄ μμ.
Image<float> imgf; // ν½μ
κ°μΌλ‘ float νμ
μ μ¬μ©νλ κ·Έλ μ΄μ€μΌμΌ μ΄λ―Έμ§ κ°μ²΄ μμ±
Image<RGB<uint8_t>> rgbimg; // ν½μ
κ°μΌλ‘ 8 λΉνΈ μ μνμ μ¬μ©νλ RGB image μμ±. RGB<> ν΄λμ€ ν
νλ¦Ώμ μ 곡λ skeleton code μ°Έκ³
β« Image ν΄λμ€ ν
νλ¦Ώμμ μ 곡νλ public interface
βΌ μμ±μ
β Image() // default constructor
β Image(size_t _width, size_t _height); // μ΄λ―Έμ§ κ°μ²΄ μμ± μ μ΄λ―Έμ§μ ν¬κΈ°λ§νΌ λ©λͺ¨λ¦¬ ν λΉ (_width x _height)
β Image(size_t _width, size_t _height, const PixelType& val); // μ΄λ―Έμ§ κ°μ²΄ μμ± μ μ΄λ―Έμ§μ ν¬κΈ°λ§νΌ λ©λͺ¨λ¦¬ ν λΉνκ³ val κ°μ μ΄μ©ν΄ λͺ¨λ ν½μ
κ° μ΄κΈ°ν
β Image(const Image<PixelType>& img); // copy constructor
βΌ μλ©Έμ
β ~Image(); // νΉλ³ν μ무 μΌλ νμ§ μμ.
βΌ μ°μ°μ
β λμ
μ°μ°μ (operator=)β κ°μ ν½μ
νμ
μ μ¬μ©νλ μ΄λ―Έμ§ κ°μ²΄λ‘λΆν° λμ
λ°λ μ°μ°μ. λ€μκ³Ό κ°μ λμμ μνν μ μμ΄μΌ ν¨.
β« const Image<float> a; Image<float> b; a = b;
β λ°°μ΄ μ‘μΈμ€ μ°μ°μ
β« μ΄λ λ°λ‘ ꡬνν νμ μμ. skeleton μ½λμ μ΄λ―Έ ꡬνλμ΄ μμΌλ μ°Έκ³ λ°λ
βΌ κΈ°ν λ©€λ² ν¨μ
β size_t width() const; // μ΄λ―Έμ§μ κ°λ‘ κΈΈμ΄ λ¦¬ν΄
β size_t height() const; // μ΄λ―Έμ§μ μΈλ‘ κΈΈμ΄ λ¦¬ν΄
β« κΈ°ν μꡬ μ¬ν
βΌ Image ν΄λμ€μ ν½μ
κ°μ μ μ₯νκΈ° μν 곡κ°μ λμ ν λΉμ μ΄μ©ν΄ ꡬνν΄μΌ νλ€. μ΄ λ λ¬Έμ 1 μμ ꡬνν SharedArray λ₯Ό μ΄μ©νμ¬ κ΅¬νν κ².
βΌ Image ν
νλ¦Ώ ν΄λμ€λ₯Ό κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ°μ΄λ μ λ€λ¦ νλ‘κ·Έλλ° κ΄μ λλ λ€λ₯Έ μΈ‘λ©΄μμ κ°μ ν μ μλ€λ©΄ μ΄λ€ μμΌλ‘ κ°μ ν μ μμμ§ λ³΄κ³ μμ κΈ°μ ν κ².
βΌ λ³Έ κ³Όμ μ κ°μ΄ μ 곡λ image_test.cpp λ Image ν΄λμ€ ν
νλ¦Ώμ μ΄μ©ν κ°λ¨ν μμ μ²λ¦¬ μμ μ½λλ‘ Image ν΄λμ€ ν
νλ¦Ώμ΄ μ ꡬνλμλ€λ©΄ λ€μκ³Ό κ°μ λ΄μ©μ μΆλ ₯νκ² λλ€. μ΄ μΈμλ λ³ΈμΈμ΄ ꡬννκ³ μΆμ λ€λ₯Έ μμ μ²λ¦¬ μμ λ μΆκ° κΈ°λ₯μ΄ μλ€λ©΄ ꡬννκ³ μ΄λ₯Ό λ³΄κ³ μμ κΈ°μ ν κ². Image ν΄λμ€ μμ μ‘°κ΅λ image_test.cpp μ μ μλ μ½λ μΈμλ λ 볡μ‘ν μν©μ ν
μ€νΈν΄ λ³Ό κ²μ΄λ€.
Dealloc Array
Dealloc Array
Dealloc Array
Dealloc Array
Dealloc Array
2. ASCII conversion
2.1. Grayscale conversion
Dealloc Array
2.2. Downsampling
Dealloc Array
Dealloc Array
Dealloc Array
2.3. ASCII art drawing
OOOOOOOOOOOOOOOOOOOOOOOOOpEo3Cii}t5SVOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOde())JJJJJJJ7(uSpOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOx7)))))JJJJJJJ777iZhOOOOOOOO
OOOOOOOOOOOOOOOOOOOOptv)))))))JJJJJJJ77777Ij6pOd]p
OOOOOOOOOOOOOOOOOOOp3vvv)))))))JJJJJJJ77J7777(((CO
OOOOOOOOOOOOOOOOOOOevvv)v))))))))JJJJJJJ7777777(ZO
OOOOOOOOOOOOOOOOOOkvvvvvv)))))))))JJJJJJJJ77777|9O
OOOOOOOOOOOOOOOOOOfTvvvvvv)v)))))))JJJJJJJJ7777yOO
OwOOOOOOOOOOOOOOO6TTTvvvvvvv))))))))JJJJJJJJ77YOOO
OFfwOOOOOOOOOOOOp5TTTvvvvvvv))v)))))))JJJJJJIqVdOO
OZ(7[4OOOOOOOOOOVLTTTTvvvvvvT+v))))))))Jfnj6dyidOO
OVI77F2OOOOOOOOOOT=zTTTTvvT!^Lv)))))))}Z[3}FJJfOOO
Op3JJJ)19OOOOOOOOSk1vTTTTzCjpy)vv)))))7))JJJJJEOOO
Olv7JJ))J5pOOOOOOSz^5TTTT3E/vV[)v)))))))))JJJeOOOO
Ov)JJ))vvTFSOOOOOu“[)JTTo/.`xjvvvvv)))))))|yOOOOO
Oy()))vvTTTL19OOd97vtfff7}I`=htvvvvv))))Jt2pOOOOOO
OOpP))vvTLLLsv5pqFo3fffffTuShovvvvvvv))eSaZ3dOOOOO
OOOOe(vTTLLsLLL|ZLvifFF7FJTTTTTTvvvvvv}|)))ZOOOOOO
OOOOpSSFLLssLLLLsLLL|+`.FTTTTTTTTvvvvvvv))fpOOOOOO
OOOOOOOpYJLLLLLLLLsLTFJi7LTTTTTTTvvvvvvvvf4OOOOOOO
OOOOOOOOOVZ)LLLLLLLsLLTLLLTTTTTTTTTTvvvvnpOOOOOOOO
OOOOOOOOOOOVx|LLLLLLLLLLLLLLTTTTTTTTvv}wOOOOOOOOOO
OOOOOOOOOOOOOOwl)sLLJLs))LLLTTTTTTT(uwOOOOOOOOOOOO
OOOOOOOOOOOOOOOOpFsl{exfCfLLLLLsJo9OOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOI)CC{LtJ3LLLLLLLsiqOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOO[LeCLFLl3LLLLLsLLLL5OOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOYsYTJ5L}1LsLLL7sLsLL[OOOOOOOOOOOO
OOOOOOOOOOOOOOOOOETIlLs7f3LLLLLPIsLTTvYOOOOOOOOOOO
OOOOOOOOOOOOOOOOO9TITlIfs1LLsss9p3LTvvJkOOOOOOOOOO
OOOOOOOOOOOOOOOOOOv{I3f337LLLLLdOpIvv)JuOOOOOOOOOO
OOOOOOOOOOOOOOOOOO{ssLsLLLssLLs4OOV{)J(EOOOOOOOOOO
OOOOOOOOOOOOOOOOOOuLsLsLLLLLLLsVOOO6(C3EOOOOOOOOOO
OOOOOOOOOOOOOOOOOOjLLLLLLLLLLLspOOOO]o5OOOOOOOOOOO
OOOOOOOOOOOOOOOOOOPsLLLLLLLLLLLpOOOOO4OOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOl)iultt|ss)JiypOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOk|fOOOO4}LJ(J7fVOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOC|VOOOOpnv((3pOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOO5|qOOOOOOSC(apOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOVh2(e22]]2ESx|oOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOE5ZlI[55555555CF5OOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOphqE]ayjjjya2Sk9OOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOpppOOOOOOOOOOOOOOOOOOOOOOO
Dealloc Array
Dealloc Array
Dealloc Array
Dealloc Array Dealloc Array
λΆλ‘ β μ°Έμ‘° μΉ΄μ΄ν
(Reference counting)
STL μ shared_ptr μ μ°Έμ‘° μΉ΄μ΄ν
μ μ΄μ©νμ¬ λμ ν λΉλ κ°μ²΄λ₯Ό κ΄λ¦¬νλ€. λμ μ리λ λ€μκ³Ό κ°λ€. μλμ κ°μ μ½λκ° μλ€κ³ νμ.
1: {
2: string* tmp = new string(“Some String”);
3: shared_ptr<string> ptr1(tmp);
4: {
5: shared_ptr<string> ptr2;
6: ptr2 = ptr1;
7: }
8: }
μμ μ½λμμ 2 λ² λΌμΈκΉμ§ μ€νλμλ€κ³ νμ. μ΄ λ tmp κ° κ°λ¦¬ν€κ³ μλ string κ°μ²΄λ heap μμμ λμ ν λΉλλ€. μ΄λ heap μμμ λ€μκ³Ό κ°μ΄ λνλΌ μ μλ€.
3 λ² λΌμΈμ΄ μ€νλλ©΄μ shared_ptr μ κ°μ²΄μΈ ptr1 μ΄ μμ±λκ³ ptr1 μ tmp κ° κ°λ¦¬ν€λ string κ°μ²΄λ₯Ό κ°λ¦¬ν€κ² λλ€. μ΄ λ λ§μ°¬κ°μ§λ‘ heap μμμ μ΄ object λ₯Ό λͺ κ°μ shared_ptr κ°μ²΄κ° κ°λ¦¬ν€κ³ μλμ§λ₯Ό μΉ΄μ΄ν νλ λ³μ (counter)λ₯Ό μμ±νλ€. κ·Έλ¦¬κ³ νμ¬ ptr1 μ΄ object λ₯Ό κ°λ¦¬ν€κ³ μμΌλ―λ‘ μΉ΄μ΄ν λ³μλ₯Ό 1 λ‘ μ΄κΈ°ννλ€. μ΄ μνλ₯Ό λμννλ©΄ λ€μκ³Ό κ°λ€.
5 λ² λΌμΈμ΄ μ€νλλ©΄μ shared_ptr κ°μ²΄μΈ ptr2 κ° μμ±λλ€. κ·Έλ¦¬κ³ 6 λ² λΌμΈμ΄ μ€νλλ©΄μ ptr2 μ ptr1 μ κ°μ λμ νλ€. μ΄ λμ μ°μ°μμ ptr2 λ ptr1 μ΄ κ°λ¦¬ν€κ³ μλ object λ₯Ό κ°μ΄ κ°λ¦¬ν€λ©΄μ λμμ counter λ κ°λ¦¬ν€κ² λλ€. λν μ΄ λ counter μ κ°μ 1 μ¦κ°μν¨λ€.
7 λ² λΌμΈμ΄ μ€νλλ©΄μ μμͺ½ λΈλ‘μ λ²μ΄λκ² λκ³ λΈλ‘ λ΄μ local variable μΈ ptr2 κ°μ²΄κ° μλ©Έλλ€. ptr2 κ°μ²΄κ° μλ©Έλλ©΄μ counter λ₯Ό 1 κ°μμν¨λ€.
λ§μ§λ§μΌλ‘ 8 λ² λΌμΈμ΄ μ€νλλ©΄μ λ°κΉ₯μͺ½ λΈλ‘μ λ²μ΄λκ² λκ³ λΈλ‘ λ΄μ local variable μΈ ptr1 κ°μ²΄κ° μλ©Έλλ€. ptr1 κ°μ²΄κ° μλ©Έλλ©΄μ λ€μ counter λ₯Ό 1 κ°μμν¨λ€. κ·Έλ¦¬κ³ κ°μλ counter κ° 0 μ΄ λμλ€λ©΄ ptr1 κ°μ²΄μ μλ©Έμλ counter μ object λ₯Ό λͺ¨λ λμ ν΄μ νλ€.




Reviews
There are no reviews yet.