If I want to get the Cartesian Product of these two vector<string>
s:
vector<string> final{"a","b","c"};
vector<string> temp{"1","2"};
But I want to put the result in final
, such that final would contain:
a1
a2
b1
b2
c1
c2
I'd like to do this without creating a temporary array. Is it possible to do this? If it matters, the order of final
is not important.
You may try the following approach
The program output is
This works for me:
This is just a personal preference option to Vald from Moscow's solution. I think it may be faster for dynamic arrays because there would be less branching. But I haven't gotten around to writing a timing test bench.
Given the inputs
vector<string> final
andvector<string> temp
:EDIT:
Nope, this solution is slower not faster: http://ideone.com/e.js/kVIttT
And usually significantly faster, though I don't know why...
In any case, prefer Vlad from Moscow's answer
Try the function cartesian: