Sets Equivalent in cpp

Oct 26, 2020 at 2:27pm
What is the equivalent cpp code for the below python code snippet.

1
2
3
  //Here the left points is the list of objects.
  left_shapes = list(set(left_points))
  left_shapes.sort()

Oct 26, 2020 at 3:59pm
c++ has sets but they are not exactly what you think of from math (same as how vectors are not math vectors).
you can sort a c++ list without a set. What purpose does the set do for py? In c++ it could be used to remove duplicates, or other things, but it would be an unnecessary data copy from one container to another in most scenarios.
Oct 26, 2020 at 3:59pm
In c++ std::set exists. See:

http://www.cplusplus.com/reference/set/set/?kw=set

They are already sorted.
Oct 26, 2020 at 4:06pm
good point, you could just not have the list, and go directly to set. But it depends on what you are trying to do beyond the 2 lines.
Oct 26, 2020 at 5:16pm
Sets aren't ordered in python.

As Jonnin says, it depends what happens beyond those two lines. But you will have to define an ordering operation (which, given the names, might be non-trivial).
Oct 26, 2020 at 6:57pm
The purpose of sets here is to remove duplicates @jonnin
In the next step I am iterating the left_shapes and checking for the if condition. @lastchance
Oct 26, 2020 at 7:07pm
In the next step I am iterating the left_shapes and checking for the if condition.


I don't know what "checking for the if condition" means. Why not show us the next step rather than just describing what it does?

A C++ std::set will (1) get rid of duplicates and (2) order the elements. So, it looks like that's what you need.
Oct 26, 2020 at 7:22pm
What are left_shapes, and how are they ordered? There needs to be some sort of well-defined ordering between the shapes.
Last edited on Oct 26, 2020 at 7:22pm
Oct 26, 2020 at 9:54pm
if you need to remove dupes, using a set up front and never fooling with the list sounds like a possible way to build it.
Topic archived. No new replies allowed.