Hi there

I need help writing a data structure to hold and assign data to a particular unique ID.

I think I want an vector of structures:

    struct Data {
        int ID;
        double latitude;
        double longitude;
        int speed;
        int altitude;
      };

I receive messages in the above format, they can come in a mixture BUT each one will have a Unique ID at the beginning of the message.
Example:

56885 53.56 -8.5 //ID LAT LONG
48569 52.5 -0.3 //ID LAT LONG
56885 2000 //ID Altitude
56885 21 //ID MPH

I think I need to do 2 things;
-Go through the structure until I find the ID and then add the corresponding data to the structure
-If not found, create a new struct and add the ID.

Any help would be greatly appreciated

Many Thanks

Will your ID determine which of the two types the struct value is? Or do you have special guard values? I'd first consider if storing them like this is what you want. (You could use two containers) Do you need the type-specific values, or it is only the ID you're interested in?

If you actually need the values, maybe some sort of inheritance solution could prove useful. Maybe a union, like:

// No idea what your objects represent, so calling them Foo and Bar for now..
struct Foo
{
    double latitude;
    double longitude;
};

struct Bar
{
    int speed;
    int altitude;
};

union FooBar
{
    Foo foo;
    Bar bar;
};

struct Data
{
    int ID;
    FooBar foobar;
};

Also, this line confused me a little:

Go through the structure until I find the ID and then add the corresponding data to the structure

So you already known the ID's beforehand, but not the data? "If not found, create a new struct and add the ID." I assume this is always the case at the start right? You just continuously update the values belonging to ID's?

TL;DR: Need more information.

-edit-
Reading your post there are actually 3 types of objects, not 2. Are there more? (Or any mixtures of them?)
Also as memory usage probably isn't an issue it's maybe better to not use a union.

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.