TeodorKolev TeodorKolev - 1 year ago 169
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)

Answer Source

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, " "))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download