-3

I wrote a code to sort a vector of Integer and noticed that one version is working fine and another one isn't.

Version 1: Using vector.reserve

#include <bits/stdc++.h>
using namespace std;
int main(void)
{
      ios_base::sync_with_stdio(false);
      vector<int> a;
      a.reserve(4);
      int i = 0;
      while (i < 4)
      {
            cin >> a[i++];
      }
      sort(a.begin(), a.end());
      for (int i :a)
      {
            cout << i << " ";
      }
}
INPUT: 1 5 3 2
OUTPUT:

Version 2: Defining vector size in advance

#include <bits/stdc++.h>
using namespace std;
int main(void)
{
      ios_base::sync_with_stdio(false);
      vector<int> a(4);
      int i = 0;
      while (i < 4)
      {
            cin >> a[i++];
      }
      sort(a.begin(), a.end());
      for (int i :a)
      {
            cout << i << " ";
      }
}
INPUT: 1 5 3 2
OUTPUT: 1 2 3 5

I am not quite sure what are the differences between two and when to use which if there is some distinction.

Piyush Keshari
  • 170
  • 2
  • 10
  • 2
    Check the [documentation for `reserve`](https://en.cppreference.com/w/cpp/container/vector/reserve) to see what it really does. Looks like [you want `resize`](https://en.cppreference.com/w/cpp/container/vector/resize) – user4581301 Nov 12 '20 at 18:24
  • 1st version invokes undefined behavior. `reserve` is not required to, actually, allocate memory, hence your loop accesses elements outside the `std::vector`. – Algirdas Preidžius Nov 12 '20 at 18:25
  • 2
    Side note: Take care with `#include ` ([why](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)) and `using namespace std;` ([why](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)). Put them together and you can smurf up a program in really crazy ways really easily. – user4581301 Nov 12 '20 at 18:26
  • More handy reading (since bolov found a better duplicate): [Choice between vector::resize() and vector::reserve()](https://stackoverflow.com/questions/7397768/choice-between-vectorresize-and-vectorreserve) – user4581301 Nov 12 '20 at 18:29
  • ```Put them together and you can smurf up a program in really crazy ways really easily.``` @user4581301 yes I know but I am into competietive coding and everyone suggests to do so in order to save time. – Piyush Keshari Nov 12 '20 at 18:34
  • 2
    The time savings are a sucker bet. `#include ` includes the entire standard library and slows build times by close to an order of magnitude. If you lose 5-10 seconds every time you rebuild the program, the time saved typing is eaten up very, very quickly. – user4581301 Nov 12 '20 at 18:37
  • @user4581301 Thankyou very much and I do appreciate your concern, I will keep this in mind from now onwards. – Piyush Keshari Nov 12 '20 at 18:44

1 Answers1

1

The vector reserve method does not change the size of the vector, it changes the amount of allocated memory, possibly. You still cannot write to the indices 0 through 3 in the loop you have in the first example because the size of the vector is still 0. You'd want resize, which also changes the size.

Anonymous1847
  • 2,568
  • 10
  • 16