Is the initialization order of the vector elements guaranteed by the standard?
It is not guaranteed that the elements are initialized in the order of their indices. In C++11, see [vector.cons]/3:
Effects: Constructs a
This says nothing about the ordering, so nothing can be assumed. The wording changes in later editions of the standard, but no ordering ever seems to have been imposed.
On your first question, the
C++20 (but this goes back to
C++11 as well) section dealing with the
vector sequence container makes no promises about the order in which elements are constructed within the vector itself, only that the elements are set to some specific value:
Effects: Constructs a
Nothing at all about the order in that (very brief) section(a).
But you have a bigger problem with your method, specifically I don't think you really want to go out to
cin for every case where you're default constructing a point variable.
There may be, for example, cases where you need a default-constructed temporary variable and it's going to be seen as a hang if your program suddenly stops to accept user input, especially without some prompt to the user :-)
That makes your second question moot but, assuming you're concerned about inefficiencies in initialising vector elements then changing them with an input loop, I wouldn't be. The structure with no constructor (i.e., just a couple of
int variables) doesn't need to initialise them(b), so a vector of them can just do the allocation and stop there.
(a) Some order is guaranteed in the standard, such as the order of disparate members within a class, or the order of elements within an array. However, the elements of a vector are neither of those things.
(b) This is covered in
C++20 10.9 Iniitialisation [class.init]:
When no initializer is specified for an object of (possibly cv-qualified) class type (or array thereof), or the initializer has the form
(), the object is initialized as specified in
C++20 9.3 Initializers [dcl.init]:
To default-initialize an object of type
Tis a (possibly cv-qualified) class type, constructors are considered. The applicable constructors are enumerated, and the best one for the initializer
()is chosen through overload resolution. The constructor thus selected is called, with an empty argument list, to initialize the object.
Tis an array type, each element is default-initialized.
- Otherwise, no initialization is performed.
It's that first bullet point that kicks in for a type with no constructors explicitly defined or inherited. In that case, it uses the implicitly-defined constructor which is equivalent to a user defined constructor with no body and no initialiser list.