class array to list

Started by
11 comments, last by Absurdon 7 years, 9 months ago

Hi

the code below is ok but I wonder how to push an array of the Ausfaelle class to list and read it back?

Many thanks.

int mListe ()
{
std::list<Ausfaelle> points;

points.push_back(Ausfaelle(1,1));

std::list<Ausfaelle>::iterator iter;

for(iter = points.begin(); iter != points.end(); ++iter)
{
Ausfaelle test = *iter;
std::string s = std::to_string(test.active);
char const *pchar = s.c_str();
XPLMDebugString(pchar);
}

return 0;
}

Advertisement

You iterate your array and add each element to your list. Alternatively, you may want to check signatures (4) and (5) here

Thanks. How should that look for a 2 dimensional array like:

Ausfaelle Ausfaelle_Line[100]={
{ 1, 2, 0, "OUTSID", 1, "EXTERNAL CHECKS PAGE 3", 0, 1, "sim/cockpit2/annunciators/passenger_oxy_on", NULL },

{ 1, 2, 0, "OUTSID", 1, "EXTERNAL CHECKS PAGE 3", 0, 1, "sim/cockpit2/annunciators/passenger_oxy_on", NULL },

{ 1, 2, 0, "OUTSID", 1, "EXTERNAL CHECKS PAGE 3", 0, 1, "sim/cockpit2/annunciators/passenger_oxy_on", NULL },

...

...

}

You shouldn't really be using this kind of arrays though. You may also want to take a look at range-based for.

That's what I just tried but no luck:

std::list<Ausfaelle>line[10];
for( int x = 10; x < 20; x = x + 1 )
{
line[x].push_back(Ausfaelle(bremsen,1));
}

std::list<Ausfaelle>::iterator iter;
for( int y = 10; y < 20; y = y + 1 )
{
for(iter = line[y].begin(); iter != line[y].end(); ++iter)
{
Ausfaelle test = *iter;
std::string s = std::to_string(test.active);
char const *pchar = s.c_str();
XPLMDebugString(pchar);
//std::cout << test.active << "; ";
}

}

Well, my answer assumed that you want to add an array of Ausfaelle to your existing list. The code you have now does not work because on the first iteration you're attempting to access line[10], which does not exist. It might be better explaining what the actual problem is and what you're trying to achieve instead of throwing stuff against the wall and then wondering why they don't stick.

First things first: you can't access line[10] or anything higher than 10 because line has only 10 elements and counting starts from 0 so it only has indicies from 0 to 9. So never access indicies higher than 9 because even if it might seem to work it is accessing some random memory there, could be anything.

Other than that from what I understood u want to push arrays to the list. Am i right? There is multiple ways to do that depending on what information you have on the size of the arrays. If they are fixed size you can simply do std::list<Ausfaelle*> and make a list of pointers instead. Now when initialising the array you do Ausfaelle* temp = new Ausfaelle[size_of_the_array] and then push_back that pointer. If however the arrays are not of fixed size you will need to do things a bit different. You could either do a small struct as a helper:


struct AusfaelleArray {
    Ausfaelle* start;
    size_t array_size;
}

and make a list of that or you can make a list of std::vector<Ausfaelle> so it's std::list<std::vector<Ausfaelle>> line;

Initializing that would be like this:


AusfaelleArray element = {0}; // Initializing the struct with zero (0) to avoid errors by memory faults
element.start = new Ausfaelle[size_of_array]; // creating the array on the heap. don't forget to delete later
element.array_size = size_of_array; 

When you want to access that then you can iterate through your list as normal and when accessing you can just do:


for(iter = line.begin(); iter != line.end(); ++iter)
{
    std::vector<Ausfaelle> temp = *iter; // Use AusfaelleArray if you decided to go that way
    for(int i = 0; i < temp.size(); i++) { // if u gone the other way instead of temp.size() use temp.array_size
        Ausfaelle test = temp[i];
        // do your stuff
    }
}

Please don't just copy and paste over as it's been a while since i worked with C++ and I'm not 100% sure if the code works exactly as stated. It should give you a hint on the conept though.

Be aware that when allocating memory on the heap with new you need to delete it manually aswell.

As of how you are doing it now you make an array of std::list<Ausfaelle> with 10 elements (10 lists) in it. if you want it that way arround you need to be aware that your array as it is now has exactly 10 elements and will always have unless you create a new one and copy the old one over. Again you can use the std::vector instead that automatically does that for you in the background. But you will still need to use indicies that are in range of the array/vector.

I hope that helped you a bit finding a solution to your problem. If you still have issues I suggest you give a more percise description of what you want to do, because it makes it easier for people to help you.

Assuming you have this array:




Ausfaelle Ausfaelle_Line[]={
{ 1, 2, 0, "OUTSID", 1, "EXTERNAL CHECKS    PAGE 3", 0, 1, "sim/cockpit2/annunciators/passenger_oxy_on", NULL },
{ 1, 2, 0, "OUTSID", 1, "EXTERNAL CHECKS    PAGE 3", 0, 1, "sim/cockpit2/annunciators/passenger_oxy_on", NULL },
{ 1, 2, 0, "OUTSID", 1, "EXTERNAL CHECKS    PAGE 3", 0, 1, "sim/cockpit2/annunciators/passenger_oxy_on", NULL },
}



If you want to add copies of each Ausfaelle to a list, you do:


std::list<Ausfaelle> points;
// Copy from beginning to end of Ausfaelle_Line, and push onto the back of the "points" list:
std::copy(std::begin(Ausfaelle_Line), std::end(Ausfaelle_Line), std::back_inserter(points));

If, instead, you want pointers to the Ausfaelle objects in Ausfaelle_Line (an important distinction), you can do:


    std::list<Ausfaelle*> pointsPointers;
    // Using std::transform here, because the source type is Ausfaelle, but the destination type is Ausfaelle*
    std::transform(std::begin(Ausfaelle_Line), std::end(Ausfaelle_Line), std::back_inserter(pointsPointers),
        [](Ausfaelle &thing) { return &thing; } // "convert" Ausfaelle& to Ausfaelle*
        );

The distinction here is different. Are you wanting to work on copies of the objects in the original array? Or actually references to them?
Other questions you need to think about:
- what is the lifetime of this Ausfaelle data? If you're copying pointers to the objects into a list, you need to ensure the original objects are kept alive as long as you are using that list. Not a problem in the case when you have a static array you've copied from - but if they might come from other sources, you need to consider this
- Why are you using a list, and not a vector?
I might also note that the c++ containers support initializer lists, so you can do this directly:

    std::list<Ausfaelle> Ausfaelle_Line = {
        { 1, 2, 0, "OUTSID", 1, "EXTERNAL CHECKS    PAGE 3", 0, 1, "sim/cockpit2/annunciators/passenger_oxy_on", NULL },
        { 1, 2, 0, "OUTSID", 1, "EXTERNAL CHECKS    PAGE 3", 0, 1, "sim/cockpit2/annunciators/passenger_oxy_on", NULL },
        { 1, 2, 0, "OUTSID", 1, "EXTERNAL CHECKS    PAGE 3", 0, 1, "sim/cockpit2/annunciators/passenger_oxy_on", NULL },
    };
... if that's what you want...

Thanks. Now first I try to keep it simple. First push and second read values:

std::list<Ausfaelle>line[10];
for( int x = 0; x < 10; x = x + 1 )
{
line[x].push_back(Ausfaelle(x,1,22,bremsen));
}

if (mldg == 0){
std::list<Ausfaelle>::iterator iter;
for( int y = 0; y < 10; y = y + 1 )
{
for(iter = line[y].begin(); iter != line[y].end(); ++iter)
{
Ausfaelle test = *iter;
if (test.drValue == 1){

...

-------

Ausfaelle::Ausfaelle(int a, int b, int c, int d) {
a = index;
b = active;
c = color;
d = drValue;
}

test.drValue is alway 0 so I need to find my errors.

Thanks


Ausfaelle::Ausfaelle(int a, int b, int c, int d) {
    a = index;
    b = active;
    c = color;
    d = drValue;
}


You are assigning to local variables (the parameters to the constructor) and everything you do there is lost when the function returns.

This topic is closed to new replies.

Advertisement