在向量中初始化结构 [英] initialization of a struct in a vector

查看:60
本文介绍了在向量中初始化结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


这应该是基本的,但我似乎无法获得正确的语法。


i有一个struct


struct PTStruct

{

int x;

int y;

};


class abc

{

public:

abc() ;

...


std :: vector< PTStruct> mypoints;

}


void abc :: abc():mypoints(100)

{


}

并且我试图将mypoints中的所有PTStruct变量(x和y)设为

初始化为零,但是我某种程度上搞砸了语法。


有什么建议应该怎么做?


感谢所有人!


Jay

解决方案



Jayw710写道:

大家好,
这应该是基本的,但我似乎无法获得正确的语法。

我有一个结构

struct PTStruct
{
int x;
int y;


在这里添加一个ctor。 Ctors总是很有用,即使在结构中:


PTStruct(int initX,int initY):x(initX),y(initY){}


};

班级abc
{
公开:
abc();
...

标准::矢量< PTStruct> mypoints;
}

void abc :: abc():mypoints(100)



更改为:


void abc :: abc():mypoints(100,PTStruct(0,0))


希望这有帮助,

-shez-

{

}

我正在尝试获取$中的所有PTStruct变量(x和y) b $ b mypoints被初始化为零,但是我在某种程度上搞砸了语法。

有什么建议应该怎么做?

感谢所有人!

周杰伦




Jayw710写道:

...
我有一个结构


{
int x;
int y;
};

类abc
{
public:
abc();
...

std :: vector< PTStruct> mypoints;
}

void abc :: abc():mypoints(100)


''void''???构造函数没有返回类型。应该只是


abc :: abc():mypoints(100)

{

}

和我试图将mypoints中的所有PTStruct变量(x和y)初始化为零,但是我在某种程度上搞砸了语法。

有什么建议应该怎么做?
...




你不需要做任何事情。上面的代码已经将所有结构中的所有

字段初始化为零。如果你的编译器没有这样做,那就坏了。


-

祝你好运,

Andrey Tarasevich




Andrey Tarasevich写道:

Jayw710写道:

...
我有一个结构


int x;
int y;
};
班级abc
公共:
abc();
...

std :: vector< PTStruct> mypoints;
}

void abc :: abc():mypoints(100)
''void''???构造函数没有返回类型。应该是..................................................................................................................... $ b和我试图将
mypoints中的所有PTStruct变量(x和y)初始化为零,但是我在某种程度上搞砸了语法。

有什么建议应该怎么做?
...



你不需要做任何事情。上面的代码已经将所有结构中的所有
字段初始化为零。如果你的编译器没有这样做,那就是



坏了。




不依赖它向量从哪里获取内存?


hi all,

this should be basic, but I can''t seem to get the right syntax.

i have a struct

struct PTStruct
{
int x;
int y;
};

class abc
{
public:
abc();
...

std::vector<PTStruct> mypoints;
}

void abc::abc() : mypoints(100)
{

}
and i''m trying to get all the PTStruct variables (x and y) in mypoints to be
initialized to zero, but I''m screwing up the syntax somehow.

Any suggestions how this should be done?

Thanks to all!

Jay

解决方案


Jayw710 wrote:

hi all,

this should be basic, but I can''t seem to get the right syntax.

i have a struct

struct PTStruct
{
int x;
int y;
add a ctor here. Ctors are always useful, even in structs:

PTStruct(int initX, int initY) : x(initX), y(initY) { }

};

class abc
{
public:
abc();
...

std::vector<PTStruct> mypoints;
}

void abc::abc() : mypoints(100)

change this to:

void abc::abc() : mypoints(100, PTStruct(0, 0))

Hope this helps,
-shez-
{

}
and i''m trying to get all the PTStruct variables (x and y) in mypoints to be initialized to zero, but I''m screwing up the syntax somehow.

Any suggestions how this should be done?

Thanks to all!

Jay




Jayw710 wrote:

...
i have a struct

struct PTStruct
{
int x;
int y;
};

class abc
{
public:
abc();
...

std::vector<PTStruct> mypoints;
}

void abc::abc() : mypoints(100)
''void''??? Constructors have no return type. Should be just

abc::abc() : mypoints(100)
{

}
and i''m trying to get all the PTStruct variables (x and y) in mypoints to be
initialized to zero, but I''m screwing up the syntax somehow.

Any suggestions how this should be done?
...



You don''t need to do anything. The code above already initializes all
field in all structs to zero. If your compiler doesn''t do that, it''s broken.

--
Best regards,
Andrey Tarasevich



Andrey Tarasevich wrote:

Jayw710 wrote:

...
i have a struct

struct PTStruct
{
int x;
int y;
};

class abc
{
public:
abc();
...

std::vector<PTStruct> mypoints;
}

void abc::abc() : mypoints(100)
''void''??? Constructors have no return type. Should be just

abc::abc() : mypoints(100)

{

}
and i''m trying to get all the PTStruct variables (x and y) in mypoints to be initialized to zero, but I''m screwing up the syntax somehow.

Any suggestions how this should be done?
...



You don''t need to do anything. The code above already initializes all
field in all structs to zero. If your compiler doesn''t do that, it''s


broken.



Doesn''t it depend where the vector gets its memory from?


这篇关于在向量中初始化结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆