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.0516 sec