본문 바로가기
반응형

C++19

6-2. 포인터의 기본적인 사용법, 널 포인터 & (address of operator) 모든 변수는 내부적으로 주소를 가진다. 변수가 어떤 메모리 주소에 담겨있는지 알고싶다면 &를 사용한다. 이 메모리 주소를 담는 변수를 포인터라고 부른다. * (de-reference operator) #include using namespace std; int main() { int x = 5; int y = 10; int *ptr_x = &x; int *ptr_y = &y; cout 2024. 2. 18.
5-3. C++ std::cin.fail(), std::cin.clear(), std::cin.ignore() 함수 cin.ignore() cin.ignore함수는 첫번째 parameter 개수만큼 문자를 읽어온 뒤 버리거나, 두번째 parameter(delim)에 해당하는 문자를 만나면 해당 문자까지 읽고 버린다. 둘중 먼저 만족하는 조건을 따르게 된다. int main() { int x; while (true) { cout > x; if (cin.fail()) { std::cin.clear(); std::cin.ignore(32767, '\n'); cout 2024. 2. 7.
5-2. C++ 난수 만들기 난수 만들기 (Random Number Generation) 실행할 때마다 다른 난수 생성하기 #include #include // std::rand(), std::srand() #include // std::time() using namespace std; int main() { std::srand(static_cast(std::time(0))); for (int count = 1; count 2024. 2. 5.
5-1. C++ switch-case, do-while switch-case 특별한 경우에 if문보다 깔끔하고 간결하게 코딩을 할수있다. #include using namespace std; enum class Colors { RED, // 0 GREEN, // 1 BLUE, // 2 }; void printColor(Colors Color) { switch(static_cast(Color)) // cast를 통해 case를 정수로 { case 0 : cout 2024. 2. 3.
4-3. C++ 자료형에게 가명 붙이기 (type aliases), 구조체 (struct) 자료형에게 가명 붙이기 (Type aliases) typedef 뒤에 이름을 붙여주고 싶은 자료형을 써주고 가명을 사용한다. #include #include int main() { using namespace std; typedef vector pairlist_t; // using pairlist_t = vector 로도 사용가능 pairlist_t pairlist1; pairlist_t pairlist2; return 0; } 내 프로그램에서 이 자료형을 표현할때 이 가명을 쓰겠다고 선언하는 것이다. 기본적으로 긴 문장을 짧게 줄이는데 매우 편리하게 사용할 수 있다. 구조체 (struct) 다양한 요소들을 묶어서 마치 하나의 자료형인 것처럼, 하나의 사용자 정의 자료형인 것처럼 사용할 수 있게 해주는 .. 2024. 2. 2.
4-2. C++ 문자열, 열거형, 열거형 클래스 문자열 (string) C++ 언어에서 기본적으로 제공해주는 것은 한 글자이다. 한 글자를 여러개 나열하는 방식으로 문자열을 사용한다. #include #include #include int main() { using namespace std; cout > name; std::getline(std::cin, name); cout > age; // std::cin.ignore(32767, '\n'); std::cin.ignore(std::numeric_limits::max(), '\n'); string abc = "abcdefg"; cout 2024. 2. 2.
반응형