TeodorKolev TeodorKolev - 3 months ago 31
C++ Question

C++ add int to int array

How can I add int to my int array. I do not want to set array size, I do not want to use external loop.

int myArray[] = {};
if (condition)


As Leon suggests what you're looking for is vector and specifically its push_back method.

You could use it as follows:

vector<int> myArray; // currently size 0

if(condition) {
    myArray.push_back(value); // now resized to 1;


You can use an ostream_iterator to print a vector. For example:

copy(cbegin(myArray), cend(myArray), ostream_iterator<int>(cout, " "))