There is no way to do what you say in C++ with plain arrays. The C++ solution for that is by using the STL library that gives you the std::vector.
You can use a vector in this way:
#include
std::vector< int > arr;
arr.push_back(1);
arr.push_back(2);
arr.push_back(3);
Arrays in C++ cannot change size at runtime. For that purpose, you should use vector
vector
arr.push_back(1);
arr.push_back(2);
// arr.size() will be the number of elements in the vector at the moment.
As mentioned in the comments, vector is defined in vector header and std namespace. To use it, you should:
#include
and also, either use std::vector in your code or add
using std::vector;
or
using namespace std;
after the #include