Solved

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

Posted on 2011-03-08
8
491 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now