This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Create a Point from a C++ Vector

0 votes

Is there a way to create a C++ Point directly from a std::vector without first creating an Array?

More importantly, can the coordinates be output from a Point directly to a std:vector?

asked May 20, 2017 by pf4d FEniCS User (2,970 points)
edited May 21, 2017 by pf4d

1 Answer

0 votes
 
Best answer
  1. You can use Point's constructor accepting a dim and a a raw array, passing it your vector's data:

    std::vector<double> vec {1, 2, 3};
    Point p(3, vec.data());
    
  2. This one is harder. You basically need to alias your buffer telling std::vector to use the memory that Point already allocated. You can achieve this with a custom allocator, but the amount of boilerplate grows pretty fast. See e.g. this question on StackOverflow.

answered Jun 7, 2017 by mdbenito FEniCS User (4,530 points)
selected Aug 11, 2017 by pf4d
...