tags:

views:

213

answers:

3

This looks simple but I am confused: The way I create a vector of hundred, say, ints is

std::vector<int>  *pVect = new std::vector<int>(100);

However, looking at std::vector's documentation I see that its constructor is of the form

explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );

So, how does the previous one work? Does new call the constructor with an initialization value obtained from the default constructor? If that is the case, would

std::vector<int, my_allocator> *pVect = new std::vector<int>(100, my_allocator);

where I pass my own allocator, also work?

+3  A: 

To (perhaps) clarify:

To create a vector object called v of 100 elements:

std::vector <int> v( 100 );

this uses the vector constructor that takes the size (100) as the first parameter. To create a dynamically allocated vector of 100 elements:

std::vector <int> * p = new std::vector<int>( 100 );

which uses exactly the same constructor.

anon
Thanks, my original post was somewhat garbled, corrected that now. My question is: how do I know that the other third parameters of teh constructor can be skipped, because the vector ctor actually has three parameters? And, why is it OK to skip the 2nd and 3rd arguments (defaults are used) but not just the 2nd one? Thanks.
recipriversexclusion
Pavel Radzivilovsky
+10  A: 

You are doing it all wrong. Just create it as an automatic object if all you need is a vector in the current scope and time

std::vector<int> pVect(100);

The constructor has default arguments for the second and third parameters. So it is callable with just an int. If you want to pass an own allocator, you have to pass a second argument since you can't just skip it

std::vector<int, myalloc> pVect(100, 0, myalloc(some_params));

A dedicated example might clarify the matter

void f(int age, int height = 180, int weight = 85);

int main() { 
  f(25); // age = 25, height and weight taken from defaults.
  f(25, 90); // age=25, height = 90 (oops!). Won't pass a weight!
  f(25, 180, 90); // the desired call.
}
Johannes Schaub - litb
My question exactly: Why can you skip the second argument in the first case and not the second? Why doesn't the ctor handle the second value as it did in the first case?
recipriversexclusion
@recipriversexclusion: The second and third parameters have default arguments so that you can call the constructor with one, two, _or_ three arguments. If you want to pass an argument to the third parameter, you need also to pass an argument to the second parameter (how else would the compiler know that you meant "skip the second parameter and give this value to the third?").
James McNellis
@recip because it won't make arbitrary decisions on its own. Argument passing works by equating the positions of arguments and parameters, not by equating their types. Imagine that you create a vector of `float`, and that your allocator can be created by an int (conversion). Then in `vector<float, myalloc> v(10, 5)` the compiler can't just say "instead of passing the 5 to the second parameter as default value of the 10 elements to create, i'll pass it to the third parameter, converting it to an allocator". That just wouldn't work. How could you be able to clearly state your desire then?
Johannes Schaub - litb
Johannes, in your example all the parameters have the same type so there's no way the compiler can deduce which is which when you say `f(25, 90)`; however, in `vector`'s ctor they're all different, so why can't the compiler know which parameter I'm skipping and replace it with the default?
recipriversexclusion
Oh, I see, you've already answered my question. James and Johannes, this was very helpful, thanks a lot for your answers.
recipriversexclusion
A: 

You are creating a vector of hundred elements. As you can see in the second code sample you posted:

explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );

This constructor takes the amount of elements to put into vector and avalue which will be inserted into vector n times. If you do not specify a value a value is constructed using default constructor of your vector type T. Here it would be the "default" constructor of int which initializes it to 0 (there is no such thing as a default constructor of int, however C++ standard says that int is initialized to 0 in such cases).

pajton