When returning a range of unknown size, I usually use an output iterator. For instance, a pathfinding algorithm that returns all the points in the shortest path would look like:
template <typename OutputIterator>
void search(const float3 &start, const float3 &end, OutputIterator output);
This is great because the user (having more information) can use a deque, or vector, or reuse the storage from the previous call, etc...
But how would one deal with the output if you were returning an unknown number of paths, each with an unknown number of points in the path?
To put it in more concrete terms, I have a function that will try to calculate any number of routes that the user requests. I could easily just require them to make a vector of vectors, but I would like for the user to retain the flexibility to specify storage.
// Calculate 10 different routes from start to end.
vector<vector<float3> > paths;
multiple_route_search(start, end, 10, paths);
might be the ideal syntax, but would require multiple_route_search to specialize on lots of different container type combinations.
I also thought about simply returning them all through one output iterator, just like the first example, but putting in some sort of sentinel value (like the last point repeated twice since that should not happen in a path) to signify the start of the next path, but that seems like I'm forcing too much extra work on the user for him to be able to get at each individual path, especially if he wants the 10th path out of 10.
I'd love to hear people's design ideas...
[Edited by - mfawcett on November 20, 2008 1:44:54 PM]