Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to default initilise a std::vector

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,

like image 561
Mario Avatar asked Jan 07 '20 19:01

Mario


People also ask

How do you declare a vector by default value?

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);

How do you initial a vector in C++?

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.

Is vector always initialized with 0?

@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.

What is default initialization in C++?

Default member initializer (C++11) [edit] This is the initialization performed when an object is constructed with no initializer.

How to create and initialize a vector in C++ STL?

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()

How to initialize an std::vector?

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.

What is the difference between initializing and declaring a vector?

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.

What is the default value of a vector in C++?

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:


2 Answers

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.

like image 61
NathanOliver Avatar answered Oct 23 '22 13:10

NathanOliver


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;
like image 39
Tobias Avatar answered Oct 23 '22 11:10

Tobias