U E D R , A S I H C RSS

AcceleratedC++/Chapter11

1. Chapter 11 Defining abstract data types

3 Student_info , 대, 되는 .
, 복, 대, 고려 다.
는 STL vector 만들 보면 만드는 방 다.

1.1. 11.1 The Vec class

를 결다. 로 그 객 로그램 보는 것 다.
~cpp 
//vector 
vector<Student_info> vs;
vector<double> v(100);

//vector가   는다.
vector<Student_info>::const_iterator b, e;
vector<Student_info>::size_type i = 0;

//vector보기 , size 및 index 
for(i = 0; i != vs.size(); ++1)
	cout<<vs[i].name();

//   반복
b=vs.begin();
e=vs.end();
것들 vector clone Vec 를 구드들다.

1.2. 11.2 Implementing the Vec class

1.2.1. 11.2.1 메모리

떤 방로 Vec를 구를 결는 것다.
template class다.
릿 뿐만 는 것 가능다.
~cpp 
template <class T> class Vec {
public:
	//interface
private:
	//implementation
};
 
begin, end, size 를 구 므로 , , 다.
size는 begin, end 를 기를 구는 것 가능므로 , 고 개 다.
~cpp 
template <class T> class Vec {
public:
	//interface
private:
	T* data;	//  
	T* limit;	//  나를 난 곳 
};
 
릿 며, 때 type parameter로 따라 다.
따라 Vec 되는부가 instiation 되기 다.

1.2.2. 11.2.2 (Constructor)

2가를 가능기 때문 2개는 다.
~cpp 
Vec<Student_info> vs;		// default constructor
Vec<Student_info> vs(100);	// Vec  기를 
				//  vector    다.
 

~cpp 
template <class T> class Vec {
public:
	Vec() { create(); }	// 느 구  기 때문 를 만들 다.
	explicit Vec(size_type n, const T& val = T()) { create(n, val); }
private:
	T* data;	//  
	T* limit;	//  나를 난 곳 
};
 
2 라메 T 다.
기본 2가 기 때문 다.
explicit
는 경. 될 가능 기 때문 는 방법다. (12.2 )
~cpp 
Vec<int> vi(100);	// Working. int 를  
Vec<int> vi = 100;	// not working. 로 Vec를 고 그 값 vi 다. refer 11.3.3
 

1.2.3. 11.2.3

const_iterator, iterator.
back_inserter(T)기를 변경 value_type, push_back . (value_type )
list 반복를 구는 경라면 ++ 노드로 결된 다 노드를 리로딩나, 는 배 를 가리므로 단 를 리는 것 만리는 근 반복를 구는 것 가능다.

value_type T가 되는 것다. 배는 size_type cstddef 는 size_t로 는 것 다.

~cpp 
template <class T> class Vec {
public:
	typedef T* iterator;
	typedef const T* const_iterator;
	typedef size_t size_type;
	typedef T value_type;

	Vec() { create(); }	// 느 구  기 때문 를 만들 다.
	explicit Vect(size_type n, const T& val = T()) { create(n, val); }
private:
	iterator data;	//  
	iterator limit;	//  나를 난 곳 
};
 
typedef를 고, 멤 변경.

1.2.4. 11.2.4 및 size


~cpp 
for (i = 0; i != vs.size(); ++)
	cout<<vs[i].name();
 
가능는 operator[], size() 다.

로딩(operator overload)
로드 operatorop 로 나난다. ~cpp []~cpp operator[]로 나 다.
로드 가 멤라면(friend ) , 는 두 로 나 다.
다면 되고 로드 다.
~cpp [] 로 size_type 고, 리로 value_type 다.
~cpp 
template <class T> class Vec {
public:
	typedef T* iterator;
	typedef const T* const_iterator;
	typedef size_t size_type;
	typedef T value_type;

	Vec() { create(); }	// 느 구  기 때문 를 만들 다.
	explicit Vect(size_type n, const T& val = T()) { create(n, val); }

	size_type size() const { return limit - data; }

	T& operator[](size_type i) { return data[i]; }
	const T& operator[](size_type i) const { return data[i]; };	//   때문다.
private:
	iterator data;	//  
	iterator limit;	//  나를 난 곳 
};
 
operator[] 2가 로딩 는 근
모든 멤 를 더 는다. 그것데, 그 객가 const나 const 가 2가는 것 가능기 때문 parameter specification 로딩 가능다.

1.2.5. 11.2.5 반복를 리


~cpp 
template <class T> class Vec {
public:
	typedef T* iterator;
	typedef const T* const_iterator;
	typedef size_t size_type;
	typedef T value_type;

	Vec() { create(); }	// 느 구  기 때문 를 만들 다.
	explicit Vect(size_type n, const T& val = T()) { create(n, val); }

	size_type size() const { return limit - data; }

	T& operator[](size_type i) { return data[i]; }
	const T& operator[](size_type i) const { return data[i]; };	//   때문다.

	//반복 들
	iterator begin() { return data; }
	const_iterator begin() const { return data; }

	iterator end() { return limit; }
	const_iterator end() const { return limit; }

private:
	iterator data;	//  
	iterator limit;	//  나를 난 곳 
};
 

1.3. 11.3 Copy control

리가 복, 대, 러는 방법 를 만들 게 된다.
렇게 로는 C++ 다. (그만 때 많 것들 고려되다.)

1.3.1. 11.3.1 Copy

implicit copy constructor
~cpp 
vector<int> vi;
double d;
d = median (vi);		// copy constructor work

string line;
vector<string> words = split(words);	// copy constructor work
 

explicit copy constructor
~cpp 
vector<Student_info> vs;
vector<Student_info> v2 = vs;	// copy constructor work (from vs to v2)
 

된 객 갖는 다.
를 복는 것 , 복 만든다는 미를 기 때문 매개변 는 것 다.
~cpp 
template <class T> class Vec {
public:
	Vec(const Vec& v);	// copy constructor
	// 과 같
};
 

게 됩다. 때 만 그 멤 라고 다면 문가 발게 된다.
멸되면 를 복 게되기 때문다.
따라 고 그 대 되는 값 런 문가 발 는다.
다른 로 create() 록 만든다.
~cpp 
template <class T> class Vec {
public:
	Vec(const Vec& v) { create(v.begin(), v.end() ); }	// copy constructor
	// 과 같
};
 

1.3.2. 11.3.2 대(Assignment)

(assignment operator)
operator=() 러가 const 를 대다.
C++ 기본 대 (left-hand side) 다.
~cpp 
template <class T> class Vec {
public:
	Vec& operator=(const Vec&);	// copy constructor
	// 과 같
};
 
로딩
는 복 달리 로 대다. 복 만들 기 때문 만, 대 는 기 기 때문 게 구 다.
기 대 (self-assignment) 고려가 다.
~cpp 
template<class T> Vec<T>& Vec<T>::operator=(const Vec& rhs) {
	// self-assignment check
	if (&rhs != this) {
		//lhs memory deallocation
		uncreate();
		create(rhs.begin(), rhs.end());
	{
	return *this;
}
 
  • Vec& 로
    릿 매개변 다.
    Vec::operator= 라, Vec<T>::operator= 가 다. 그러나 단 operator=가 Vec<T>라는 면 더 릿 를 반복 다.
  • this
    this는 멤 다. this는 멤 를 리다.
    this 드로 기 대 게 되므로 문가 발게 된다.
  • *this
    된다. 를 리면 객 scope를 벗나기 때문 바른 동다.

1.3.3. 11.3.3 대

C++ = 가 객 , 복 되기 때문 , 대 다.
= 가 되면 복 로, 된 객 되면 대 된다.
* 변
* , 매개변
*
*

2가
~cpp 
string url_ch = "~;/?:@=&$-_.+!*'(),";
string spaces(url_ch.size(), ' ');
 

, 대
~cpp 
vector<string> split(const string&);
vector<string> v;
v = split(line);	//   를 복
			// 를 대 가 v 
 

1.3.4. 11.3.4 (Destructor)

(destructor)
된 객는 delete 드로 는 메모리 공 는데, 객로 동 된 변다면 객 메모리 다.
로 반 며 객 명과 동 갖는 대 로 ~를 가 다.
~cpp 
template <class T> class Vec {
public:
	~Vec() { uncreate; }	// copy constructor
	// 과 같
};
 

1.3.5. 11.3.5 디 (Default operation)

, , 복 , 대 러가 기 된 방 기본 만든다.
(has-a)런 디 며, 기본(primitive type)는 값 다. 그러나 다른 며, 가 가리던 공 는다.
default constructor 를 는 객 를 가 다.
로는 명된 디 를 만드는 것 다.

1.3.6. 11.3.6 (rule of three)

본과 복 로 동다.
는 경 memory leak
다면 반드 바른 동다.

모든 복 대로 동 것들
T::T()
T::~T()
T::T(const T&)
T::operator=(const T&)

Rule of three : 복 , , 대 가 밀를 갖는 것 는 말다.

1.4. 11.4 Dynamic Vecs

push_back
push_back 기를 는데 보다 많 나더 만들 가능 가리는데 기로 다.
~cpp 
template<class T> class Vec {
public:
	size_type size() const { return avail - data; }
	iterator end() { return avail; }
	const_iterator end() const { return avail; }
	void push_back(const T& val) {
		if (avail == limit)
			grwo(0;
		unchecked_append(val);
	}
private:
	iterator data;
	iterator avail;
	iterator limit;
}

1.5. 11.5 Flexible memory management

Vec new, delete 드를 는다. 배 new로 게 되면 기본되는 객 되기 때문다. 리가 것과는 다른 방로 동다.
따라 리는 공 라브러리 는 관리다.

<memory> allocate<> STL
~cpp 
template<class T> class allocator {
pubilc:
	T* allocate(size_t);
	void deallocate(T*, size_t);
	void construct(T*, T);
	void destroy(T*);
};
allocate, deallocate 메모리 당, 다. 그러나 는다.
construct, destroy 당된 메모리 공 다. (공 allocate를 당된 공 다.)

allocate
~cpp 
template<class In, class For> For uninitialized_copy(In, In, For);
tempalte<class for, class T> void uninitialized_fill(For, For, const T&);
uninitialized_copy 3 다.
uninitialized_fill T로 다.

1.5.1. 11.5.1 Vec


~cpp 
//vec.h
#ifndef VEC_H
#define VEC_H

#include <algorithm>
#include <cstddef>
#include <memory>

using std::max;

template <class T> class Vec {
public:
	typedef T* iterator;
	typedef const T* const_iterator;
	typedef size_t size_type;
	typedef T value_type;
	typedef T& reference;
	typedef const T& const_reference;

	Vec() { create(); }
	explicit Vec(size_type n, const T& t = T()) { create(n, t); }

	Vec(const Vec& v) { create(v.begin(), v.end()); }
	Vec& operator=(const Vec&);	// as defined in 11.3.2/196
	~Vec() { uncreate(); }

	T& operator[](size_type i) { return data[i]; }
	const T& operator[](size_type i) const { return data[i]; }

	void push_back(const T& t) {
		if (avail == limit)
			grow();
		unchecked_append(t);
	}

	size_type size() const { return avail - data; }  // changed

	iterator begin() { return data; }
	const_iterator begin() const { return data; }

	iterator end() { return avail; }                 // changed
	const_iterator end() const { return avail; }     // changed
	void clear() { uncreate(); }
	bool empty() const { return data == avail; }

private:
	iterator data;	// first element in the `Vec'
	iterator avail;	// (one past) the last element in the `Vec'
	iterator limit;	// (one past) the allocated memory

	// facilities for memory allocation
	std::allocator<T> alloc;	// object to handle memory allocation

	// allocate and initialize the underlying array
	void create();
	void create(size_type, const T&);
	void create(const_iterator, const_iterator);

	// destroy the elements in the array and free the memory
	void uncreate();

	// support functions for `push_back'
	void grow();
	void unchecked_append(const T&);
};

template <class T> void Vec<T>::create()
{
	data = avail = limit = 0;
}

template <class T> void Vec<T>::create(size_type n, const T& val)
{
	data = alloc.allocate(n);
	limit = avail = data + n;
	std::uninitialized_fill(data, limit, val);
}

template <class T>
void Vec<T>::create(const_iterator i, const_iterator j)
{
	data = alloc.allocate(j - i);
	limit = avail = std::uninitialized_copy(i, j, data);
}

template <class T> void Vec<T>::uncreate()
{
	if (data) {
		// destroy (in reverse order) the elements that were constructed
		iterator it = avail;
		while (it != data)
			alloc.destroy(--it);

		// return all the space that was allocated
		alloc.deallocate(data, limit - data);
	}
	// reset pointers to indicate that the `Vec' is empty again
	data = limit = avail = 0;

}

template <class T> void Vec<T>::grow()
{
	// when growing, allocate twice as much space as currently in use
	size_type new_size = max(2 * (limit - data), ptrdiff_t(1));

	// allocate new space and copy existing elements to the new space
	iterator new_data = alloc.allocate(new_size);
	iterator new_avail = std::uninitialized_copy(data, avail, new_data);

	// return the old space
	uncreate();

	// reset pointers to point to the newly allocated space
	data = new_data;
	avail = new_avail;
	limit = data + new_size;
}

// assumes `avail' points at allocated, but uninitialized space
template <class T> void Vec<T>::unchecked_append(const T& val)
{
	alloc.construct(avail++, val);
}

template <class T>
Vec<T>& Vec<T>::operator=(const Vec& rhs)
{
	// check for self-assignment
	if (&rhs != this) {

		// free the array in the left-hand side
		uncreate();

		// copy elements from the right-hand to the left-hand side
		create(rhs.begin(), rhs.end());
	}
	return *this;
}

#endif
 
(class invariant)
  • data는 , 다면 0다.
  • data<=avail<=limit
  • 는 [data, avail)
  • 는 [avail, limit)

    ~cpp 
    template <class T> void Vec<T>::create()
    {
    	data = avail = limit = 0;	// 디  기 때문   낸다.
    }
    
    template <class T> void Vec<T>::create(size_type n, const T& val)
    {
    	data = alloc.allocate(n);	//  다.
    	limit = avail = data + n;	// 끝 가리다.  n개 기 때문 avail = limit 다.
    	std::uninitialized_fill(data, limit, val);	//    러개기 때문   다.
    }
    
    template <class T>
    void Vec<T>::create(const_iterator i, const_iterator j)
    {
    	data = alloc.allocate(j - i);
    	limit = avail = std::uninitialized_copy(i, j, data);
    }
     
    기 바람.

    ~cpp 
    template <class T> void Vec<T>::uncreate()
    {
    	if (data) {
    		// destroy (in reverse order) the elements that were constructed
    		iterator it = avail;
    		while (it != data)
    			alloc.destroy(--it);
    
    		// return all the space that was allocated
    		alloc.deallocate(data, limit - data);
    	}
    	// reset pointers to indicate that the `Vec' is empty again
    	data = limit = avail = 0;
    
    }
     
    는 내부 destroy 다.
    당된 공 deallocate 메모리 공 다.
    가 끝난 는 다 로 나내기 data, limit, avail 다.

    ~cpp 
    template <class T> void Vec<T>::grow()
    {
    	// when growing, allocate twice as much space as currently in use
    	size_type new_size = max(2 * (limit - data), ptrdiff_t(1));	// 는 벡는 1개만 다. 
    
    	// allocate new space and copy existing elements to the new space
    	iterator new_data = alloc.allocate(new_size);
    	iterator new_avail = std::uninitialized_copy(data, avail, new_data);
    
    	// return the old space
    	uncreate();
    
    	// reset pointers to point to the newly allocated space
    	data = new_data;
    	avail = new_avail;
    	limit = data + new_size;
    }
    
    // assumes `avail' points at allocated, but uninitialized space
    template <class T> void Vec<T>::unchecked_append(const T& val)
    {
    	alloc.construct(avail++, val);
    }
     
    grow 래 공 2배를 기 때문 된 push_back로 메모리 드를 다.
    unchecked_append()는 grow() 바로 뒤만 동록 구기 때문 다. (물론 문로 남겨 다른 로그래머가 듯)



Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:25
Processing time 0.1301 sec