-2

I want to loop a increasing container, for example, if the container is std::vector. i need to push_back when looping the vector elements.

std::vector<int> v = {1, 3,5, 7,9};
for (auto i : v) {  // v is std::vector
  if (i % 2 == 1) v.push_back(i + 1);  // just a demo, the push_back will happen in some condition, won't be a endless loop
  printf("%d ", i);
} // the expected result is : 1, 3, 5, 7, 9, 2, 4, 6, 8, 10

I know vector is not the correct container, is there any good tools can handle this?

10
  • what is the expected result of this loop? That it adds elements to v until infinity? Or that it adds a copy of all elements in v to v ? Neither of this requires this loop Feb 5 at 13:21
  • It’s not clear what you’re asking. You want an arbitrarily-sized list? Feb 5 at 13:21
  • 1
    if you have code that is not doing what you expect it to do you should post a minimal reproducible example and explain what you expect it to do. The example you posted is unclear. Feb 5 at 13:23
  • @463035818_is_not_a_number the push_back only happens when some conditions are satisfied, it wont be an endless loop Feb 5 at 13:24
  • please show the code. Broken code alone is not sufficient to describe what it is supposed to do, because it is just broken. Why did you write the loop? What is in the vector before, waht should be in the vector after it? Feb 5 at 13:27

3 Answers 3

1

You can't use iterators or a range based for loop because of reallocation. But there's nothing wrong with the old-school approach of using an index. That's immune to reallocation

std::vector<int> v = {1, 3, 5, 7, 9};
for (size_t i = 0; i < v.size(); ++i) {
    if (v[i] % 2 == 1)
        v.push_back(v[i] + 1);
    printf("%d ", v[i]);
}
1

The problem is, as 463035818_is_not_a_number said in the comments, push_back() can (potentially) invalidates iterators. (for example, it can make std::vector to re-reallocate).

A much simpler solution is to use a for loop instead:

int main()
{
    std::vector<int> v = {1, 3,5, 7,9};
    for (int i = 0; i < v.size(); ++i) 
    { 
        if (v[i] % 2 == 1) 
        {
            v.push_back(v[i] + 1);
        }
        std::cout << v[i] << ' ';
    }
}

Link.

0

push_back invalidates iterators when the vector has to reallocate. You can resevere enough space and use an iterator based loop:

#include <vector>
#include <iostream>

int main() {

    std::vector<int> v = {1, 3,5, 7,9};
    v.reserve(v.size()*2);
    auto begin = v.begin();
    auto end = v.end();
    for (; begin != end; ++begin) {
        if (*begin % 2 ==1) v.push_back(*begin + 1);
    }
    for (const auto& e : v) std::cout << e << " ";
}

This assumes that you only need to iterate the elements that were already in the vector before the loop. (Your code assumes that the loop iterates also the newly added ones, but the condition is such that this is not necessary. You need a second loop for printing then.)

Alternatively you could use an index based loop:

  auto s = v.size();
  for (size_t i=0; i < s; ++i) {
      // ....

Not the answer you're looking for? Browse other questions tagged or ask your own question.