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
廣告