티스토리 뷰

*자료형

자료형 

 크기

특징 

short

2byte 

-2^15 ~ 2^15-1

unsigned short

2byte 

 0 ~ 2^16-1

int

4byte 

 -2^31 ~ 2^31-1

unsigned int

4byte 

 0 ~ 2^32

 float

4byte

 float f = 0.5f ; 와 같이 뒤에 f를 써준다.

 double

8byte 

 

 char

1byte 

 

 wchar_t

2byte 

wchar_t w = L'A'; 와 같이 앞에 L을 써준다. 

(char) 0 = 48, (char) A = 65, (char) a = 97


* 0.00123 -> 1.23 * 10^-3 -> 1.23 | -3 과 같은 부동소수점 형태로 저장이 되며,

정밀도를 잃어버릴 수 있는 단점이 있습니다.


* 0이 아닌 모든 값 = true

(bool)99 => true, !99 => false


(int)3.94 => 3


*비트연산자를 통한 두 수의 값 바꾸기

int a, b;

a = a ^ b;

b = a ^ b;

a = a ^ b;


*구조체(struct)의 대입

1
2
3
4
5
6
7
8
struct Student {
    int number;
};
 
int main(){
    Student s1 { 10 }; 구조체의 초기화
    Student s2 = s1;
}
cs

*배열과 포인터
1
2
3
4
5
int main(){
    int arr[10];

배열의 원소를 가리키는 포인터
    int *ptr1 = arr;//동일 int *ptr1 = &arr[0];

배열의 전체를 포인터 변수로
    int (*ptr2)[10= &arr;
}
cs


*void형 포인터

1
2
3
4
5
6
int main(){
    int value = 10;
    void * ptr;
    ptr = &value;
    cout << *(int *)ptr;
}
cs

void형 포인터를 사용할 때, 가리키고 있는 자료형을 알고 있어야합니다.


*const

-a는 const로 지정되어 있기 때문에 새로운 값이 들어갈 수 없습니다.
1
2
3
4
int main(){
    const int a = 123;
    = 456; 에러
}
cs

-포인터가 가리키는 변수가 const

1
2
3
4
5
6
7
int main(){
    int a = 123;
    int b = 456;
    const int * ptr = &a;
    *ptr = 123; 에러
    ptr = &b;
}
cs

-포인터가 const

1
2
3
4
5
6
7
int main(){
    int a = 123;
    int b = 456;
    int * const ptr = &a;
    *ptr = 123;
    ptr = &b; 에러
}
cs
-포인터와 포인터가 가리키는 변수 둘다 const

1
2
3
4
5
6
7
8
int main(){
    int a = 123;
    int b = 456;
    const int * const ptr = &a;
    *ptr = 123; 에러
    ptr = &b; 에러
    *(int *)ptr = 456; 정상 작동
}
cs



*열거형(enum)

1
2
3
4
5
6
7
8
enum Color {Red, Blue, Green};
 
int main(){
    Color c1 = Red;
    c1 = Red + Blue; 에러 : 열거형의 산술은 불가능
    c1 = (Color)1;
    int n = Red; // n의 결과값 : 0
}
cs


*레퍼런스(reference)

레퍼런스 중에서도 const의 속성을 가진 레퍼런스만 상수로 초기화할 수 있습니다.

1
2
3
4
int main(){
    const int &ref = 10;
    int &ref2 = 15;
}
cs



*유니온(union)

메모리를 공유하여 사용합니다.

가장 큰 변수를 기준으로 메모리공간을 갖습니다.

1
2
3
4
5
6
7
8
9
10
11
union Student {
    char name;
    int number;
    double grade;
};
int main(){
    Student s;
    s.name = 'r';
    s.number = 1213;
    s.grade = 4.3;
}
cs

각 멤버 변수들의 시작 주소값이 일치함을 볼 수 있습니다.


*비트필드(bitfield)

구조체의 멤버 변수에 사용가능한 비트 영역을 할당한다.

4bit 

3bit 

5bit 

1bit 

 int

int 

int (사용 안함) 

bool 


1
2
3
4
5
6
struct Flag {
    int a : 4;
    int b : 3;
    int : 5;
    bool c : 1;
};
cs


*함수

이차원 배열의 인자 전달

1
2
3
4
5
6
7
void function(int arr[][10]) {
}
int main()
{
    int arr[10][10];
    function(arr);
}
cs

함수의 인자로 레퍼런스를 취할 때, 읽기만 한다면 const를 사용하자.

1
2
3
4
5
6
7
8
void function(const int &ref) {
    cout << ref;
}
int main()
{
    int a = 10;
    function(a);
}
cs


*함수의 오버로딩

오버로딩과 디폴트

1
2
3
4
5
6
7
8
void function(int a, int b = 10);
void function(int a);
void function(int a = 10int b); 에러 : 디폴트 인자들은 우측에 있어야한다.
int main()
{
    function(10100);
    function(10); 에러 : 충돌 발생
}
cs

오버로딩 규칙

1
2
3
4
5
6
int func(int i);
void func(int i);
return 형식으로는 구분되는 함수를 오버로드할 수 없습니다.
 
int func(int i);
int func(int &i);
오버로드된 함수에 대한 호출이 모호합니다.
 
cs


*함수 포인터로 함수 가리키기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
typedef void (*FP) (int);
void function(int a)
{
    cout << a<<endl;
}
int main()
{
    void (*p)(int);
    p = &function;
    (*p)(10);
    
    FP ptr = &function;
    (*ptr)(20);
}
cs


*동적 메모리의 할당과 해제

1
2
3
4
5
6
int main()
{
    int *arr = new int [10];
    delete[] arr;
    delete[] arr; 에러 : 해제는 한번만 실시한다.
}
cs


*문자열

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>
int main()
{
    char a[10= "rhkd";
    char b[10= "rmfh";
    string str1 = "rhkd";
    string str2 = "rmfh";
 
문자열 비교
    if (strcmp(a, b)) return true;
    if (str1 == str2) return true;

문자열의 길이    
    strlen(a);
    str1.size();

문자열의 복사 
    strcpy(a, b);
    str1 = str2;

문자열 이어 붙이기 
    strcat(a, b);
    str1 += str2;
 
    str1.c_str(); // const char * 형로 변환, 메모리의 번지 주소
    str1.substr(02); // string.substr(pos, len) : pos~len까지 획득
str1.find("hk"); // string.find("st") 해당 문자열의 index return
}
cs

getline()

1
2
3
4
5
6
7
8
9
10
11
int main()
{
    char a[10= "rhkd";
    char b[10= "rmfh";
    string str1 = "rhkd";
    string str2 = "rmfh";
    
    cin.getline(a, 10);
    cin.clear();
    getline(cin, str1);
}
cs
   


clear() : 플래그의 초기화, 버퍼는 조작하지 않습니다.

1
2
3
4
5
6
7
8
9
10
11
int main()
{
    char a[10= "rhkd";
    char b[10= "rmfh";
    string str1 = "rhkd";
    string str2 = "rmfh";
    
    cin.getline(a, 10);
    cin.clear();
    getline(cin, str1);
}
cs



*객체의 정적 생성과 동적 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    Point pt(100100); 정적생성
 
    Point* p = 0;
    p = new Point(5050); 동적 생성
 
    pt.Print();
    p->Print();
 
    // 동적으로 생성한 객체를 해제한다.
    delete p;
    p = NULL;
 
    return 0;
}
 
cs

동적 생성된 객체 delete를 통하여 직접 해제할 때 소멸자가 호출됩니다.

p의 소멸-> pt의 소멸

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/03   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
글 보관함