
DsPair 클래스는 두 개의 요소를 저장하는 클래스로, 일반적으로 두 개의 변수를 멤버 변수로 가지고 있습니다. 이 두 개의 변수는 일반적으로 left와 right로 표시됩니다.
DsPair 클래스의 isEmpty 메소드는 두 개의 요소가 모두 null 또는 비어 있는지 확인하여 true/false를 반환합니다. isEmpty 메소드는 다음과 같이 구현할 수 있습니다.
#hostingforum.kr
cpp
bool DsPair::isEmpty() {
return (left == nullptr && right == nullptr);
}
이 메소드는 left와 right 멤버 변수가 모두 null이면 true를 반환하고, 둘 중 하나 이상이 null이 아니면 false를 반환합니다.
DsPair 클래스의 isEmpty 메소드는 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
cpp
int main() {
DsPair pair1;
DsPair pair2;
pair1.left = new int(1);
pair1.right = new int(2);
std::cout << std::boolalpha << pair1.isEmpty() << std::endl; // false
std::cout << std::boolalpha << pair2.isEmpty() << std::endl; // true
delete pair1.left;
delete pair1.right;
std::cout << std::boolalpha << pair1.isEmpty() << std::endl; // true
return 0;
}
이 예제에서, pair1은 비어 있지 않지만 pair2은 비어 있습니다. isEmpty 메소드를 호출하여 true/false를 확인할 수 있습니다.
2025-03-03 02:57