8

I am very sorry that I am asking such a beginner question but I am finding contradictory information online. I would ask at University but it is out until February next year.

Do Vectors resize automatically? Or do you need to check the current size periodically and resize when you need more room. It looks to be resizing for me automatically but I'm not sure if that is a feature or the compiler waving a magic wand.

Lord Windy
  • 772
  • 1
  • 8
  • 24

2 Answers2

13

If you use push_back or insert, yes vector resizes itself. Here is a demo:

#include<iostream>
#include<vector>

using namespace std;

int main() {
    vector < int > a;
    a.push_back(1);
    a.push_back(2);
    a.push_back(3);
    for (int value : a) {
        cout << value << " ";
    }
    cout << endl << "Current size " << a.size() << endl;
    return 0;
}

It gives output as:

1 2 3
Current size 3

Remember now if you do a[3] = 5. It will not resize your vector automatically.
Also you can manually resize vector if you want. For demo append following code to above code.

a.resize(6);
for (int value : a) {
    cout << a << " ";
}
cout << endl << "Current size " << a.size() << endl;

Now it will output:

1 2 3
Current size 3
1 2 3 0 0 0
Current size 6

It think you got your answer.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
Ashwani
  • 1,938
  • 11
  • 15
  • Nit pick: `vector::iterator it;` ... `for(it = a.begin(); it < a.end(); it++)` - would be better as `...` `for (vector::iterator it = a.begin(); it != a.end(); ++it)`. (i.e. defining variable in the tightest useful scope, `!=` is idiomatic as it works for all Standard containers, `++it` to avoid reliance on optimiser to eliminate temporary). – Tony Delroy Nov 27 '14 at 07:23
  • 1
    Or even for range `for (const auto& e : a) { std::cout << e << " "; }` since C++11. – Jarod42 Nov 27 '14 at 08:22
  • @TonyD I mostly code in `C` and I am new to `C++`, So most of my `C++` code looks like `C`. Also it does not affect the answer. Anyway, thanks for mentioning I will update it. – Ashwani Nov 27 '14 at 09:51
  • @AshwaniDausodia: sure - no worries. True "does not affect the answe" but other beginners see this stuff and might pick up habits.... Cheers. – Tony Delroy Nov 27 '14 at 09:55
  • 1
    I refactored your code to use the more idiomatic (since C++11) ranged-for loop. I've done this because this question was featured on the isocpp.org feed, so it is likely to get much higher exposure. – Björn Pollex Dec 08 '14 at 18:27
  • I'm getting reproducible segmentation faults when using emplace to insert new elements beyond the current vector size. Insert doesn't seg fault but values are not constructed properly hence for a simple vector you'll likely find many 'junk' values between the previous end() and the resized end() iterators. For my purposes a proper call to resize() is the way to go. If you are emplacing an item just beyond the current size you might get away with it as the vector may have allocated some extra space under the hood in anticipation of a future resize, I believe. – Stephen Blinkhorn May 10 '17 at 19:27
  • Update to my previous comment... seg faults with insert too (on my compiler/platform). – Stephen Blinkhorn May 10 '17 at 19:39
5

Do Vectors resize automatically?

Yes, they do, and you can convince yourself of that very easily:

std::vector<int> squares;
for (int i = 0; i < 100; ++i)
{
    squares.push_back(i * i);
    std::cout << "size: " << squares.size() << "\n";
}
fredoverflow
  • 256,549
  • 94
  • 388
  • 662