Solved

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

Posted on 2011-03-08
8
506 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:panJames
  • 4
  • 2
  • 2
8 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 350 total points
ID: 35070841
>> 2. explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );

This is the one getting used in that line of code for both constructor calls.


>> "const Allocator& = Allocator()"
>> 
>> What does it mean?

It's used if you want to specify a custom memory allocator. In most cases, you don't need this, and the default (Allocator()) is just fine.
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 150 total points
ID: 35070901
Here are two different ways to construct your 64 x 48 matrix. In matrix1, all the space is allocated, so you can immediately use 2d subscripting to assign values. In matrix2, only 64 rows are defined; but each row has no space allocated. In this case, you cannot use 2d subscripting; instead, you use the push_back method on an individual row to allocate space and set a value.
typedef vector<int> Row;
typedef vector<Row> Matrix;
  // Matrix is just an array (vector) of rows, where each row consists of an array of ints

int main() {
   const int nRows = 64;
   const int nCols = 48;
   Matrix matrix1( 64, Row(48) );
   
   // since the entire matrix is allocated, you can say use 2d indexing:
   for( int k=0; k < nRows; ++k ) {
      for( int j=0; j < nCols; ++j ) {
         matrix1[k][j] = 2*k+j;
      }
   }
   
   Matrix matrix2(64) ; // 64 rows defined; but each has no space yet

   for( int i=0; i < nRows; ++i) {
      for( int n=0; n < nCols; ++n ) {
         matrix2[i].push_back(n+i);
      }
   }
}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35070919
This question seems to be about understanding that line of code, not about finding alternative ways of doing it ...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:phoffric
ID: 35070920
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.
0
 

Author Comment

by:panJames
ID: 35070961
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





0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35070994
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.
0
 

Author Comment

by:panJames
ID: 35071148
<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

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35071196
>> object needs to have the same name as class to "work" as constructor.

No. The class is mentioned in Type already.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question