clearing a vector of pointers [duplicate]

Yes, the code has a memory leak unless you delete the pointers. If the foo class owns the pointers, it is its responsibility to delete them. You should do this before clearing the vector, otherwise you lose the handle to the memory you need to de-allocate. You could avoid the memory management issue altogether by … Read more

Convert data.frame column to a vector?

I’m going to attempt to explain this without making any mistakes, but I’m betting this will attract a clarification or two in the comments. A data frame is a list. When you subset a data frame using the name of a column and [, what you’re getting is a sublist (or a sub data frame). If you want … Read more

c++ vector bubble sort

You need to pass by reference; by making a copy, you sort a temporary copy, and then that’s it; it disappears and the original is still unsorted, because, once again, you sorted only a temporary copy of the entire vector. So change vector<int> a to vector<int>& a. Here’s the code, fixed: http://coliru.stacked-crooked.com/a/2f118555f585ccd5

Displaying contents of a vector container in C++

There is an idiomatic way for printing a vector out. This way is safe and doesn’t require you to keep track of the vectors size or anything like that. It is also easily recognisable to other C++ developers. This method works on other container types too that do not allow random access. This works both … Read more

Rotating a Vector in 3D Space

If you want to rotate a vector you should construct what is known as a rotation matrix. Rotation in 2D Say you want to rotate a vector or a point by θ, then trigonometry states that the new coordinates are To demo this, let’s take the cardinal axes X and Y; when we rotate the X-axis 90° counter-clockwise, … Read more