PHP - Ds Deque::contains() 函数



PHP 的 Ds\Deque::contains() 函数用于确定双端队列是否包含给定的值。如果当前双端队列包含指定的值,则此函数返回布尔值“true”;否则,它将返回“false”。

语法

以下是 PHP Ds\Deque::contains() 函数的语法 -

public Ds\Deque::contains(mixed ...$values): bool

参数

此函数接受一个名为“values”的参数,如下所述 -

  • values - 需要检查的值。

返回值

如果提供的任何值都存在于双端队列中,则此函数返回“true”,否则返回“false”。

示例 1

以下是 PHP Ds\Deque::contains() 函数的基本示例 -

<?php 
   $deque = new \Ds\Deque([1, 2, 3, 4, 5]);
   echo("The deque elements are: \n"); 
   print_r($deque);
   $val = 2;
   echo "The given value: ".$val;
   echo "\nIs deque contain the element ".$val." or not? "; 
   var_dump($deque->contains($val)); 
?>

输出

执行上述程序后,将显示以下输出 -

The deque elements are:
Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The given value: 2
Is deque contain the element 2 or not? bool(true)

示例 2

如果指定的元素不存在于当前双端队列中,则 Ds\Deque::contains() 函数返回“false” -

<?php 
   $deque = new \Ds\Deque(["Tutorials", "Point", "India"]);
   echo("The deque elements are: \n"); 
   print_r($deque);
   $val = "Tutorix";
   echo "The given value: ".$val;
   echo "\nIs deque contain the element '".$val."' or not? "; 
   var_dump($deque->contains($val)); 
?>

输出

上述程序显示以下输出 -

The deque elements are:
Ds\Deque Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The given value: Tutorix
Is deque contain the element 'Tutorix' or not? bool(false)

示例 3

条件 语句中使用函数“result”来检查指定值“e”是否在双端队列中找到 -

<?php 
   $deque = new \Ds\Deque(['a', 'e', 'i', 'o', 'u']);
   echo("The deque elements are: \n"); 
   print_r($deque);
   $val = 'e';
   echo "The given value: ".$val;
   echo "\nIs the element '".$val."' found in deque or not? "; 
   $result = $deque->contains($val);
   if($result){
	   echo "Found";
   }	   
   else{
	   echo "Not found";
   }
?>

输出

执行上述程序后,它会生成以下输出 -

The deque elements are:
Ds\Deque Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The given value: e
Is the element 'e' found in deque or not? Found
广告