Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-08
8
Medium Priority
?
521 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 1400 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 600 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
Industry Leaders: 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

877 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