Creating a list with sublists in c ++

1

I'm trying to create a list with the sublists (20.0 20.0 0.0) (36.0 150.0 0.0) (200.0 130.0 0.0) (215.0 20.0 0.0) in C ++ and would like help because I do not know how to join the 4 to form a list. I need something like append in LISP.

    
asked by anonymous 27.03.2017 / 19:03

1 answer

1
#include <iostream>
#include <vector>

typedef std::vector<double> reals_list;

int main()
{
    std::vector<reals_list> list_of_lists;

    list_of_lists.push_back({20.0, 20.0, 0.0});
    list_of_lists.push_back({36.0, 150.0, 0.0});
    list_of_lists.push_back({200.0, 130.0, 0.0});
    list_of_lists.push_back({215.0, 20.0, 0.0});

    std::cout << list_of_lists[3][0]
        << ", " << list_of_lists[3][1]
        << ", " << list_of_lists[3][2] << std::endl;
}

I could not help but notice that the internal list always has 3 elements. If this is the case, I recommend creating a class or fixed-frame structure, which performs much better than std::vector , which is a generic vector of arbitrary size. Example:

#include <iostream>
#include <vector>

struct Vector3 {
    double x, y, z;
};

int main()
{
    std::vector<Vector3> list_of_lists;

    list_of_lists.push_back({20.0, 20.0, 0.0});
    list_of_lists.push_back({36.0, 150.0, 0.0});
    list_of_lists.push_back({200.0, 130.0, 0.0});
    list_of_lists.push_back({215.0, 20.0, 0.0});

    std::cout << list_of_lists[3].x
        << ", " << list_of_lists[3].y
        << ", " << list_of_lists[3].z << std::endl;
}
    
27.03.2017 / 20:15