C++에서 사용할 수 있는 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>);
istream >> var; //입력 연산자
ostream << var; //출력 연산자
int n;
scanf("%d", &n);
printf("%d", n);
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);
}
<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
};
myStruct m;
m.var1 = value;
myClass c;
var = c.method1(arg);
myStruct* pm = new myStruct;
pm->var1 = value;
myClass* pc;
var = pc->method1(arg);