當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


GO SearchFloat64s用法及代碼示例

GO語言"sort"包中"SearchFloat64s"函數的用法及代碼示例。

用法:

func SearchFloat64s(a []float64, x float64) int

SearchFloat64s 在 float64s 的排序切片中搜索 x,並返回 Search 指定的索引。如果 x 不存在,則返回值是插入 x 的索引(它可能是 len(a))。切片必須按升序排序。

例子:

此示例演示在按升序排序的列表中搜索 float64。

package main

import (
    "fmt"
    "sort"
)

func main() {
    a := []float64{1.0, 2.0, 3.3, 4.6, 6.1, 7.2, 8.0}

    x := 2.0
    i := sort.SearchFloat64s(a, x)
    fmt.Printf("found %g at index %d in %v\n", x, i, a)

    x = 0.5
    i = sort.SearchFloat64s(a, x)
    fmt.Printf("%g not found, can be inserted at index %d in %v\n", x, i, a)
}

輸出:

found 2 at index 1 in [1 2 3.3 4.6 6.1 7.2 8]
0.5 not found, can be inserted at index 0 in [1 2 3.3 4.6 6.1 7.2 8]

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 SearchFloat64s。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。