최대 1 분 소요


std::cerr c에서의 stderr와 상응.

exception



< ex >

#include <iostream>
#include <exception>

class MakeException : public std::exception {
	public:
		const char* what(void) const throw() { return "made exception"; }
};

int main(void)
{
	int a;

	try
	{
		while (1)
		{
			std::cin >> a;
			if (a > 10)
				throw MakeException();
		}
	}
	catch (std::exception& e)
	{
		std::cerr << e.what() << std::endl;
	}
	finally
	{
		//
	}
}

try-catch구문에서 조건만족시 throw로 예외를 발생시킨다. std::exception클래스를 상속받아 멤버변수 what을 오버라이딩하여 예외처리시 원하는 구문을 띄울 수 있다.

std::exception::what

class Exception {
	virtual const char* what() const throw();
}

역할 : Get string identifying exception.


const_cast

const_cast<new_type>(expression)

변수에 붙어있는 const를 잠시 제거해주는 역할을 한다.
포인터 레퍼런스의 상수성을 잠시 제거.

boolalpha

bool a = true;
std::cout << std::boolalpha << a << std::endl;

bool타입을 출력할 때, boolalpha를 사용하면 알파벳표기로 바꿔준다.

난수생성

#include <cstdlib>
#include <ctime>
#include <iostream>

int main()
{
    srand(time(NULL));				//seed값으로 현재시간 부여 
    printf("난수 : %d\n", rand());
    printf("난수 : %d\n", rand());
    printf("난수 : %d\n", rand());
    printf("난수 : %d\n", rand());
    printf("난수 : %d\n", rand());
}
난수 : 1314053464
난수 : 574743700
난수 : 335921694
난수 : 101403095
난수 : 1327285594

댓글남기기