TeodorKolev TeodorKolev - 21 days ago 5
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)
{
myArray.push(value);
}

Answer

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;
}

EDIT:

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

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