1

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

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

4

2 回答 2

2

我不确定这是否是你想要的,所以如果它不起作用,请发表评论:

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);
}
于 2009-11-23T04:39:54.143 回答
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());
}
于 2009-11-23T04:42:42.180 回答