Multi‑Dimension Containers
1. What is a "multi‑dimension container"?
In C++, there is no special syntax or built‑in type for multi‑dimension containers. Instead, you create them by nesting containers inside containers.
For example:
- a container of containers → 2D
- a container of containers of containers → 3D
- a container of containers of containers of containers → 4D
This works with any container: ::fast_io::vector,
::fast_io::string, ::fast_io::array, or your own types.
In practice, ::fast_io::vector<T> is the default container.
It grows automatically and is the easiest to use for multi‑dimension structures.
2. 2D containers (containers of containers)
A 2D container is simply a container where each element is itself a container.
Example: 2D vector
#include
#include
int main() {
using namespace ::fast_io::iomnp;
// A vector where each element is another vector
::fast_io::vector<::fast_io::vector> grid{
{1zu, 2zu, 3zu},
{4zu, 5zu, 6zu},
{7zu, 8zu, 9zu}
};
println(grid[1][2]); // prints 6
}
Example: vector of strings
A vector of strings is also a 2D container:
- outer dimension → list of strings
- inner dimension → characters inside each string
::fast_io::vector<::fast_io::string> lines{
"Hello",
"fast_io",
"world"
};
println(lines[0][1]); // 'e'
Example: array of vectors
#include
#include
#include
int main() {
using namespace ::fast_io::iomnp;
::fast_io::array<::fast_io::vector, 3zu> buckets{
::fast_io::vector{1zu, 2zu},
::fast_io::vector{3zu},
::fast_io::vector{4zu, 5zu, 6zu}
};
println(buckets[2][1]); // prints 5
}
3. 3D containers (containers of containers of containers)
A 3D container is simply one more level of nesting.
#include
#include
int main() {
using namespace ::fast_io::iomnp;
::fast_io::vector<
::fast_io::vector<
::fast_io::vector
>
> cube{
{
{1zu, 2zu, 3zu},
{4zu, 5zu, 6zu}
},
{
{7zu, 8zu, 9zu},
{10zu, 11zu, 12zu}
}
};
println(cube[1][0][2]); // prints 9
}
You can think of this as:
- 2 “layers”
- each layer has 2 “rows”
- each row has 3 “columns”
4. 4D containers (containers of containers of containers of containers)
A 4D container is just one more level of nesting beyond 3D.
#include
#include
int main() {
using namespace ::fast_io::iomnp;
::fast_io::vector<
::fast_io::vector<
::fast_io::vector<
::fast_io::vector
>
>
> hypercube{
{
{
{1zu, 2zu},
{3zu, 4zu}
},
{
{5zu, 6zu},
{7zu, 8zu}
}
},
{
{
{9zu, 10zu},
{11zu, 12zu}
},
{
{13zu, 14zu},
{15zu, 16zu}
}
}
};
println(hypercube[1][0][1][1]); // prints 12
}
This structure has:
- 2 blocks
- each block has 2 layers
- each layer has 2 rows
- each row has 2 columns
5. Iterating through multi‑dimension containers
You simply nest loops the same way you nest containers.
for(auto &row : grid) {
for(auto &x : row) {
print(x, " ");
}
println();
}
This pattern works for any depth.
Key takeaways
- C++ has no built‑in multi‑dimension containers.
- You create multi‑dimension structures by nesting containers.
- Container of containers → 2D; container of containers of containers → 3D; and so on.
- You can freely mix
vector,string, andarray. ::fast_io::vector<T>is the default container and the most flexible choice.- Nesting works because containers behave like regular values that can be stored inside each other.