You can declare a multidimensional vector like this:
std::vector< std::vector<int> > v;
You can also use typedef to make code easier to follow:
typedef std::vector<int> int_vector;
typedef std::vector<int_vector> int_matrix;
When writing like in the first example, you should avoid writing the closing angle brackets one after the other to avoid the compiler confuse it with the >>
operator.
You should also avoid returning objects like this from a function because this operation involves copying the whole vector. Instead, you can pass a vector by reference for example:
void process(int_matrix& m)
{
// m.push_back(...)
}
int main(int argc, char* argv[])
{
int_matrix m;
// Initialize m here.
// ...
// Call your methods.
process(m);
// ...
return 0;
}
EDIT:
You can structure your code like this:
// int_matrix.hpp
#ifndef _INT_MATRIX_HPP
#define _INT_MATRIX_HPP
#include <vector>
typedef std::vector<int> int_vector;
typedef std::vector<int_vector> int_matrix;
extern void process(int_matrix& m);
#endif // ~_INT_MATRIX_HPP
.
// int_matrix.cpp
#include "int_matrix.hpp"
void process(int_matrix& m)
{
m.clear();
// ...
}
.
// main.cpp
#include "int_matrix.hpp"
#include <iostream>
int main(int argc, char* argv[])
{
int_matrix m1;
int_matrix m2;
// ...
process(m1);
process(m2);
// ...
return 0;
}