Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-08
8
Medium Priority
?
518 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

721 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