I am just trying to learn STL for Competitive programming and stuck with this doubt! 1. When i use vector::reserve(n) my loops labeled as loop1 and loop2 don't print anything. 2. but when i use vector::assign(n,0) my loops labeled as loop 1 and loop 2 works fine. why is it so?
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
int test;
scanf("%d", &test);
while (test > 0) {
int n;
scanf("%d", &n);
vector<int> arr;
arr.reserve(n);
//arr.assign(n,0);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
sort(arr.begin(), arr.end());
vector<int>::iterator itr;
// loop1
for (int x : arr) {
printf("%d ", x);
}
//loop2
for (itr = arr.begin(); itr != arr.end(); itr++) {
printf("%d ", *itr);
}
test--;
}
return 0;
}