각 타입의 최대, 최소값을 구해주는 STL
#include <iostream>
#include <limits>
void main()
{
// int 형의 최대, 최소값
std::cout << std::numeric_limits<int>::min() << std::endl;
std::cout << std::numeric_limits<int>::max() << std::endl;
// double의 최대, 최소값
std::cout << std::numeric_limits<double>::min() << std::endl;
std::cout << std::numeric_limits<double>::max() << std::endl;
}
사용은 이처럼 하게 되고,
사용 자체 보단,
이 클래스를 구현한다고 했을땐,
템플릿 특수화를 사용하게 되는데
그것에 대해서 살짝 집어 보고 넘어가도록 하자.
#include <iostream>
// 클래스 템플릿이 존재하는 상황에서
template< typename T >
class Data{};
// 특수화 템플릿 클래스를 생성(int type)
template < > class Data < int >
{
public:
// 값을 받는 부분이 int 이기 때문에 return은 int
inline static int min() throw()
{
return -100000;
}
inline static int max() throw()
{
return 1000000;
}
};
// 특수화 템플릿 클래스를 생성(double type)
template < > class Data < double >
{
public:
// 값을 받는 부분이 double 이기 때문에 return은 double
inline static double min() throw()
{
return -1234;
}
inline static double max() throw()
{
return 1234;
}
};
void main()
{
std::cout << Data<int>::max() << std::endl; // Data<int>의 클래스를 호출
std::cout << Data<int>::min() << std::endl;
std::cout << Data<double>::max() << std::endl; // Data<double>의 클래스를 호출
std::cout << Data<double>::min() << std::endl;
}
'프로그래밍 > STL' 카테고리의 다른 글
iterator - 반복자 (0) | 2010.04.08 |
---|---|
시퀀스 컨테이너 - vector, list, deque (0) | 2010.04.05 |
용어적 설명. (0) | 2010.04.05 |
2가지 변수를 저장하는 pair (0) | 2010.04.04 |
auto_ptr의 개념. (0) | 2010.04.02 |