我正在尝试将一个矢量变量放在Google的Go编程语言的结构中。这就是我到目前为止所做的:
想要:
type Point struct { x, y int }
type myStruct struct {
myVectorInsideStruct vector;
}
func main(){
myMyStruct := myStruct{vector.New(0)};
myPoint := Point{2,3};
myMyStruct.myVectorInsideStruct.Push(myPoint);
}
有:
type Point struct { x, y int }
func main(){
myVector := vector.New(0);
myPoint := Point{2,3};
myVector.Push(myPoint);
}
我可以让矢量在我的main函数中运行得很好,但我想将它封装在一个struct中以便于使用。
答案 0 :(得分:1)
我不确定这是否是你想要的,所以如果它不起作用,请留言:
package main
import "container/vector";
type Point struct { x, y int };
type mystruct struct {
myVectorInsideStruct * vector.Vector;
}
func main() {
var myMyStruct mystruct;
myMyStruct.myVectorInsideStruct = new(vector.Vector);
myPoint := Point{2,3};
myMyStruct.myVectorInsideStruct.Push(myPoint);
}
答案 1 :(得分:0)
不确定这是你想要的,但是:
package main
import (
"fmt";
"container/vector";
)
type myStruct (
struct {
myVectorInsideStruct vector.IntVector;
}
)
func main() {
v := new(myStruct);
v.myVectorInsideStruct.Init(0);
for i := 1 ; i < 10 ; i++ {
v.myVectorInsideStruct.Push(i);
}
fmt.Printf("v.myVectorInsideStruct: %v\n", v.myVectorInsideStruct.Data());
}