c++ - Do Vectors resize automatically? -
i sorry asking such beginner question finding contradictory information online. ask @ university out until february next year.
do vectors resize automatically? or need check current size periodically , resize when need more room. looks resizing me automatically i'm not sure if feature or compiler waving magic wand.
if use push_back
or insert
, yes vector resizes itself. here 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); (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 if a[3] = 5
. not resize vector automatically.
can manually resize vector if want. demo append following code above code.
a.resize(6); (int value : a) { cout << << " "; } cout << endl << "current size " << a.size() << endl;
now output:
1 2 3 current size 3 1 2 3 0 0 0 current size 6
it think got answer.
Comments
Post a Comment