PHP - Ds Vector::rotate() 函数



PHP 的 Ds\Vector::rotate() 函数通过给定的旋转次数旋转向量。

旋转以这样的方式执行:向量的元素向左移动指定位置数。如果旋转次数为负数,则元素会向右移动。

如果旋转次数为正数,则旋转相当于连续调用 “$vector->push($vector->shift())”,或者如果旋转次数为负数,则相当于 “$vector->unshift($vector->pop())”。

语法

以下是 PHP Ds\Vector::rotate() 函数的语法:

public void Ds\Vector::rotate( int $rotations )

参数

以下是此函数的参数:

  • rotations − 向量需要旋转的次数。

返回值

此函数不返回值。

示例 1

以下是 PHP Ds\Vector::rotate() 函数的基本示例:

<?php
   $vector = new \Ds\Vector([1, 2, 3, 4, 5]); 
   echo("The original vector: \n"); 
   print_r($vector);
   $rotations = 1;
   echo "The number of rotations: ".$rotations;
   #using rotate() function
   $vector->rotate($rotations); 
   echo "\nThe vector after rotating by ".$rotations." places: \n"; 
   print_r($vector);
?>

输出

以上程序输出以下内容:

The original vector:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The number of rotations: 1
The vector after rotating by 1 places:
Ds\Vector Object
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 1
)

示例 2

以下是 PHP Ds\Vector::rotate() 函数的另一个示例。我们使用此函数通过给定的旋转次数 3 旋转此向量 (['a', 'e', 'i', 'o', 'u']):

<?php 
   $vector = new \Ds\Vector(['a', 'e', 'i', 'o', 'u']); 
   echo("The original vector: \n"); 
   print_r($vector);
   $rotations = 3;
   echo "The number of rotations: ".$rotations;
   #using rotate() function
   $vector->rotate($rotations);
   echo "\nThe vector after rotating by ".$rotations." places: \n"; 
   print_r($vector);
?>

输出

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

The original vector:
Ds\Vector Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The number of rotations: 3
The vector after rotating by 3 places:
Ds\Vector Object
(
    [0] => o
    [1] => u
    [2] => a
    [3] => e
    [4] => i
)

示例 3

如果我们将旋转值传递为 ,则此函数将不会旋转向量,并且原始向量将保持不变:

<?php
   $vector = new \Ds\Vector(["Tutorials", "Point", "India"]); 
   echo("The original vector: \n"); 
   print_r($vector);
   $rotations = 0;
   echo "The number of rotations: ".$rotations;
   #using rotate() function
   $vector->rotate($rotations); 
   echo "\nThe vector after rotating by ".$rotations." places: \n"; 
   print_r($vector);
?>

输出

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

The original vector:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The number of rotations: 0
The vector after rotating by 0 places:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)

示例 4

如果给定的 rotations 参数值为负数,则 rotate() 函数将逆时针旋转向量:

<?php
   $vector = new \Ds\Vector([10, 20, 30, 40, 50]); 
   echo("The original vector: \n"); 
   print_r($vector);
   $rotations = -1;
   echo "The number of rotations: ".$rotations;
   #using rotate() function
   $vector->rotate($rotations); 
   echo "\nThe vector after rotating by ".$rotations." places: \n"; 
   print_r($vector);
?>

输出

执行以上程序后,将生成以下输出:

The original vector:
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
The number of rotations: -1
The vector after rotating by -1 places:
Ds\Vector Object
(
    [0] => 50
    [1] => 10
    [2] => 20
    [3] => 30
    [4] => 40
)
php_function_reference.htm
广告