zoukankan      html  css  js  c++  java
  • Go语言的map以及sort

    通过这个例子了解map的使用。


    Go语言程序:

    // map project main.go
    package main
    
    import (
    	"fmt"
    	"sort"
    )
    
    func main() {
    
    	var countryCapitalMap map[string]string
    	/* 创建集合 */
    	countryCapitalMap = make(map[string]string)
    
    	/* map 插入 key-value 对,各个国家对应的首都 */
    	countryCapitalMap["France"] = "Paris"
    	countryCapitalMap["Italy"] = "Rome"
    	countryCapitalMap["Japan"] = "Tokyo"
    	countryCapitalMap["India"] = "New Delhi"
    	countryCapitalMap["China"] = "Beijing"
    
    	fmt.Println("Original:")
    	/* 使用 key 输出 map 值 */
    	for country := range countryCapitalMap {
    		fmt.Println("Capital of", country, "is", countryCapitalMap[country])
    	}
    	fmt.Println("")
    
    	/* map 删除 key-value 对 */
    	delete(countryCapitalMap, "India")
    	fmt.Println("After deletion:")
    	/* 使用 key 输出 map 值 */
    	for country := range countryCapitalMap {
    		fmt.Println("Capital of", country, "is", countryCapitalMap[country])
    	}
    	fmt.Println("")
    
    	/* 映射反转 */
    	capitalCountryMap := make(map[string]string, len(countryCapitalMap))
    	for country, capital := range countryCapitalMap {
    		capitalCountryMap[capital] = country
    	}
    	fmt.Println("After the reversal:")
    	for capital := range capitalCountryMap {
    		fmt.Println("Country of", capital, "is", capitalCountryMap[capital])
    	}
    	fmt.Println("")
    
    	/* 查看元素在集合中是否存在 */
    	captial, ok := countryCapitalMap["United States"]
    	/* 如果 ok 是 true, 则存在,否则不存在 */
    	if ok {
    		fmt.Println("Capital of United States is", captial)
    	} else {
    		fmt.Println("Capital of United States is not present")
    	}
    	fmt.Println("")
    
    	/* 排序输出 */
    	var keys []string
    	for k := range countryCapitalMap {
    		keys = append(keys, k)
    	}
    	sort.Strings(keys)
    	for _, k := range keys {
    		fmt.Println("Key:", k, "Value:", countryCapitalMap[k])
    	}
    }
     

    程序运行结果:

    Original:
    Capital of France is Paris
    Capital of Italy is Rome
    Capital of Japan is Tokyo
    Capital of India is New Delhi
    Capital of China is Beijing
    
    After deletion:
    Capital of France is Paris
    Capital of Italy is Rome
    Capital of Japan is Tokyo
    Capital of China is Beijing
    
    After the reversal:
    Country of Paris is France
    Country of Rome is Italy
    Country of Tokyo is Japan
    Country of Beijing is China
    
    Capital of United States is not present
    
    Key: China Value: Beijing
    Key: France Value: Paris
    Key: Italy Value: Rome
    Key: Japan Value: Tokyo

    程序说明:

    1.map似乎可以直接使用

    2.排序可以使用sort包

    3.map是无序的需要另外定义一个数组来排序


    参考链接:

    1.Ubuntu安装Go语言环境

    2.Ubuntu构筑LiteIDE的Go语言开发环境



  • 相关阅读:
    软件工程学习小结
    Java中protected方法访问权限的问题
    HDU1395 2^x mod n = 1 暴力题
    HDU1394 线段树求最小逆序数
    HDU2030 汉字统计
    HDU2028 最小公倍数
    mysql中如何创表以及添加各种约束条件
    数据库知识总结
    如何在dos命令中启动mysql或sql server 服务器的一些操作
    mysql如何建表
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7563558.html
Copyright © 2011-2022 走看看