Resize Array in C++

  1. Use the resize Method to Resize an Array in C++
  2. Use the erase Method to Reduce Number of Elements in Array in C++
  3. Use Custom-defined Function to Resize an Array in C++

This article will introduce multiple methods of how to resize an array in C++.

Use the resize Method to Resize an Array in C++

Since the fixed-length array containers are not supposed to be resized in C++, we will focus on the std::vector class. resize is the vector container’s built-in function, and it changes the number of elements the vector contains. The function can reduce the number of elements if the first argument count is less than the current size of the vector. Otherwise, if the count argument is greater than the vector size, resize inserts additional elements with default values of 0 or the user can supply the desired value as a second argument to the function.

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

template<typename T>
void printVectorElements(vector<T> &vec)
{
    for (auto i = 0; i < vec.size(); ++i) {
        cout << vec.at(i) << "; ";
    }
    cout << endl;
}

int main() {
    vector<int> i_vec1 = {12, 32, 43, 53, 23, 65, 84};

    cout << "i_vec1             : ";
    printVectorElements(i_vec1);
    cout << "size: " << i_vec1.size() << endl;

    i_vec1.resize(i_vec1.size() - 2);

    cout << "i_vec1 (resized -2): ";
    printVectorElements(i_vec1);
    cout << "size: " << i_vec1.size() << endl;
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

i_vec1             : 12; 32; 43; 53; 23; 65; 84;
size: 7
i_vec1 (resized -2): 12; 32; 43; 53; 23;
size: 5

Use the erase Method to Reduce Number of Elements in Array in C++

The erase function is another built-in method of the std::vector class that can remove a single element from the vector or even delete the whole range specified with corresponding iterators.

In the following example, a given range from element 2 to the end is removed from the vector of integers. Note that it is common practice to pass the range parameters with begin/end iterators.

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

template<typename T>
void printVectorElements(vector<T> &vec)
{
    for (auto i = 0; i < vec.size(); ++i) {
        cout << vec.at(i) << "; ";
    }
    cout << endl;
}

int main() {
    vector<int> i_vec1 = {12, 32, 43, 53, 23, 65, 84};

    cout << "i_vec1          : ";
    printVectorElements(i_vec1);
    cout << "size: " << i_vec1.size() << endl;

    i_vec1.erase(i_vec1.begin() + 2, i_vec1.end());

    cout << "i_vec1 (resized): ";
    printVectorElements(i_vec1);
    cout << "size: " << i_vec1.size() << endl;
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

i_vec1          : 12; 32; 43; 53; 23; 65; 84;
size: 7
i_vec1 (resized): 12; 32;
size: 2

Use Custom-defined Function to Resize an Array in C++

Alternatively, we can define a separate function that iterates through the vector and removes a given number of elements from the end of the vector. This can be implemented using the pop_back built-in function that removes the last element in vector. As shown in the next code sample, the resizeVector function template is defined to take a vector of type Ts and several elements to remove from it.

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

template<typename T>
void printVectorElements(vector<T> &vec)
{
    for (auto i = 0; i < vec.size(); ++i) {
        cout << vec.at(i) << "; ";
    }
    cout << endl;
}

template<typename T>
void resizeVector(vector<T> &vec, int elems)
{
    for (auto i = 0; i < elems; ++i) {
        vec.pop_back();
    }
}

int main() {
    vector<int> i_vec1 = {12, 32, 43, 53, 23, 65, 84};

    cout << "i_vec1          : ";
    printVectorElements(i_vec1);
    cout << "size: " << i_vec1.size() << endl;

    resizeVector(i_vec1, 3);

    cout << "i_vec1 (resized): ";
    printVectorElements(i_vec1);
    cout << "size: " << i_vec1.size() << endl;
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

i_vec1          : 12; 32; 43; 53; 23; 65; 84;
size: 7
i_vec1 (resized): 12; 32; 43; 53;
size: 4

Related Article - C++ Array

  • Return Pointer to Array in C++
  • Shift Elements in Array in C++