라이브러리
[PHP] DsDeque::apply - 각 값에 콜백 함수를 적용하여 모든 값을 업데이트합니다.
PHP 에서 DsDeque::apply 메서드는 Deque (Double-ended Queue) 객체에 적용할 함수를 지정하여 해당 함수를 Deque의 모든 요소에 적용하는 메서드입니다.
DsDeque::apply 메서드의 사용법
DsDeque::apply 메서드는 두 개의 매개변수를 받습니다.
* `$callback`: 적용할 함수
* `$key`: 적용할 함수에 전달할 키 (선택 사항)
예제
#hostingforum.kr
php
// Deque 객체를 생성합니다.
$deque = new SplDoublyLinkedList();
// Deque에 요소를 추가합니다.
$deque->push(1);
$deque->push(2);
$deque->push(3);
$deque->push(4);
$deque->push(5);
// Deque의 요소에 'double' 함수를 적용합니다.
$deque->apply(function ($value) {
return $value * 2;
});
// Deque의 요소를 출력합니다.
while ($deque->valid()) {
echo $deque->current() . "
";
$deque->next();
}
결과
#hostingforum.kr
2
4
6
8
10
설명
위 예제에서, Deque 객체에 'double' 함수를 적용하여 Deque의 모든 요소의 값을 2배로 증가시켰습니다. 결과적으로 Deque의 요소는 2, 4, 6, 8, 10이 됩니다.
추가 예제
#hostingforum.kr
php
// Deque 객체를 생성합니다.
$deque = new SplDoublyLinkedList();
// Deque에 요소를 추가합니다.
$deque->push('apple');
$deque->push('banana');
$deque->push('cherry');
$deque->push('date');
$deque->push('elderberry');
// Deque의 요소에 'ucfirst' 함수를 적용합니다.
$deque->apply(function ($value) {
return ucfirst($value);
});
// Deque의 요소를 출력합니다.
while ($deque->valid()) {
echo $deque->current() . "
";
$deque->next();
}
결과
#hostingforum.kr
Apple
Banana
Cherry
Date
Elderberry
설명
위 예제에서, Deque 객체에 'ucfirst' 함수를 적용하여 Deque의 모든 요소의 첫 글자를 대문자로 변환시켰습니다. 결과적으로 Deque의 요소는 Apple, Banana, Cherry, Date, Elderberry가 됩니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.