C++ Quick Reference

Brie·2023년 11월 6일
0

C++

목록 보기
1/8
post-thumbnail

개요

C++에서 사용할 수 있는 Quick Reference Sheet입니다.

Quick Reference Sheet

헤더 파일 추가

#include <headerfile> // library#include "headerfile" // user's header

표준 라이브러리 주 사용 헤더파일

io, algorithms, containers, concurrency

자료형

bool, char, int, double
and standard library types such as string, vector<> and so on

배열, 포인터, 참조자

int arr[5] = { 1,2,3,4,5 };
int* p = arr;
int& r = arr[0];

주석

// comment text
/*
    Multi-line comment text
*/

산술 연산자

++, --, +, -, *, /, %

비트 연산자

&, |, <<, >>, ~, ^

관계 연산자

<, <=, >, >=, ==, !=

논리 연산자

||, &&, !

조건 분기문

if(<conditions>) {
    <statement 1>;
}
else {
    <statement 2>;
}

switch (<expression>)
case <constant 1>:
    <statement sequence 1>;
break;
case <constant 2>:
    <statement sequence 2>;
break;
//...
case <constant n>:
    <statement sequence n>;
break;
default:
    <statement sequence n+1>;
break;
}

반복문

for(<initialize>; <condition>; <update>)
{ <statement>; }

for ( <range_declaration> : <range_expression> )
{ <statement>; }

while(<condition>)
{ <statement>; }

do { <statement>; }
while(<condition>);

I/O 연산자

istream >> var; //입력 연산자
ostream << var; //출력 연산자

int n;
scanf("%d", &n);
printf("%d", n);

파일 I/O

fstream file;
file.open("filename", <file mode constant>);
// read using opeartor
file >> var;
file << var;
// getline
getline(file, line);
// reading and writing binary
file.read(memory_block, size);
file.write(memory_block, size);
file.close();
// File Mode Constants
ios::in     // 읽기 가능
ios:out    // 쓰기 가능
ios::ate    // EOF 찾아가기(seek)
ios::app   // EOF에 이어서 쓰기
ios::trunc // 이전 내용 버리기
ios::nocreate   // 만들어진 파일이 없으면 실패
ios::noreplace // 만들어진 파일이 있으면 실패
// C-Style
/* FEOF example */
FILE * pFile;
char buffer [100];
pFile = fopen ("myfile.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else {
while ( ! feof (pFile) ) {
if ( fgets (buffer , 100 , pFile) == NULL )break;
        fputs (buffer , stdout);
    }
    fclose (pFile);
}

함수(function)

<return_data_type> <function_name>(parameter list)
{ body }

사용자 정의 자료형

// 구조체(struct)
struct <structure_name>
{
public:
  member_type1 member_name1;
protected:
private:
  ...
};

// 클래스(class)
class <class_name>
{
public:
  // method_prototypes
protected:
  // method_prototypes
private:
  // method_prototypes
  // data_attributes

};

객체(objects)

myStruct m;
m.var1 = value;

myClass c;
var = c.method1(arg);

myStruct* pm = new myStruct;
pm->var1 = value;

myClass* pc;
var = pc->method1(arg);

0개의 댓글