Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to slice the structure vector in c++?

I have an data named faces which definition is like this:

struct ivec3 {
    unsigned int v0;
    unsigned int v1;
    unsigned int v2;
};

std::vector<ivec3> faces;

I got the faces with 100 elements(faces.size()=100).
Now I want to get all v0 of faces. If I use the Python, I can do it like this

all_v0 = faces[:, 0]

So, how can I use the slicing operation in C++ like above code of Python?

Thanks very much!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do this with the help of std::transform:

std::vector<int> all_v0;
all_v0.reserve(faces.size());
std::transform(faces.begin(), faces.end(), 
               std::back_inserter(all_v0), 
               [] (const ivec3& i) { return i.v0; });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading