排序
package main
import(
"fmt"
"sort"
)
func main() {
testInSort()
testString()
testFloat()
}
func testFloat(){
var a = [...]float64{12.5,455.677,236.77,-234.66,0,5.99}
sort.Float64s(a[:])
fmt.Println(a)
}
func testString(){
var a = [...]string{"wefD","wefghn","thGw","wGGef","sfsf"}
sort.Strings(a[:])
fmt.Println(a)
}
/*数组是值类型,如果要改变值,需要传切片进去,去改变*/
func testInSort(){
var a = [...]int{1,3,5,-5,-4}
//需要传递切片进去才能排序
sort.Ints(a[:])
fmt.Println(a)
}
输出如下:
PS E:\golang\go_pro\src\safly> go run demo.go
[-5 -4 0 1 3 5]
[sfsf thGw wGGef wefD wefghn]
[-234.66 0 5.99 12.5 236.77 455.677]
PS E:\golang\go_pro\src\safly>
二分查找
ort.SearchInts(a []int,b int) 从数组a中查找b,前提是a必须有序
sort.SearchFloats(a []float64,b float64) 从数组a中查找b,前提是a必须有序
sort.SearchStrings(a []string,b string) 从数组a中查找b,前提是a必须有序
package main
import(
"fmt"
"sort"
)
func main() {
testSearchIn()
} //从数组a中查找b,前提是a必须有序
func testSearchIn(){
var a = [...] int {1,8,38,2,348,484,-6}
//没有排序直接进行查找
index:= sort.SearchInts(a[:],2)
//输出1是错误的,因为没有排序
fmt.Println(index)
//进行排序操作
sort.Ints(a[:])
fmt.Println(a)
//传切片过去
index= sort.SearchInts(a[:],2)
fmt.Println(index)
}
输出如下:
PS E:\golang\go_pro\src\safly> go run demo.go
1
[-6 1 2 8 38 348 484]
2
PS E:\golang\go_pro\src\safly>
map
map创建方式
package main
import(
"fmt"
)
func main() {
testMap()
testMoreMap()
}
//map嵌套
func testMoreMap(){
a:= make(map[string]map[string]string,100)
//value是map,使用时候需要为其开辟空间
a["k1"] = make(map[string]string)
a["k1"]["k1k1"]= "aaa"
fmt.Println(a)
}
//创建map的2种方式
func testMap(){
a := make(map[string]string,10)
a["abc"] = "abc"
fmt.Println(a)
var b map[string] string = map[string]string{
"bcd":"bcd",}
fmt.Println(b)
}
输出如下:
PS E:\golang\go_pro\src\safly> go run demo.go
map[abc:abc]
map[bcd:bcd]
map[k1:map[k1k1:aaa]]
PS E:\golang\go_pro\src\safly>
map循环遍历
package main
import(
"fmt"
)
func main() {
testMap3()
fmt.Println("---------------")
testMap4()
testMap5()
}
/*切片里面是map*/
func testMap5() {
var a []map[int]int
a = make([]map[int]int, 5)
if a[0] == nil {
a[0] = make(map[int]int)
}
a[0][10] = 10
fmt.Println(a)
}
/*循环遍历*/
func testMap4(){
a:= make(map[string]map[string]string,100)
a["k1"] = make(map[string]string)
a["k1"]["k2"] = "abc"
a["k1"]["k3"] = "bcd"
a["k2"] = make(map[string]string)
a["k2"]["k4"] = "efg"
a["k2"]["k5"] = "hij"
for k,v := range a{
fmt.Println(k)
for k1,v1:= range v{
fmt.Println(k1,v1)
}
}
//删除
fmt.Println("-------删除前后-------")
delete(a,"k1")
for k,v1)
}
}
}
func testMap3(){
a := make(map[string]map[string]string,10)
modify(a)
fmt.Println(a)
}
func modify(a map[string]map[string]string){
_,ok:=a["zhangsan"]
//输出false
fmt.Println(ok)
if !ok {
a["zhangsan"] = make(map[string]string)
}
a["zhangsan"]["pass"] = "123"
return
}
输出如下:
PS E:\golang\go_pro\src\safly> go run demo.go
false
map[zhangsan:map[pass:123]]
---------------
k1
k2 abc
k3 bcd
k2
k4 efg
k5 hij
-------删除前后-------
k2
k4 efg
k5 hij
[map[10:10] map[] map[] map[] map[]]
PS E:\golang\go_pro\src\safly>
@H_502_505@排序、反转
package main
import(
"fmt"
"sort"
)
func testMapReverse(){
var a map[int]string
var b map[string] int
a = make(map[int]string,5)
b = make(map[string] int ,5)
a[1] = "a"
a[2] = "b"
for k,v := range a{
b[v] = k
}
for k,v := range b{
fmt.Println(k,v)
}
}
func main() {
testMapSort()
fmt.Println("--------")
testMapReverse()
}
func testMapSort(){
var a map[int]int
a = make(map[int]int ,8)
a[1] = 10
a[2] = 11
a[3] = 12
a[4] = 13
//定义一个切片
var b []int
//说明map是无序的
for k,v := range a{
fmt.Println(k,v)
b = append(b,v)
}
sort.Ints(b)
fmt.Println("----------对map的值进行排序(放到切片里面)-----------")
for k,v:= range b{
fmt.Println(k,v)
}
}
输出如下:
PS E:\golang\go_pro\src\safly> go run demo.go
1 10
2 11
3 12
4 13
----------对map的值进行排序(放到切片里面)-----------
0 10
1 11
2 12
3 13
--------
b 2
a 1
PS E:\golang\go_pro\src\safly>