初始化:
res := make([][length]int, length),
例如:
res := make([][2]int, 10)
fmt.Println(res)
输出:
[[0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0]]
或者
a := [][]float64{
{1, 2, 3, 4},
{12, 21, 3, 14},
{1, 1, 2, 3},
{2, 3, 1, 6},
{2, 2, 3, 3},
{1, 1, 1, 1}}
排序:
//根据二维切片的第一个属性从大到小排序,第二个属性默认是递增顺序
people := [][]int{{9, 0}, {7, 0}, {1, 9}, {3, 0}, {2, 7}, {5, 3}, {6, 0}, {3, 4}, {6, 2}, {5, 2}}
less := func(i, j int) bool {
if people[i][0] == people[j][0] {
return people[i][1] < people[j][1]
}
return people[i][0] > people[j][0]
}
//调用排序函数
sort.Slice(people, less)
fmt.Println(people)
//输出
//[[9 0] [7 0] [6 0] [6 2] [5 2] [5 3] [3 0] [3 4] [2 7] [1 9]]
原文地址:https://www.cnblogs.com/nyist-xsk/p/11365412.html
时间: 2024-11-05 20:30:46