×

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

Types of polymorphism in C++

What is polymorphism in C++ ? Polymorphism literally translates to "multiple forms". This indicates that the same thing behaves differently depending on the context in programming. Polymorphism is a feature of C++...

6 minutes read.

C++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes read.

Multilevel Inheritance

C++ Multilevel Inheritance Multilevel inheritance is such an inheritance in which a derived class is created from another derived class. C++ Multilevel Inheritance Example In this example, a base class Student is inherited in...

2 minutes read.

Dynamic Binding in C++

The notion of dynamic binding solved the challenges associated with static binding. Static binding refers to bindings that can be resolved by the compiler at runtime. All storage, stationary, and...

3 minutes read.

C++ Structs

We frequently encounter scenarios in which we must store a bunch of data, whether of comparable or dissimilar data kinds. Arrays are used to hold a group of data of...

6 minutes read.

SDL library in C++ with Examples

SDL stands for Simple DirectMedia Layer. Using OpenGL and Direct3D, it is a cross-platform development library created to give users low-level access to audio, keyboard, mouse, joystick, and graphics hardware....

3 minutes read.

C++ OOPs Concept

The main goal of C ++ programming is to add the idea of ​​object orientation to the C programming language. Inheritance, data binding, polymorphism and other concepts are part of...

4 minutes read.

Default Constructor

C++ Default Constructor A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created. If a programmer does not create any...

2 minutes read.

fread() Function in C++ Programming

C++ language is used to make high-performance applications that can work efficiently, and it is one of the world's most popular languages. It is an object-oriented and high-level programming language;...

3 minutes read.

Abstract class in C++

In this article, you will get exposure to an abstract class in C++. We will discuss this topic using some practical examples too. To understand the abstract classes, you should...

6 minutes read.

Decltype type Specifier in C++

The primary use of C++ decltype is to inspect the declaration type of an entity in an expression. The auto keyword can declare a particular type of variable, whereas the...

4 minutes read.

wcscpy(), wcslen(), wcscmp() Functions in C++

There are many built-in functions in C++ programming language which differentiate it from C programming language in most hardware-coded languages. We will now closely look into the applications of three...

4 minutes read.

Hierarchical Inheritance

C++ Hierarchical Inheritance Hierarchical inheritance inherits the property of one base class in more than one derived class.   C++ Hierarchical Inheritance Example #include <iostream>   using namespace std;   class Person {       char gender[10];       int age;   public:       void getPerson()       {           cout << "Age: "; cin >> age;           cout << "Gender: "; cin >> gender;       }       void dispPerson()       {           cout << "Age: " << age << endl;           cout << "Gender: " << gender << endl;       }   };   class Employee : public Person {       float salary;   public:       void getEmployee()       {           Person::getPerson();           cout << "Salary: Rs."; cin >> salary;       }       void dispEmployee()       {           Person::dispPerson();           cout << "Salary: Rs." << salary << endl;       }   };   class Student : public Person {       char level[20];   public:       void getStudent()       {           Person::getPerson();           cout << "Class: "; cin >> level;       }       void dispStudent()       {           Person::dispPerson();           cout << "Level: " << level << endl;       }   };   int main()   {       Person per;       Employee emp;       Student stu;       cout << "Student data" << endl;       cout << "Enter data" << endl;       stu.getStudent();       cout << endl << "Displaying data" << endl;       stu.dispPerson();       cout << endl << "Staff Data" << endl;       cout << "Enter data" << endl;       emp.getEmployee();       cout << endl << "Displaying data" << endl;       emp.dispPerson();   } Output: Student data Enter data Age: 10 Gender: f Class: 5 Displaying data Age: 10 Gender: f Employee data Enter data Age:...

1 minute read.

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

Skyline Problem in C++

We have given n rectangular buildings in a 2-dimensional city. Here, to compute the Skyline of the given n rectangle structures in a two-dimensional metropolis while removing hidden lines, the...

3 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

3 minutes read.

Programs to Print Pyramid Patterns in C++

We will explore how to use code to print a variety of patterns utilizing stars (*), numbers (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,.…)  and alphabets...

7 minutes read.

Random Number Generator in C++

In programming, we need to frequently create the randomly. For example, a dice game, handing out cards to players, apps for rearranging tunes, etc. T There are two tools available in...

4 minutes read.

Auto keyword in C++

The primary function of auto keyword is to assign and detect the value of the data type automatically in C++. The compiler knows the data type of the variable by...

2 minutes read.

C++ Expressions

C ++ equations are made up of operators, constants and variables arranged according to language rules. It may also include function calls that give results. To calculate the value, the...

4 minutes read.