Reputation: 4118
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);
}
Upvotes: 0
Views: 558
Reputation: 38959
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, " "))
Upvotes: 4
Reputation: 437
You cannot use push into an array. I would suggest you to use lists or vectors if you don't want to set any size.
Upvotes: 0