U E D R , A S I H C RSS

AcceleratedC++/Chapter10

1. Chapter 10 Managing memory and low-level data structures

���� vector, string STL�� �������� ����하�� �������� 통해� �����������.
���� �� �������� �� ������.
low-level���� 표� ���� �������� STL����� ������, 하�������� ���� ���� ���� 흡��하�� ��������.

1.1. 10.1 Pointers and arrays

����(array) vector ����하�� vector�����하�� ����.
����(pointer) �� �� ���������, ������ ������ ���� �� ���, ���� ���� ���� ��������.

1.1.1. 10.1.1 포���

����(pointer) �� ������� ��
�� ������(address operator) ������ �� ����한��.
������ ������(dereference operator) ���� p ��키�� ���� ����한��.
��통 ������������ ����화��키�� ���� ����하�� �� 0����. 흔�� �� � 포����(null pointer)�� ����.
����� ������ ������ type-name * �� ����한��.
~cpp  int *p;    // *p�� int ��� ������.
int* p;    // p�� int*�� �� ����하�� 표�����. C��파���� * �� �� ����하�� ������ ������ 2�� 표� ����.
 

���� ����
~cpp  int* p, q;      // p�� int* �� 포���� 형��, q�� int 형 ��킨��.
 

����
~cpp 
//pointer_example.cpp
#include <iostream>

using std::cout;
using std::endl;

int main()
{
	int x = 5;

	// `p' points to `x'
	int* p = &x;
	cout << "x = " << x << endl;

	// change the value of `x' through `p'
	*p = 6;
	cout << "x = " << x << endl;
	return 0;
}
 

1.1.2. 10.1.2 함���� 한 포���

� 함��포��� ������ ������ ���� ���� ������ ������������. ������ C, C++������ �������� 테크������ ��히�� � ���������?

6.2.2 �������� 함���� ������ 함�� ��하�� �� ������.
���� ������ ���� �� �� 함���� �� ��하��, ��행하�� �� ������ ����.
���� 한 포��� ������ ��하�� ������ ������ 통해�� 포����� ���� ������ ������ 함���� ���� ���� ��하��.
~cpp 
int (*fp)(int);
		
int next(int n)
{
	return n+1;
}
fp = &next;
fp = next;		// ������ 2 표� ���� next함���� 포��� fp�� ��하�� ���� ��하��.
		
int i = 0;
int n =1;
		
i = (*fp)(i);
i = fp(i); 		// ������ �� 2���� 표����� ��효한 표�������. 
 
6.2.2���� ����하�� write_ayalysis�� ������ ��펴���� 하��.
~cpp 
void write_analysis(std::ostream& out, const std::string& name,
                    double analysis(const std::vector<Student_info>&),
                    const std::vector<Student_info>& did,
                    const std::vector<Student_info>& didnt);		
 
������� 3���� ������ ��펴���� 하��.
~cpp double analysis(const std::vector<Student_info>&)
 

C++ ���� �� 표��� ������ ��하�� �� ������ ���� ���� ���� 형 ��킨��.
~cpp double (*analysis)(const std::vector<Student_info>&)
 
������ ������ ����하�� 함���� 표����� �������� 함�� ����키�� ���� ��한 ������.
�� ����한 ���� 형 ���� ����형���� ������ ������. ��� 함�� ����하�� �������� �������� 포��� �����할 ��� ����.
~cpp 
//������ 표�
typedef double (*analysis_fp)(const vector<Student_info>&);
analysis_fp get_analysis_ptr();		// �� ���� ����하�� ���� ��합����.
 
//������ �� 표�
double (*get_analysis_ptr()) (const vector<Student_info>&);
 
������ ������� ��������� ��한 ���� get_analysis_ptr()��하�� �� �� ������하��� const vector<Student_info>& ������ ���� double 형 ����하�� 함�� ���� ��������.
������ ���������� ���� 2������ ���� ���� ������하���� �� �� �� ��������.
�������� ���� ���� �������� ��������. (Addition 1����합����.)
�� 포����� 흔히 ���� 함���� ������ ��������.
~cpp 
template<class In, class Pred>
In find_if(In begin, In end, Pred f) {
	while (begin != end && if f(*begin))		//����� Pred�� Pred(*begin)�� �� ���� ���� ����� ��합����.
		++begin;
	return begin;
}

bool is_negative(int n) {
	return n<0;
}

vector<int>::iterator i = find_if(v.begin(), v.end(), is_negative);		// &is_negative  ����하 ���� ������ ����형�� 포����형�� ���� ����. 
 

1.1.3. 10.1.3 ����

����(Array)
�������� �� ���������� ��� 하�� ������ ������ �������� ������ �����������. �� ���� ������ ���� ��파 ���� �� �� ��������.
� C99 표���� ���� 크�� �������� 할�� �� ��합����.
�������� ����� ���� ������ ������ 크�� ������� size_type �� ������ ��������. ���� C Standard Definition ��하 <cstddef> �� size_t�� ���� unsigned ����� ������ 크�� ����해����.
~cpp 
const size_t NDim = 3;
const size_t PTriangle = 3;
double coords[NDim];
double coords1[PTriangle];
 
���� �� 표� const�� ���� ���� �� ����화하�� ������ ��파���� �� 크�� �� �� ����. ��한 ���� �� ������ ������ coord�� �� ��할 �� ���� ������ ������ ����한��.
��한 ������ �� �� ������ ������ ������ ���� ��키�� 포����형�� ����화 ���� ������ ��������� ��한 �� ���� ����.
~cpp 
*coord = 1.5;			//coord ������ ������ ���� 1.5�� 할��. ��, coord[1] = 1.5;  ��한 표��� ����. 
 

1.1.4. 10.1.4 포������� ����

��� ���� ����������. �������������� 포��� ����한 �� ����해 �� ����.
coord coord�� 0���� ���� ��키��
coord+1 1���� ����
coord+2 2���� ����
coord+3 3���� ����. �������� ��효하 ���� 포����� �������� ��효한 포�������.
coord+4 ��효하 �����
coord-1 -1���� ����. �������� ��효하 ���� 포����� �������� ��효한 포�������.

~cpp 
vector<double> v;
copy(coords, coords + NDim, back_inserter(v)); 
 
coord+NDim coord�� ���� ������� 1�� �� �� �� ��키�� ������ 표� ��효한 표�������.
p, q �� ������ �������� 포�����, p-q�� p q������ ���� �������� �� ������� ���� ����.
������ �� ���� ������� �� ��� ����템 ���� �� �� ����, ���� ������� ���� ���� ������ <cstddef>���� ptrdiff_t�� �� 통해�� ������ 형 ����한��.
a n�� ������ ��������, a+i ��효하�� �� 0<=i<=n, a+i가 a의 요소를 가리키기 위한 필요 충분 조건은, 0<=i

1.1.5. 10.1.5 ��(Indexing)

����� �� �� ����������. ��� vector ���� �� ���� ����.
���� p ������ m���� ���� ��킨���� pn m+n���� ���� ��킨��. (������ �� �� ���� ��킨��.)

1.1.6. 10.1.6 ���� ����화

STL�� ��테������ ���� ���� ����화�� �������� ���� ����한��.
�� ���� ����함���� ������ ����화���� ������ �������� ������ 크�� ����� �� ����.

~cpp 
const int month_length[] = {
	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};		// �� ���� 
 
������ ������� month_length�� ������ 크�� �������� ����� �������� ������ ����. ���� ��파�� ����화�� ���� ������������.

1.2. 10.2 String literals revisited

"hello" �� ������ ����� ���� const char�� ��������. �� ���� ������ ����하�� ������ ������ 한�� �� �� ���� 포함하����, ���� ������ ������� �� ������� '\0' �� ����� ���� ��������.
~cpp 
		const char hello[] = { 'h', 'e', 'l', 'l', 'o', '\0' };		//�� 표� const char hello[] = "hello"; ��한 표�����. 
 
<cstring> strlen
~cpp 
size_t strlen(const char* p) {		// ������ ��� �� ���� 
	size_t size = 0;
	while (*p++ != '\0')
		++size;
	return size;
}
 
������ ���� ��탕�� string�������� ���� �� ����화 행하�� ���� ��하��.
~cpp 
string s(hello);
string s("hello");
string s(hello, hello+strlen(hello));
 
������ 3 표� ���� ����.

1.3. 10.3 Initializing arrays of character pointers

~cpp 
//letter_grade.cpp
#include <cstddef>
#include <string>

using std::string;

string letter_grade(double grade)
{
	// range posts for numeric grades
	static const double numbers[] = {
		97, 94, 90, 87, 84, 80, 77, 74, 70, 60, 0
	};

	// names for the letter grades
	static const char* const letters[] = {
		"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D", "F"
	};

	// compute the number of grades given the size of the array
	// and the size of a single element
	static const size_t ngrades = sizeof(numbers)/sizeof(*numbers);

	// given a numeric grade, find and return the associated letter grade
	for (size_t i = 0; i < ngrades; ++i) {
		if (grade >= numbers[i])
			return letters[i];
	}

	return "?\?\?";
}
static 키���� ����하�������� ����화�� �������� �� ����한��.
���� ���� 화하 ���� ������ const 키���� ����
array_pointer / sizeof(*array_pointer) ����하�� array �� ���� ������ �� �� �� ����. ������ 표����� �� ��힌��.

1.4. 10.4 Arguments to main

�� ���������� ��������� ��행���� ������������ �� ���� ��하��. ���� main함�� int, char** �� 2�� ���� �� ��������.
~cpp 
#include <iostream>

using std::cout;
using std::endl;

int main(int argc, char** argv)
{
	// if there are command-line arguments, write them
	if (argc > 1) {
		cout << argv[1];               // write the first argument

		// write each remaining argument with a space before it
		for (int i = 2; i != argc; ++i)
			cout << " " << argv[i];    // `argv[i]' is a `char*'
	}
	cout << endl;
	return 0;
}
char**�� (char*) ������ ���� �������� ����하�� ����. �� ������ ����� ������ ���� ��������.
����� ���� ��하�� ����.

1.5. 10.5 Reading and writing files

1.5.1. 10.5.1 표 ���� ��트

C++���� ������ 표 ���� ��트 ������ ���� ��트 통해� ��������� �� ������. �� ��������� ��행���� ������ ��태���� ����하�� ���� ��하��.
�� ������������ ���� 2�� ���� ��하�� ����하�� ���� ����한��.
cerr �� ����하 ����, �������� ������. ������.
clog �� ����하��, �������� ����한��.
���� ���� ���� ���하�� ���� �� ���� ����하�� �� . C������ 하�� ��� C ���� ���� ���� ��트�� ���� ���한 ���� ����.

1.5.2. 10.5.2 ���� ���� 파 ���� 파 ������

�� ������ � iostream �������� ���� ������킨 ofstream, ifstream������ ����한��. ���� 클������ <fstream>�� ���� ��������.
<fstream>������하�� 파 �� char* 형����, string ����� ����. ���� fstream library ������ ���� STL�� ������ ���� ���� ���� ��������.
~cpp 
//copy_file.cpp
#include <fstream>
#include <string>

using std::endl;
using std::getline;
using std::ifstream;
using std::ofstream;
using std::string;

int main()
{
	ifstream infile("in");
	ofstream outfile("out");

	string s;

	while (getline(infile, s))
		outfile << s << endl;
	return 0;
}
 
���� 파�� ���� string 형 ����하�� ������ string형�� ������ c_str() ����해����하�� ���� ��하��.
~cpp 
string file("out");
ifstream infile(file.c_str());
 
~cpp 
//concat_files.cpp
#include <iostream>
#include <fstream>
#include <string>

using std::cerr;
using std::cout;
using std::endl;
using std::getline;
using std::ifstream;
using std::string;

int main(int argc, char **argv)
{
	int fail_count = 0;
	// for each file in the input list
	for (int i = 1; i < argc; ++i) {
		ifstream in(argv[i]);

		// if it exists, write its contents, otherwise generate an error message
		if (in) {
			string s;
			while (getline(in, s))
				cout << s << endl;
		} else {
			cerr << "cannot open file " << argv[i] << endl;
			++fail_count;
		}
	}
	return fail_count;
}
 
����한 ����������� �� ����함. 한�� ������.

1.6. 10.6 Three kinds of memory management

~cpp 
int* invalid_pointer() {
	int x;
	return &x;		//함���� ������ x������ ������� �� ��효한 ���������� �� ��킨��. 
}

~cpp 
int* pointer_to_static() {
	static int x;
	return &x;		//��효하��. static �� 함�� ������하�� 함�� ���� ��행���� ������ ���� 한�� 할���� �� 함

�� ������������ ���� ��������� �������� 해������.  ������������ �� static �� ����하�� �������� ��효하��.
}
���������� �� ����, ���� ����형, 포����형�� ����하�� �� ����� ��효한 ��������.
����하�� ������ static���� ����해��.
���������� 할 ���� �� ����하�� ������ ���� ��행���� ���� 할���� ��������� ��행���� ���� �� ��효한 ��태�� ������.
���������� 할 new, delete 키���� ����해� ��������� ��하�� ������ ���������� 할�� 해���� ����.

1.6.1. 10.6.1 ���� 할 �� 해��

������ ��� T�� ��하��, ������ ���� ������ 할�� ��
~cpp 
new T(args)
 
�� 표� ����하�� ����.
������ 할�� ������ ���������� ������ 해��해 ������.

1.6.2. 10.6.2 ���� 할 �� 해��

������ ��� T, �� ������ ���� n�� ��하��, ������ ���� ������ 할�� ��
~cpp 
new T[n]
 
�� 표� ����하�� ����.
������ ������ 할������ ������� �� T* 형�� �� ������ ������ ������ 포��� ����.
������ ������ ��폴트 ������ ����화����.
������ ���� ������ ������ delete[]������ 해�� ��하��.
~cpp 
char* duplicate_chars(const char* p) {
	size_t length = strlen(p) + 1;
	char* result = new char[length];
	
	copy(p, p+length, result);
	return result; 
 
��한 �������� ������

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