An object is a data structure that encapsulates some part of your program’s functionality. An object represents a discrete unit of functionality that can be reused independently of the rest of the program. C++ has a very convenient feature called object initialization, which allows us to initialize an object with a bunch of initial values, so that it can be created in one shot rather than having to create each individual field separately.
You can initialize vectors with data in C++. Initializer lists make it much easier to create initializers for complex types such as vectors.
The first line of Listing 2-5 uses the standard vector constructor to create a vector of size 2. The second line of Listing 2-5 creates a vector of size 3, using a special constructor that takes three arguments.
Here’s the code you’ll need to make that happen. The code in Listing 2-5 is used to replace the HTML for each product listing page.
Small C++ Program on Constructing vector Objects
#include
#include
using namespace std;
int main()
{
using MyVector = vector;
MyVector vectorA( 1 );
cout << vectorA.size() << " " << vectorA[0] << endl;
MyVector vectorB( 1, 10 );
cout << vectorB.size() << " " << vectorB[0] << endl;
return 0;
}
The integer value of the vectorA variable will be initialized with a single 0.
You’d expect this book to contain a single integer containing 1 but this would be wrong.
The first parameter to the constructor for a vector is used to tell it how many values the vector will have and this will be one in this example. The second parameter tells it where the values are going to live.
If you had expected vectorB to contain two values, one for each of these two categories, you would have been disappointed because what you have here is a vector that has only one element in it 10. and that’s the VectorA is created using the same constructor as vectorB but it specifies a value to be used to instantiate the members of the vector.
#include
#include
using namespace std;
int main()
{
using MyVector = vector;
MyVector vectorA( 1 );
cout << vectorA.size() << " " << vectorA[0] << endl;
MyVector vectorB( 1, 10 );
cout << vectorB.size() << " " << vectorB[0] << endl;
MyVector vectorC{ 1, 10 };
cout << vectorC.size() << " " << vectorC[0] << endl;
return 0;
}
You can see that the first vector contains a single element and its value is 0 However the second vector contains two elements and both of its elements are the same. So, their values are added up to become the second vector’s value of the third vector is constructed using uniform initialization and its first element has the value one.
The value of the second element will be 10. If you do not take care to ensure that the correct type of initialization is being used with your types, you risk generating a significant difference in the behavior of your programs.
Listing 2-7 shows a more explicit use of the initializer_list to construct a vector.
Small C++ Program on Explicit initializer_list Usage
#include
#include
using namespace std;
int main()
{
using MyVector = vector;
MyVector vectorA( 1 );
cout << vectorA.size() << " " << vectorA[0] << endl;
MyVector vectorB( 1, 10 );
cout << vectorB.size() << " " << vectorB[0] << endl;
initializer_list initList{ 1, 10 };
MyVector vectorC(initList);
cout << vectorC.size() << " " << vectorC[0] << endl;
return 0;
}
The code in Listing 2-7 contains an explicit initialization list, which is used to construct a vector.
Here’s an example of creating a vector using uniform initialization. You normally wouldn’t need to explicitly write code like this. However, you’ll need to understand what happens when the compiler uses uniform initialization to create a vector.