C++ explode function
The C++ Standard Template Library provides a type of dynamically allocated array, a vector. We should be able to build a function that returns a vector of strings.
Note, because we wanted to stick with mirroring the same parameters that php uses, we had to return the vector instead of a reference to an existing vector. This is not be very efficient because the entire vector is copied (copy constructor) when it is returned and assigned to v. This is because the vector is allocated on the stack within the explode function. When the function completes, and the function is popped off the stack, the vectors is de-allocated, and so when we return the vector, we implicitly call the copy constructor of the vector which in turn calls the copy constructor of string in the vector, which is avoidable.
How is it avoidable or how can we make it more efficient? By passing in a reference to an existing vector of strings. We did not do that in this example because that would violate the requirements of this function (mirror the php function). There is another way around it, and that is to use the new keyword to allocate a vector on the heap. The only problem with this, is that you have to explicitly delete what is eventually returned in order to avoid memory leaks. Also, it violates the recommended practice, of putting 'new' and its corresponding 'delete' at the beginning and end of the same function.