×

Vector resize() in C++

Vector resize() in C++

Vectors are called dynamic arrays and can automatically adjust their size when a component is added or deleted. This container is used for storage.

The function modifies the real content of the container by inserting or deleting the components from it. Thus it occurs,

  1. If at present the given value of n is less than the size, then additional elements are demolished.
  2. If n is greater than the current container size, then the next components are modified at the end of the vector.

Syntax:

vectorname.resize(int n, int val)

Parameters:

N – The container size is new, displayed in the number of objects.

Val -If this variable is specified, new features with this value are initialized.

Return value:

This method contains nothing.

Exception:

The only exception if this occurs is thrown by Bad alloc, if it keeps failing to reallocate.

The programs below show how the function works

The size of the vector container is lowered.

// resizing of the vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> vect;
    // 5 elements are inserted
    // in the vector
    vect.push_back(5);
    vect.push_back(6);
    vect.push_back(7);
    vect.push_back(8);
    vect.push_back(9);
    cout << "vector before resizing:"
         << endl;
    // displaying the contents of the
    // vector before resizing
    for (int t = 0; t < vect.size(); t++)
        cout << vect[t] << " ";
    cout << endl;
    // vector is resized
    vect.resize(4);
    cout << "vector after resizing:"
         << endl;
    // displaying the contents of the
    // vector after resizing
    for (int t = 0; t < vect.size(); t++)
        cout << vect[t] << " ";
    return 0;
}

Output:

Vector resize() in C++

Size of the vector container is increased.

// resizing of the vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> vect;
    // 10 elements are inserted 
    // in the vector
    vect.push_back(1);
    vect.push_back(3);
    vect.push_back(5);
    vect.push_back(7);
    vect.push_back(9);
    vect.push_back(11);
    vect.push_back(13);
    vect.push_back(15);
    vect.push_back(17);
    vect.push_back(19);
    cout << "Contents of vector before resizing:"
         << endl;
    // displaying the contents of the
    // vector before resizing
    for (int a = 0; a < vect.size(); a++)
        cout << vect[a] << " ";
    cout << endl;
    // vector is resized
    vect.resize(12);
    cout << "Contents of vector after resizing:"
         << endl;
    // displaying the contents of 
    // the vector after resizing
    for (int a = 0; a < vect.size(); a++)
        cout << vect[a] << " ";
    return 0;
}

Output:

Vector resize() in C++

The size of the vector container is increased, and new elements are initialized with the specified value.

// resizing of the vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> vect;
    // 11 elements are inserted
    // in the vector
    vect.push_back(2);
    vect.push_back(4);
    vect.push_back(6);
    vect.push_back(8);
    vect.push_back(10);
    vect.push_back(12);
    vect.push_back(14);
    vect.push_back(16);
    vect.push_back(18);
    vect.push_back(20);
    vect.push_back(22);
    cout << "Contents of vector before resizing:"
         << endl;
    // displaying the contents of 
    // the vector before resizing
    for (int k = 0; k < vect.size(); k++)
        cout << vect[k] << " ";
    cout << endl;
    // vector is resized
    vect.resize(20, 9);
    cout << "Contents of vector after resizing:"
         << endl;
    // displaying the contents 
    // of the vector after resizing
    for (int k = 0; k < vect.size(); k++)
        cout << vect[k] << " ";
    return 0;
}

Output:

Vector resize() in C++

Related Topics

Constexpr in C++

Constexpr is a keyword in C++ that is used to declare variables or functions as compile-time constants. This means that the value of a constexpr variable or the return value...

3 minutes read.

How to Use Getline in C++

What is getline in C++? Similar to the cin function, which allows the programmer to take input from the user getline() function also takes input from the user. But in this...

4 minutes read.

Factory Method for Designing Pattern in C++

In C++, the factory method is a type of conditional design pattern. The factory method is related to creating a new object in C++. With the help of a factory...

3 minutes read.

C++ Infinite loop

The term "infinite loop" refers to a loop that does not terminate the loop according to the condition. In some cases, an infinite loop may be required in programming, or...

4 minutes read.

C++ Recursion Function

A programming method called recursion that uses a function to call itself to address lesser problems. The Fibonacci sequence, factorial computation, and tree traversal are just a few of the...

4 minutes read.

Difference between Two Sets in C++

The distinction between the two sets is made up of the components that are present in the first set but absent from the second set. The function consistently duplicates the...

3 minutes read.

Reverse function in C++

The function std::reverse() is included in the standard template library of C++. It takes in a beginning and ending iterator, reversing the order. To use the reverse statement, we need...

2 minutes read.

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 minutes read.

Method overriding in C++

What is method overriding? Using the same function in derived class as their base class is referred to as function/method overriding in c++. Method overriding is an example of polymorphism. With...

2 minutes read.

Assertions in C/C++

Assertions are the statements used to check presumptions which is made by the programmers. Example: Assertion is used to verify whether the malloc returned by the pointer is NULL or not. For...

4 minutes read.

Swap numbers in C++

Swap numbers Swapping refers to interchanging values between two variables. Swapping is important and easy to understand programming logic in the world of coding. Though it is used in the programming...

4 minutes read.

Constructor Vs Destructor in C++

C++ Constructor A function Object () { [native code] } is a member function that shares the same name as the class. It is called automatically whenever a class object is...

3 minutes read.

Floating Point Operations and Associativity in C, C++ and Java

In this tutorial, we are going to compare Floating-point operations and the concept of associativity. Before we apply the concept of associativity in the floating-point operations in all three programming...

3 minutes read.

Print Table Using While Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

Hexadecimal to Decimal in C++

In computers, hexadecimal numbers are represented with base 16 and decimal numbers are represented with base 10 and values 0-9, whereas hexadecimal numbers have digits ranging from 0 to 15,...

3 minutes read.

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

C++ Program to Implement Merge Sort

C++ Program to Implement Merge Sort The technique of merge sort is based on the strategy of divide and conquer. We divide the set of while data into smaller bits, arranged...

3 minutes read.

C++ Fibonacci Series

What is a Fibonacci series? A Fibonacci series or sequence is a very popular programming paradigm. The next element occurring in the N terms series is determined by the sum of...

2 minutes read.

User-defined literals in C++

Introduction to User-Defined Literals: User-defined literals, introduced in C++11, are a way to extend the C++ language to allow users to define their literal suffixes and the corresponding behavior. These suffixes...

7 minutes read.

Storage Classes in C

Storage Classes in C Storage Classes are used to define the variable and function property. These functionalities include basically the scope, accessibility, and lifetime that help us detect the existence of...

4 minutes read.