I have a class that declares a vector of vectors in a .h
header file as follows:
#include <vector>
class Mapper {
public:
...
Mapper(const uint num_rows, const uint num_cols, const double initial_val);
...
private:
...
std::vector<std::vector<double>> grid_map_;
...
};
And the definition of the Mapper
class is implemented in a corresponding .cpp
. However, I'm trying to figure out if there's a way to initialize the grid_map_
vector of vectors in the Mapper
constructor using the assignment operator notation similar to what's shown below:
Mapper::Mapper(const uint num_rows, const uint num_cols, const double initial_val) {
...
grid_map_ = std::vector<vector<double>>(num_rows, num_cols, initial_val);
...
}
I know I can initialize it as follows,
std::vector<std::vector<double>> vec(num_rows, std::vector<double>(num_cols, initial_val));
grid_map_ = vec;
or I could also use an initializer list, however, I'm trying to avoid those two methods. Is such a thing possible?
What I'm trying to do is similar to declaring a custom type in the .h
, and then instantiating it in the constructor using assignment operator notation. For example in the header you'd have:
Object object_;
and then in the .cpp
file you'd have:
object_ = Object();
Is initializing a vector of vectors in this way possible?