Hi All,

Quick question...
I have a structure for a specific type of vector:

struct SParticle {
    float Position;
    float Velocity;
    float FcnVal;
    float LocalBest;
    float LocalBestVal;
} 

But now I need to define 30 objects for this structure. Is there an easy way for doing this or should I just define them one by one?

SParticle object1, object2, ...;

Thanks
Salman

Recommended Answers

All 6 Replies

Hi All,

Quick question...
I have a structure for a specific type of vector:

struct SParticle {
float Position;
float Velocity;
float FcnVal;
float LocalBest;
float LocalBestVal;
}

But now I need to define 30 objects for this structure. Is there an easy way for doing this or should I just define them one by one?

SParticle object1, object2, ...;

Thanks
Salman

Don't define them one by one. Use an array, vector, or something similar to store them. You don't want to hard-code variable names like object1, object2, etc. Do something like this (if you decide to use an array):

SParticle Objects[30];

You mentioned the word "vector" and I'm not sure whether you mean "vector" from a Physics/Math point of view (i.e. value with a direction or similar), or a C++ point of view (data container). It looks like you mean that SParticle is a vector in the mathematical sense or that it represents one. In C++ terminology, SParticle isn't a vector. You could have a vector of SParticles:

vector <SParticle> Objects;

Just wanted to make that note regarding terminology and confirm that when you said the word "vector", you aren't referring to the C++ vector container template:
http://www.cplusplus.com/reference/stl/vector/

Thanks for the information!
Actually, I guess I used the word vector wrong. What I meant is each of those objects needs to be an array by itself.

Don't define them one by one. Use an array, vector, or something similar to store them. You don't want to hard-code variable names like object1, object2, etc. Do something like this (if you decide to use an array):

SParticle Objects[30];

You mentioned the word "vector" and I'm not sure whether you mean "vector" from a Physics/Math point of view (i.e. value with a direction or similar), or a C++ point of view (data container). It looks like you mean that SParticle is a vector in the mathematical sense or that it represents one. In C++ terminology, SParticle isn't a vector. You could have a vector of SParticles:

vector <SParticle> Objects;

Just wanted to make that note regarding terminology and confirm that when you said the word "vector", you aren't referring to the C++ vector container template:
http://www.cplusplus.com/reference/stl/vector/

Thanks for the information!
Actually, I guess I used the word vector wrong. What I meant is each of those objects needs to be an array by itself.

SParticle is a struct, not an array. The way you have defined it:

SParticle object1, object2;

object1 is not an array, so I am confused by this line:

each of those objects needs to be an array by itself.

Any object of type SParticle will not be an array. You can have an array of SParticle objects, which is what I did here earlier:

SParticle objects[30];

objects is an array, but objects[0] is not an array, objects[1] is not an array, objects[2] is not an array, etc. They are each a single object of type SParticle, which is not an array. That said, it sounds to me that what I posted above is what you are looking for. If not, I guess I don't understand what you are trying to accomplish.

Hi,
After I went through your reply, it seems like Structure might not be able to do what I need. I guess I need to look into other options. This is the problem I have:

For my code I need to define about 30 objects. Each of these objects has some attributes associated with it (some of these attributes are arrays themselves). So as an example an object might look something like this:

Object1:
Value [] = {1.2, 2.3, 3, 4.6, 5, 6.0, 7.0, 8.0, 9.0, 10.5}
FcnVal = 3.6
Velocity [] = { 3, 4, 6, 5, 7, 8, 2, 1, 3, 4}
LocalBest = 2.2
LocalBestVal [] = {2.2, 2.3, 4, 5, 6, 7, 1, 2, 3, 5 }

Object2:
...

Object3:
...

and so on...

I need to create 30 of these objects. Can I do this using structure? Do the members in a data structure need to be scalar values (as opposed to arrays)?
Thanks


SParticle is a struct, not an array. The way you have defined it:

SParticle object1, object2;

object1 is not an array, so I am confused by this line:

Any object of type SParticle will not be an array. You can have an array of SParticle objects, which is what I did here earlier:

SParticle objects[30];

objects is an array, but objects[0] is not an array, objects[1] is not an array, objects[2] is not an array, etc. They are each a single object of type SParticle, which is not an array. That said, it sounds to me that what I posted above is what you are looking for. If not, I guess I don't understand what you are trying to accomplish.

Hi,
After I went through your reply, it seems like Structure might not be able to do what I need. I guess I need to look into other options. This is the problem I have:

For my code I need to define about 30 objects. Each of these objects has some attributes associated with it (some of these attributes are arrays themselves). So as an example an object might look something like this:

Object1:
Value [] = {1.2, 2.3, 3, 4.6, 5, 6.0, 7.0, 8.0, 9.0, 10.5}
FcnVal = 3.6
Velocity [] = { 3, 4, 6, 5, 7, 8, 2, 1, 3, 4}
LocalBest = 2.2
LocalBestVal [] = {2.2, 2.3, 4, 5, 6, 7, 1, 2, 3, 5 }

Object2:
...

Object3:
...

and so on...

I need to create 30 of these objects. Can I do this using structure? Do the members in a data structure need to be scalar values (as opposed to arrays)?
Thanks

Perhaps something like this?

struct SParticle
{
    float Value[10];
    int velocity[10];
    float localBestVal[10];
    float fcnVal;
    float LocalBest;
};

SParticle objects[30];

You can have arrays within structs and you can have arrays of structs. SParticle is a struct that has three arrays and two scalars as members. objects is an array of type SParticle. So you have arrays within larger arrays.

Thanks! It's working fine now...

Perhaps something like this?

struct SParticle
{
    float Value[10];
    int velocity[10];
    float localBestVal[10];
    float fcnVal;
    float LocalBest;
};

SParticle objects[30];

You can have arrays within structs and you can have arrays of structs. SParticle is a struct that has three arrays and two scalars as members. objects is an array of type SParticle. So you have arrays within larger arrays.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.