The `resize` function in C++ is used to change the size of a vector. It resizes the vector so that it contains a specified number of elements, either adding new elements or removing elements if necessary.
Here is an example code using `resize` function:
```c++
#include
#include
int main() {
std::vector vec = {1, 2, 3, 4, 5};
vec.resize(3); // Resize the vector to contain only 3 elements
for (int i : vec) {
std::cout << i << '\n'; // Output: 1 2 3
}
vec.resize(5); // Resize the vector to contain 5 elements
for (int i : vec) {
std::cout << i << '\n'; // Output: 1 2 3 0 0
}
return 0;
}
```
In the above code, we first initialize a vector `vec` with values `1,2,3,4,5`. Then, we resize it to only contain 3 elements. When we print out the elements of the vector, we see that only the first 3 elements are printed, and the last 2 elements are removed. We then resize the vector again to contain 5 elements, and now the last 2 elements are initialized with 0, as those elements did not previously exist.
The `resize` function is part of the `` library in C++.
C++ (Cpp) VI::resize - 20 examples found. These are the top rated real world C++ (Cpp) examples of VI::resize extracted from open source projects. You can rate examples to help us improve the quality of examples.