라이브러리
[PHP] DsSet::diff - 다른 세트에 없는 값을 사용하여 새 세트를 만듭니다.
DsSet::diff
PHP 7.4 버전부터 `DsSet` 클래스가 추가되었습니다. `DsSet` 클래스는 집합을 표현하는 클래스로, 집합의 연산을 수행할 수 있습니다. `DsSet::diff` 메서드는 두 집합의 차집합을 반환하는 메서드입니다.
DsSet::diff 메서드
`DsSet::diff` 메서드는 두 집합의 차집합을 반환하는 메서드입니다. 이 메서드는 두 집합의 교집합을 제외한 모든 원소를 반환합니다.
예제
#hostingforum.kr
php
use function SplObjectStorage::attach;
use function SplObjectStorage::detach;
use function SplObjectStorage::rewind;
use function SplObjectStorage::valid;
use function SplObjectStorage::current;
use function SplObjectStorage::key;
use function SplObjectStorage::next;
use function SplObjectStorage::count;
use function SplObjectStorage::contains;
use function SplObjectStorage::getInfo;
use function SplObjectStorage::getIterator;
use function SplObjectStorage::rewind;
use function SplObjectStorage::valid;
use function SplObjectStorage::current;
use function SplObjectStorage::key;
use function SplObjectStorage::next;
use function SplObjectStorage::count;
use function SplObjectStorage::contains;
use function SplObjectStorage::getInfo;
use function SplObjectStorage::getIterator;
// 집합 1 만들기
$set1 = new SplObjectStorage();
$set1->attach(new stdClass());
$set1->attach(new stdClass());
$set1->attach(new stdClass());
// 집합 2 만들기
$set2 = new SplObjectStorage();
$set2->attach(new stdClass());
$set2->attach(new stdClass());
$set2->attach(new stdClass());
$set2->attach(new stdClass());
// 집합 1에서 집합 2를 뺀다.
$diff = $set1->diff($set2);
// 결과를 출력한다.
echo "집합 1에서 집합 2를 뺀 결과:
";
foreach ($diff as $item) {
echo $item . "
";
}
이 예제에서는 두 집합을 만들고, 집합 1에서 집합 2를 뺀 결과를 출력합니다. 결과는 집합 1에만 있는 원소들입니다.
결과
#hostingforum.kr
집합 1에서 집합 2를 뺀 결과:
이 예제에서는 집합 1에만 있는 원소가 없기 때문에 결과는 없습니다. 만약 집합 1에만 있는 원소가 있다면, 그 원소들이 출력됩니다.
DsSet::diff 사용 시 주의점
* `DsSet::diff` 메서드는 두 집합의 교집합을 제외한 모든 원소를 반환합니다. 따라서, 두 집합의 교집합이 있다면, 그 원소들은 결과에 포함되지 않습니다.
* `DsSet::diff` 메서드는 집합의 원소가 객체일 때만 작동합니다. 집합의 원소가 값일 때는 작동하지 않습니다.
* `DsSet::diff` 메서드는 두 집합의 크기가 큰 경우 성능이 좋지 않을 수 있습니다. 따라서, 집합의 크기가 큰 경우에는 다른 방법을 사용하는 것이 좋습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.