I understand what vector<double> hour {-7777. -7777 ..., - 7777};
means and what vector<double> hour(24, -7777);
means but what exactly this vector without name is supposed to do? vector<double>(24, -7777)
I checked out the 1st line of code and of course it worked fine like 2nd and 3rd code would but i dont understand what exactly is happening there.
Also i dont understnd why B.Stroustrup chosen to write this vector<double> hour { vector<double>(24, -7777) }; into his "programming principles and practice using C++" instead of much easier to understand and as far as i see doing the same vector<double> hour(24, -7777);
Help please!
Ill quote what he said
Why didnt we write
1 2 3
struct {
vector<double> hour {24, -7777};
};
That would have been simpler , but unfortunately, we would have gotten a vector of two elements (24 and -7777). When we want to specify the number of elements for a vector for which an integer can be converted to the element type, we unfortunately have to use the () initializer syntax
So looked like he didnt want to use () for some reason but he used them anyway using even more complicated syntax for beginner to understand vector<double> hour { vector<double>(24, -7777) };
My limited understanding is if we would use this code above it wouldnt even work coz type of the hour vector is wrong- we should use vector<vector<double>> hour{ vector<double>(24, -7777) }; instead.
But as i said i checked it and it works fine so im super confused =/
This will create a temporary object of type vector<double > (the "unnamed" vector), with 24 elements of value -7777.0, and invoke the vector copy constructor to create an object named hour, of type vector<double>, etc.
P.S. may have misread your question and therefore given a non-answer, sorry...
hI vector<double>(24, -7777)is an Rvalue vector containing 24 double values{-7777} , so what happens in this statement vector<double> hour { vector<double>(24, -7777) }; is that your vector hour uses the vector<T>(vector<T>&&) constructor to move the values from the anonymous vector object into it's storage.
You use parenthesis in constructions to specify the number of elements you intend to store in your container {true for most sequential containers} so if he used the curly braces
i.e. {24,-7777} this would invoke the vector<T>(std::initializer_list<T>&&)
constructor instead creating a vector object containing only two values.