Solved

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

Posted on 2011-03-08
8
501 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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 …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

839 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