gpt4 book ai didi

vector - 未定义的向量.New

转载 作者:IT王子 更新时间:2023-10-29 01:49:27 25 4
gpt4 key购买 nike

来自这个问题

How do I use a (generic) vector in go?

我试图创建一个新向量,但编译器说它未定义:

$ 6g -V
6g version release.r60.3 9516
$ cat > vectest.go <<.
> package main
>
> import vector "container/vector"
> import "fmt"
>
> func main() {
> vec := vector.New(0);
> buf := make([]byte,10);
> vec.Push(buf);
>
> for i := 0; i < vec.Len(); i++ {
> el := vec.At(i).([]byte);
> fmt.Print(el,"\n");
> }
> }
> .
$ 6g vectest.go
vectest.go:7: undefined: vector.New

可能出了什么问题?

最佳答案

weekly.2011-10-18

The container/vector package has been deleted. Slices are better. SliceTricks: How to do vector-esque things with slices.

我修改了 your convertToLCD code 以获得更好的性能:5,745 ns/op 对比 19,003 ns/op。

package main

import (
"fmt"
"strconv"
)

const (
lcdNumerals = `
_ _ _ _ _ _ _ _
| | | _| _||_||_ |_ ||_||_|
|_| ||_ _| | _||_| ||_| _|
`
lcdWidth = 3
lcdHeight = 3
lcdLineLen = (len(lcdNumerals) - 1) / lcdWidth
)

func convertToLCD(n int) string {
digits := strconv.Itoa(n)
displayLineLen := len(digits)*lcdWidth + 1
display := make([]byte, displayLineLen*lcdHeight)
for i, digit := range digits {
iPos := i * lcdWidth
digitPos := int(digit-'0') * lcdWidth
for line := 0; line < lcdHeight; line++ {
numeralPos := 1 + lcdLineLen*line + digitPos
numeralLine := lcdNumerals[numeralPos : numeralPos+lcdWidth]
displayPos := displayLineLen*line + iPos
displayLine := display[displayPos : displayPos+lcdWidth]
copy(displayLine, string(numeralLine))
if i == len(digits)-1 {
display[displayLineLen*(line+1)-1] = '\n'
}
}
}
return string(display)
}

func main() {
fmt.Printf("%s\n", convertToLCD(1234567890))
}

输出:

    _  _     _  _  _  _  _  _ 
| _| _||_||_ |_ ||_||_|| |
||_ _| | _||_| ||_| _||_|

关于vector - 未定义的向量.New,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9779332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com