#정리
** 버전 체크 : cout << "C++ standard Check:" << __cplusplus << endl;- 1997년 11월 : C++ 97 ver.
- 2011년 03월 : C++ 11 ver.
- 2014년 02월 : C++ 14 ver.
- 2017년 03월 : C++ 17 ver.
# printf / cout - 출력 명령문
//출력 명령문은 cout(console out)으로 한다.
- console out << contents << endline; 뼈대로 작성한다.
- using namespace std; 를 선언하면 출력 명령어에 std를 빼고 쓸 수 있다.
//줄 넘기기는 endl; 혹은 \n으로 할 수 있다.- cout << "-----------------" << endl; //endl : 줄 넘기기
- cout << "Hello\n" ;
//1줄로 연결하여 작성할 수 있다.
- cout << "su: " << 100 << " " << "avg: " << 3.14 <<endl ;// 16진수 , 8진수, 10진수 출력
- 16진수로 표기 방법 변경 : cout << hex ;
- 8진수 표기 방법 변경 : cout << oct ;
- 10진수 표기 방법 변경 : cout << dec ;
//비트 변수 초기화 및 출력 : #include <bitset> 선언 필요
- bitset<8> x1;//x1변수는 00000000 (8개)
- bitset<8> x2{0};//x1변수는 00000000 (8개)
- bitset<8> x3{8};//<bit count> {value}
- bitset<8> x4{10};//<bit count> {value}
- bitset<32> x5{2147483647};//<bit count> {value}//배열 형태 기본 문자열 변수 사용 가능
- char name[30] = "kim";//string type 기본으로 들어가있어, 자바처럼 string 사용가능.
- string name2 = "lee"; // 자바처럼 string 사용가능.
- char *name2 = "lee"; //사용 불가 : 문자열 포인트 변수 사용 안된다.
//문자열 합성은 조심해야한다.
- string name3 = "kim" + "lee"; //상수로 인식되어 변경 불가
- string name3 = name2 + "lee"; //변수에 넣고 붙이는 것은 가능
#cpp input cin - 입력 값 받기
//문자열 배열 변수로 인풋 받기
- char name1[10]; //9+1
- cin >> name1;
//string 변수로 인풋 받기
- string name2; //갯수 제한 없음
- cin >> name2;
//int 변수로 인풋 받기 - 문자열이 들어오면 "0"으로 처리된다.
- int kor;
- cin >> kor;
# variable _ init - 변수 초기화//int(정수) 변수 선언 및 초기화
- int b(20); //c++ style >> int b = 20;
- int c = 0b1111; //c++ style >> binary(1111) = 15; : 이진법으로 넣을 수 있다.
//int pointer 변수 선언 및 초기화- int *pt_su = new int;//c++ style >> *pt_su = 0;
- int *pt_su2 = new int(10);//c++ style >> *pt_su = 10;
//array(배열) 변수 선언 및 초기화- int arr[5] = {}; //{0},{0,0,0,0,0} 모두 같은 효과
//array pointer 변수 선언 및 초기화
- int *pt_arr = new int[5]; //c++ style//delete 명령어 기능 >> NULL 할당과 같이 사용됨.
- 1. 메모리 해제(free( ))>> delete[] pt_arr; //메모리 해제
- 2. 소멸자(~클래스명( ))를 호출해준다.
- delete[] arr; //error - c style로 생성한 일반 배열은 해제할 수 없다.
** 버전 체크 : cout << "C++ standard Check:" << __cplusplus << endl;
- 1997년 11월 : C++ 97 ver.
- 2011년 03월 : C++ 11 ver.
- 2014년 02월 : C++ 14 ver.
- 2017년 03월 : C++ 17 ver.
# printf / cout - 출력 명령문
//출력 명령문은 cout(console out)으로 한다.
- console out << contents << endline; 뼈대로 작성한다.
- using namespace std; 를 선언하면 출력 명령어에 std를 빼고 쓸 수 있다.
//줄 넘기기는 endl; 혹은 \n으로 할 수 있다.
- cout << "-----------------" << endl; //endl : 줄 넘기기
- cout << "Hello\n" ;
//1줄로 연결하여 작성할 수 있다.
- cout << "su: " << 100 << " " << "avg: " << 3.14 <<endl ;
// 16진수 , 8진수, 10진수 출력
- 16진수로 표기 방법 변경 : cout << hex ;
- 8진수 표기 방법 변경 : cout << oct ;
- 10진수 표기 방법 변경 : cout << dec ;
//비트 변수 초기화 및 출력 : #include <bitset> 선언 필요
- bitset<8> x1;//x1변수는 00000000 (8개)
- bitset<8> x2{0};//x1변수는 00000000 (8개)
- bitset<8> x3{8};//<bit count> {value}
- bitset<8> x4{10};//<bit count> {value}
- bitset<32> x5{2147483647};//<bit count> {value}
//배열 형태 기본 문자열 변수 사용 가능
- char name[30] = "kim";
//string type 기본으로 들어가있어, 자바처럼 string 사용가능.
- string name2 = "lee"; // 자바처럼 string 사용가능.
- char *name2 = "lee"; //사용 불가 : 문자열 포인트 변수 사용 안된다.
//문자열 합성은 조심해야한다.
- string name3 = "kim" + "lee"; //상수로 인식되어 변경 불가
- string name3 = name2 + "lee"; //변수에 넣고 붙이는 것은 가능
--예문 코드 보기--
#include <iostream>
#include <bitset>
using namespace std; //출력명령어에 std를 빼고 쓸 수 있게 해준다.
int main(int argc, char **argv)
{
//console out << contents << endline;
//std::cout << "Hello" << std::endl;
cout << "Hello" << endl;
cout << "-----------------" << endl;//endl : 줄 넘기기
printf("%s %d %.2lf\n", "kim",100,3.14);
//줄 넘기기는 \n으로 할 수도 있다.
cout << "Hello\n" ;
//1줄로 연결하여 작성할 수 있다.
cout << "su: " << 100 << " " << "avg: " << 3.14 <<endl ;
int a1 = 14;
int a2 = 15;
//16진수로 표기 방법 변경
cout << hex;
cout << a1 << endl;
cout << a2 << endl;
int b1 = 7;
int b2 = 8;// >> 10
int b3 = 16;
//8진수 표기 방법 변경
cout << oct << b1 << " " << b2 << " " << b3 << endl;
int c = 80;
cout << dec << c << endl;
//#include <bitset>
bitset<8> x1;//x1변수는 00000000 (8개)
bitset<8> x2{0};//x1변수는 00000000 (8개)
bitset<8> x3{8};//<bit count> {value}
bitset<8> x4{10};//<bit count> {value}
bitset<32> x5{2147483647};//<bit count> {value}
cout << "x1:" << x1 << endl;
cout << "x2:" << x2 << endl;
cout << "x3:" << x3 << endl;
cout << "x4:" << x4 << endl;
cout << "x5:" << x5 << endl;
cout << "-----------------" << endl;
//버전 체크
cout << "C++ standard Check:" << __cplusplus << endl;
//1997년 11월 : C++ 97 ver.
//2011년 03월 : C++ 11 ver.
//2014년 02월 : C++ 14 ver.
//2017년 03월 : C++ 17 ver.
cout << "-----------------" << endl;
char name[30] = "kim"; //기본 문자열 배열은 사용 가능
cout << "name:" << name << endl;
//char *name2 = "lee"; //문자열 포인트 변수 사용 안됨.
string name2 = "lee"; //string type 기본으로 들어가있어, 자바처럼 string 사용가능.
cout << "name2: " << name2 <<endl;
//문자열 합성은 조심해야한다.
//string name3 = "kim" + "lee"; //상수로 인식되어 변경 불가
string name3 = name2 + "lee"; //변수에 넣고 붙이는 것은 가능
name3 += "lee";
name3 += "lee";
cout << "name3: " << name3 <<endl;
return 0;
}
#cpp input cin - 입력 값 받기
//문자열 배열 변수로 인풋 받기
- char name1[10]; //9+1
- cin >> name1;
//string 변수로 인풋 받기
- string name2; //갯수 제한 없음
- cin >> name2;
//int 변수로 인풋 받기 - 문자열이 들어오면 "0"으로 처리된다.
- int kor;
- cin >> kor;
--예문 코드 보기--
#include <iostream>
using namespace std; //출력명령어에 std를 빼고 쓸 수 있게 해준다.
int main(int argc, char **argv)
{
//console out << contents << endline;
//std::cout << "Hello" << std::endl;
cout << "Hello" << endl;
cout << "-----------------" << endl;
//문자열 배열 변수로 인풋 받기
char name1[10]; //9+1
cout << "Input name1[9Byte]: " <<endl;
cin >> name1;
cout << "name:" << name1 << endl;
//string 변수로 인풋 받기
string name2; //갯수 제한 없음
cout << "Input name2[9Byte]: " <<endl;
cin >> name2;
cout << "name2:" << name2 << endl;
//int 변수로 인풋 받기 - 문자열이 들어오면 "0"으로 처리된다.
int kor;
cout << "Input kor[9Byte]: " <<endl;
cin >> kor;
cout << "kor:" << kor << endl;
return 0;
}
# variable _ init - 변수 초기화
//int(정수) 변수 선언 및 초기화
- int b(20); //c++ style >> int b = 20;
- int c = 0b1111; //c++ style >> binary(1111) = 15; : 이진법으로 넣을 수 있다.
//int pointer 변수 선언 및 초기화
- int *pt_su = new int;//c++ style >> *pt_su = 0;
- int *pt_su2 = new int(10);//c++ style >> *pt_su = 10;
//array(배열) 변수 선언 및 초기화
- int arr[5] = {}; //{0},{0,0,0,0,0} 모두 같은 효과
//array pointer 변수 선언 및 초기화
- int *pt_arr = new int[5]; //c++ style
//delete 명령어 기능 >> NULL 할당과 같이 사용됨.
- 1. 메모리 해제(free( ))
>> delete[] pt_arr; //메모리 해제
- 2. 소멸자(~클래스명( ))를 호출해준다.
- delete[] arr; //error - c style로 생성한 일반 배열은 해제할 수 없다.
--예문 코드 보기--
#include <iostream>
using namespace std; //출력명령어에 std를 빼고 쓸 수 있게 해준다.
int main(int argc, char **argv)
{
cout << "-- var_init --" << endl;
int a = 10;
int b(20); //c++ style >> int b = 20;
//int b{100}; //c++ style >> int b = 100;
int c = 0b1111; //c++ style >> binary(1111) = 15;
cout << a << " " << b << " " << c << endl;
cout << "--- int pointer -----" << endl;
//int *pt_su = &c;
int *pt_su = new int;//c++ style >> *pt_su = 0;
cout << "pt_su: " << *pt_su << endl;
*pt_su = 99;
cout << "pt_su: " << *pt_su << endl;
cout << "--- int pointer -----" << endl;
int *pt_su2 = new int(10);//c++ style >> *pt_su = 10;
cout << "pt_su2: " << *pt_su2 << endl;
*pt_su2 = 88;
cout << "pt_su2: " << *pt_su2 << endl;
cout << "--- array -----" << endl;
int arr[5] = {}; //{0},{0,0,0,0,0} 모두 같은 효과
for(int i=0;i<5;i++){
cout << "arr["<< i <<"]: " << arr[i] << endl;
}
cout << "--- array pointer ---" << endl;
//int *pt_arr = arr; //c style
int *pt_arr = new int[5]; //c++ style
for(int i=0;i<5;i++){
cout << "pt_arr["<< i <<"]: " << pt_arr[i] << endl;
}
cout << "---- delete -----" << endl;
//delete 기능
//1. 메모리 해제(free())
//2. 소멸자(~클래스명())를 호출해준다.
//delete[] arr; //error - c style로 생성한 일반 배열은 해제할 수 없다.
delete pt_su2;
pt_su2 = NULL;
delete[] pt_arr;
pt_arr = NULL;
return 0;
}
'C ++' 카테고리의 다른 글
[C++] 3. 변수 타입(type) 정리, 연산자(operator), 분기문(statement) (0) | 2022.04.05 |
---|---|
[C++] 2. 기초 문법 - 변수 타입, auto(타입 추론), 참조변수(&), 좌측값과 우측값, const(상수 처리) (0) | 2022.04.05 |