0

I'm new to using vectors and am a little bit confused using them. I've written some code and I added some question in the comments. In addition to the questions in my comments, why do we need allocation by using reserve()? If we allocate, we will use won't we? If we need to allocate, is resize() more useful than reserve()? I'm really stuck.

#include <iostream>
#include <vector>
using namespace std;

int main()
{

    vector<int> a_vector( 10 );
    // equal vector<int> a_vector( 10,0 ); ?
    cout << "value of vector first " << a_vector.at(0) << endl; //LEGAL
    // cout << a_vector.at(10); // ILLEGAL
    cout << "vector size " << a_vector.size() << endl;
    a_vector.push_back( 100 );
    cout << "value of vector at ten " << a_vector.at(10) << endl; //LEGAL
    cout << "vector size " << a_vector.size() << endl;
    a_vector.pop_back();
    cout << "vector size " << a_vector.size() << endl;

    a_vector.resize( 12 );
    // also does it mean a_vector[10] = 0; and a_vector[11] = 0;?
    cout << "vector size " << a_vector.size() << endl;
    cout << "value of vector at ten " << a_vector.at(10) << endl; //LEGAL
    cout << "value eleventh " << a_vector.at(11) << endl; //LEGAL
    a_vector.pop_back();
    a_vector.pop_back();
    cout << "vector size " << a_vector.size() << endl;
    for (int i = 0; i < 2; i++)
    {
        //doesn't it same as a_vector.resize( 12 ); now ?
        //so why do we need resize(); ?
        //also do I need reserve() for using push_back() like this ?
        a_vector.push_back(0);
    }
    cout << "vector size " << a_vector.size() << endl;
    a_vector.pop_back();
    a_vector.pop_back();
    cout << "vector size " << a_vector.size() << endl;


    return 0;
}
DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61

1 Answers1

2
vector<int> a_vector( 10 );
// equal vector<int> a_vector( 10,0 ); ?

Yes, it's implicitly vector<int> a_vector(10, int());.

cout << "value of vector first " << a_vector.at(0) << endl; //LEGAL
// cout << a_vector.at(10); // ILLEGAL

Not illegal, it will just throw an exception.

a_vector.resize( 12 );
// also does it mean a_vector[10] = 0; and a_vector[11] = 0;?

Yes. The new elements are default constructed.

for (int i = 0; i < 2; i++)
{
    //doesn't it same as a_vector.resize( 12 ); now ?
    //so why do we need resize(); ?
    //also do I need reserve() for using push_back() like this ?
    a_vector.push_back(0);
}

Yes, in this case it's the same. Calling resize is shorter.


In conclusion:

  • resize either removes or adds default constructed values in order to make the size of the container that one
  • reserve has more to do with the capacity of the container, which is the internal size of the dynamically allocated array
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • I'd add that `resize` might be more efficient than several `push_back` (unless you call `reserve` beforehand) as it allows to make sure you won't reallocate the whole vector several times (itsnotmyrealname doesn't seem to be really sure of why resize/reserve are here). Otherwise nice answer. – Caninonos Aug 12 '15 at 21:10
  • do I need `reserve()` for using `push_back()` ? I think no but you are experienced. – Soner from The Ottoman Empire Aug 12 '15 at 21:11
  • @itsnotmyrealname No, you don't. – Shoe Aug 12 '15 at 21:13
  • @itsnotmyrealname : no you don't, however, if the memory isn't reserved already, a call to `push_back` will allocate a new larger block of memory, copy the vector contents and your new element inside, then release the old memory block (and you don't want to do that very often for efficiency reasons) – Caninonos Aug 12 '15 at 21:13
  • @Caninonos "no you don't, however, if the memory isn't reserved already, a call to push_back will allocate a new larger block a memory" not always true in most implementations. – Shoe Aug 12 '15 at 21:13
  • @ʎǝɹɟɟɟǝſ I don't think so, but my sentence might be confusing: `push_back` may reserve more memory than it really needs (a common practice is to double the reserved memory each time you have to reallocate). Still `reserve` is useful to manually reserve the good amount of memory if you know it in advance and may either save reallocations, save memory usage, or both. – Caninonos Aug 12 '15 at 21:16
  • I really can't understand so why do we need `reserve()` while there is `push_back()` and for loop ? @Caninonos @ʎǝɹɟɟɟǝſ – Soner from The Ottoman Empire Aug 12 '15 at 21:23
  • @itsnotmyrealname That helps you to ensure you will avoid any unnecessary reallocations. The size of the array (i.e. the number of meaningfull objects inside) and the size of the memory block allocated by the array are two different things. Changing the array's size is quite cheap... unless you request a bigger size than the memory block's size. If that's the case, you usually have to reallocate a bigger memory block and perform a costly copy. `reserve` allows you to tell how much memory you will need beforehand so that calls to `push_back` or `resize` never cause a reallocation and copy. – Caninonos Aug 12 '15 at 21:23
  • Aha if we `reserve()` a vector before using `push_back()` or `resize()` there will be no copy of the vector ? Did I get right ? @Caninonos – Soner from The Ottoman Empire Aug 12 '15 at 21:32
  • @itsnotmyrealname That's the idea (although it's more the copy of the contents of the vector, and not the `std::vector` itself) and only if the size doesn't grow past the size of the reserved memory (or capacity) of the vector, obviously. (Edit: however, if you're not really sure whether it's useful or not, follow jeffrey's advice and don't use it) – Caninonos Aug 12 '15 at 21:33
  • @itsnotmyrealname Something like that. Don't worry about `reserve` really. – Shoe Aug 12 '15 at 21:33