Creating a list with c++ sublists

Asked

Viewed 257 times

1

I’m trying to create a list with 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 I would like some help because I don’t know how to put the 4 together to form a list. I need something like append on LISP.

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 couldn’t help but notice that the internal list always has three elements. If this is the case, I recommend creating a fixed plot class or structure, which has a much better performance than the 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;
}
  • I didn’t even call to speak before, but in case I’m doing this program using the FDT library to program in a CAD.

  • I don’t know. I don’t know how this information would affect my response.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.