如何在Go中将向量放入结构中?

我正在尝试将向量变量放入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);

}

我可以使向量在我的主函数中正常工作,但我想将其封装在结构中以便于使用。


慕哥9229398
浏览 238回答 2
2回答

catspeake

我不确定这是否是您想要的,所以如果不起作用请发表评论:package mainimport "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);}

摇曳的蔷薇

不确定这是否是您想要的,但是:package mainimport (&nbsp; &nbsp; "fmt";&nbsp; &nbsp; "container/vector";)type myStruct (&nbsp; &nbsp; struct {&nbsp; &nbsp; &nbsp; &nbsp; myVectorInsideStruct vector.IntVector;&nbsp; &nbsp; })func main() {&nbsp; &nbsp; v := new(myStruct);&nbsp; &nbsp; v.myVectorInsideStruct.Init(0);&nbsp; &nbsp; for i := 1 ; i < 10 ; i++ {&nbsp; &nbsp; &nbsp; &nbsp; v.myVectorInsideStruct.Push(i);&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("v.myVectorInsideStruct: %v\n", v.myVectorInsideStruct.Data());}
打开App,查看更多内容
随时随地看视频慕课网APP