Link to home
Create AccountLog in
Avatar of panJames
panJames

asked on

std::vector<std::vector<int>> linesGrid(64, std::vector<int>(48));

Hello experts,

std::vector<std::vector<int>> linesGrid(64, std::vector<int>(48));

Trying to have a good understanding of this line of code so opened a new question for it.

I am looking at cplusplus.com and do not really get a good grip of this problem.

Here are constructors:

1. explicit vector ( const Allocator& = Allocator() );

2. explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );

3. template <class InputIterator>
         vector ( InputIterator first, InputIterator last, const Allocator& = Allocator() );

4. vector ( const vector<T,Allocator>& x );

Now I try to understand these constructors.

ad 1. Let's start with the first one (once I get this one, it should help with the others!)

"const Allocator& = Allocator()"

What does it mean?

Thank you

panJames
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
This question seems to be about understanding that line of code, not about finding alternative ways of doing it ...
Avatar of phoffric
phoffric

If you have a good debugger (such as VS 2010), then you can step through the program and observe carefully the differences in how matrix1 and matrix2 are constructed and filled.
Avatar of panJames

ASKER

std::vector<std::vector<int>> linesGrid(64, std::vector<int>(48));
explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );

>> how do you translate this constructor into our example?

"std::vector<std::vector<int>>" is "vector"?
"( size_type n, const T& value= T(), const Allocator& = Allocator() )" is "(64, std::vector<int>(48))" ?

where is "linesGrid" then?

panJames





Do you know what constructors are, and how they are used ?

Have a look at this tutorial on using classes in C++ to get a better understanding :

        http://www.cplusplus.com/doc/tutorial/classes/
        http://www.cplusplus.com/doc/tutorial/classes2/



In something like :

        Type object(argument1, argument2);

Type is the type of the object we want to create, object is the actual instance we create, and argument1 and argument2 are the arguments that are passed to the constructor.
<Infinity08>
In something like :

        Type object(argument1, argument2);

Type is the type of the object we want to create, object is the actual instance we create, and argument1 and argument2 are the arguments that are passed to the constructor."
</Infinity08>

Somehow I thought that:

Type object(argument1, argument2);

object needs to have the same name as class to "work" as constructor.

panJames

>> object needs to have the same name as class to "work" as constructor.

No. The class is mentioned in Type already.