m := map[string]string{
"1": "one",
"2": "two",
"1": "one",
"3": "three",
}
fmt.Println(m)
// 空结构体
var Exists = struct{}{}
// Set is the main interface
type Set struct {
// struct为结构体类型的变量
m map[interface{}]struct{}
}
import (
"fmt"
"unsafe"
)
// 定义非空结构体
type S struct {
a uint16
b uint32
}
func main() {
var s S
fmt.Println(unsafe.Sizeof(s)) // prints 8, not 6
var s2 struct{}
fmt.Println(unsafe.Sizeof(s2)) // prints 0
}
a := struct{}{}
b := struct{}{}
fmt.Println(a == b) // true
fmt.Printf("%p, %pn", &a, &b) // 0x55a988, 0x55a988
var Exists = struct{}{}
func New(items ...interface{}) *Set {
// 获取Set的地址
s := &Set{}
// 声明map类型的数据结构
s.m = make(map[interface{}]struct{})
s.Add(items...)
return s
}
func (s *Set) Add(items ...interface{}) error {
for _, item := range items {
s.m[item] = Exists
}
return nil
}
func (s *Set) Contains(item interface{}) bool {
_, ok := s.m[item]
return ok
}
func (s *Set) Size() int {
return len(s.m)
}
func (s *Set) Clear() {
s.m = make(map[interface{}]struct{})
}
func (s *Set) Equal(other *Set) bool {
// 如果两者Size不相等,就不用比较了
if s.Size() != other.Size() {
return false
}
// 迭代查询遍历
for key := range s.m {
// 只要有一个不存在就返回false
if !other.Contains(key) {
return false
}
}
return true
}
func (s *Set) IsSubset(other *Set) bool {
// s的size长于other,不用说了
if s.Size() > other.Size() {
return false
}
// 迭代遍历
for key := range s.m {
if !other.Contains(key) {
return false
}
}
return true
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有