Open In App

Default value of Vector in C++ STL

Last Updated : 21 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Vectors are the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. By default, the size of the vector automatically changes when appending elements. To initialize the map with a random default value, below is the approach: Approach:

  1. Declare a vector.
  2. Set the size of the vector to the user defined size N

Default value of the Vector:

The default value of a vector is 0. Syntax:

// For declaring 
vector v1(size); 

// For Vector with default value 0
vector v1(5);

Below is the implementation of the above approach: 

CPP




// C++ program to create an empty
// vector with default value
 
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int n = 3;
 
    // Create a vector of size n with
    // all values as 0.
    vector<int> vect(n);
 
    for (int x : vect)
        cout << x << " ";
 
    return 0;
}


Output:

0 0 0

Time Complexity: O(1)
Auxiliary Space: O(N)

Specifying a default value for the Vector:

We can also specify a random 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);

Below is the implementation of the above approach: 

CPP




// C++ program to create an empty vector
// and with a specific default value
 
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int n = 3;
    int default_value = 10;
 
    // Create a vector of size n with
    // all values as 10.
    vector<int> vect(n, default_value);
 
    for (int x : vect)
        cout << x << " ";
 
    return 0;
}


Output:

10 10 10

Time Complexity: O(N)
Auxiliary Space: O(1)



Similar Reads

vector::front() and vector::back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::front() This function can be used to fetch the first element of a vector container.Syntax : vectorname.front() Parameters : No value is needed to pass as
3 min read
vector::empty() and vector::size() in C++ STL
Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::empty() The empty() function is used to check if the vector container is empty or not.Syntax : vectorname.empty() Parameters : No parameters are p
5 min read
vector::push_back() and vector::pop_back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::push_back() push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the cur
4 min read
vector::operator= and vector::operator[ ] in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::operator= This operator is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new
4 min read
vector::at() and vector::swap() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::at() at() function is used reference the element present at the position given as the parameter to the function. Syntax: vectorname.at(position) Parameter
4 min read
vector::begin() and vector::end() in C++ STL
Vectors are the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::begin() begin() function is used to return an iterator pointing to the first element of the vector container. begin() function returns a bidirectional
3 min read
vector :: cbegin() and vector :: cend() in C++ STL
Vectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted. This storage is maintained by container. vector::cbegin() The function returns an iterator which is used to iterate container. The iterator points to the beginning of the vector.Iterator cannot modify the contents of the vector. Synta
2 min read
Initializing Vector using an Existing Vector in C++ STL
A vector is a type of container which can store objects of similar data type. Vector acts like a dynamic array where we can insert elements and the size of the array increases depending upon the elements inserted. Syntax: vector&lt;data_structure/type&gt; vector_name(size, item) To know more about vectors refer to vectors in C++. The 3D vector in C
5 min read
How to Initialize a Vector with Default Values in C++?
In C++, vectors are similar to arrays but unlike arrays, they are dynamic in nature. Vectors can resize them automatically when elements are inserted or removed during the runtime. In this article, we will learn how we can initialize a vector with default values in C++. Example: Input: vec ={, , , , , }; // Intialized the vector with default value
2 min read
Default values in a Map in C++ STL
Prerequisite: Map in STLA map is a container which is used to store a key-value pair. By default, In Primitive datatypes such as int, char, bool, float in C/C++ are undefined if variables are not initialized, But a Map is initially empty when it is declared. When this map is accessed with the [ ] (e.g map&lt;int,int&gt; mpp; mpp[1]; ) if the key is
2 min read
Practice Tags :