Howto create combinations of several vectors without hardcoding loops in C?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If the number of input vectors can change, hardcoded nested loops are the wrong tool. The generic problem is the cartesian product of several collections: pick one element from each vector and emit every possible result.
In C++, the cleanest solution is usually recursion or backtracking over a std::vector<std::vector<T>>. That way the algorithm depends on the data shape, not on how many for loops you wrote by hand.
Model the Input as a Vector of Vectors
Instead of naming each vector separately, store them in one outer container:
Now the code does not care whether there are three groups or thirty. That is the key design shift.
Generate the Cartesian Product Recursively
The usual pattern is to keep a partially built combination and extend it one group at a time:
This prints every possible combination while using only one recursive function.
Make It Reusable with a Callback
Real programs rarely want to print combinations directly. A callback makes the generator reusable:
Now the caller decides whether to print, store, or score each combination.
Think About Empty Inputs
You should decide up front how the algorithm behaves in edge cases:
- if one inner vector is empty, there are no combinations
- if the outer collection is empty, some codebases treat that as one empty combination
The exact convention depends on your application, but it should be explicit rather than accidental.
For example, a quick guard for empty inner groups can prevent wasted work:
Remember the Size Can Explode
Generic code removes the hardcoded loops, but it does not remove combinatorial growth. If you have five vectors of size ten, the result count is already one hundred thousand. At ten vectors of size ten, the output becomes enormous.
That means the real performance strategy is often:
- stream combinations one at a time
- stop early when possible
- avoid storing every result unless you genuinely need all of them
The callback form helps with this because it lets you process each combination as it is produced.
Common Pitfalls
- Writing nested loops tied to the current number of vectors.
- Forgetting to represent the input as a single collection of collections.
- Storing every combination in memory when a streaming visitor would be enough.
- Ignoring empty-vector edge cases.
- Assuming a generic implementation changes the underlying combinatorial cost.
Summary
- Treat the problem as a cartesian product over a
std::vector<std::vector<T>>. - A recursive backtracking function is the simplest generic solution in C++.
- Use a callback when the caller needs to process combinations instead of printing them.
- Handle empty inputs deliberately.
- Keep in mind that the number of combinations can grow very quickly even with clean code.

