Trying to default initialize a std::vector
#include <iostream>
#include <vector>
int main()
{
class Options
{
std::vector<int> items{100};
} options ;
std::cout << "Size:" << options.items.size() << " Items[0]:" << options.items[0] << '\n';
return 0;
}
This will print:
Size 1 : Items[0]:100
But that is not what I want, I want the vector to be of size 100.
I managed to do it with
class Options
{
std::vector<int> items{ std::vector<int>(100) };
} options ;
But I get the feeling there must be a "better" way?
Regards,
Specifying a default value for the Vector: In order to do so, below is the approach: Syntax: // For declaring vector v(size, default_value); // For Vector with a specific default value // here 5 is the size of the vector // and 10 is the default value vector v1(5, 10);
Another way to initialize a vector in C++ is to pass an array of elements to the vector class constructor. The array elements will be inserted into the vector in the same order, and the size of the vector will be adjusted automatically. You pass the array of elements to the vector at the time of initialization.
@juanchopanza yes it does default initialised with unsigned() is default initialised with 0 . If you don't want default initialisation don't give a size and call reserve to get initialised space. All the (8) constructors are consistent in that they value (or copy) initialise the vector.
Default member initializer (C++11) [edit] This is the initialization performed when an object is constructed with no initializer.
Following are different ways to create and initialize a vector in C++ STL. Initializing by one by one pushing values : // CPP program to create an empty vector. // and one by one push values. #include <bits/stdc++.h>. using namespace std; int main()
The initialization of an std::vector is also the same as that of std::array. We initialize an std::vector by either of the following ways. We can also assign values to the vector after declaration as shown below. In the above declarations, we stored the marks of 5 students in a vector named marks.
Declaring a vector is different from initializing it. Declaring a vector means creating a new vector while initializations involves passing items into the vector. Every new vector must be declared starting with the vector keyword.
The default value of a vector is 0. // For declaring vector v1 (size); // For Vector with default value 0 vector v1 (5); We can also specify a random default value for the vector. Inorder to do so, below is the approach:
The only way to initialize a class member in the body of the class is to brace or equal initialize it. That means you can do
std::vector<int> items{ std::vector<int>(100) };
or
std::vector<int> items = std::vector<int>(100);
If you don't like either of those options then you can add an initializer to the classes constructor and have
class Options
{
Options() : items(100) {}
std::vector<int> items;
} options ;
but personally I would use the first two options instead.
To avoid any confusion due to differing behavior between
std::vector<int> items(100);
and
std::vector<int> items{100};
use
std::vector<int> items;
items.resize(100); //Default initializes 100 int <=> zero
// or
items.resize(100, 17); //Initializes 100 ints with value 17
Used in a constructor
class Options
{
std::vector<int> items;
public:
Options()
{
items.resize(100);
}
} options;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With