-1

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;
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
Shivam Kasat
  • 35
  • 1
  • 6
  • 2
    `reserve` doesn't create any elements. It size is still 0. – tkausl Aug 08 '19 at 19:24
  • could you please explain! instead of down voting! I am newbie! – Shivam Kasat Aug 08 '19 at 19:25
  • but when i print the element using for(int i=0;i – Shivam Kasat Aug 08 '19 at 19:26
  • 1
    Well, the first thing you could have done is write a 3 line `main` program that simply called (wrongly) `reserve` and see what the issue was. You don't need a full-blown program to do that simple test. – PaulMcKenzie Aug 08 '19 at 19:26
  • 1
    Please read the [documentation](https://en.cppreference.com/w/cpp/container/vector/reserve). – G.M. Aug 08 '19 at 19:27
  • @PaulMcKenzie yes I understand it! but I was working over some CP problem and faced this issue! so posted it as it is – Shivam Kasat Aug 08 '19 at 19:28
  • 1
    @ShivamKasat when you are faced with a problem and are fishing for a solution, reduce the program to the minimum required too reproduce the problem. Usually this makes the path forward clear and you won't have to ask a question. If No good solution presents itself, you are in an excellent position to ask the question because there is no noise in the code sample. Use [mcve] as inspiration. – user4581301 Aug 08 '19 at 19:33

2 Answers2

2

This is a common mistake. std::vector::reserve does not create elements or change the size of the container; you're actually causing undefined behavior. reserve changes just the capacity. You are looking for std::vector::resize to change the size. Here's an example for clarity:

#include <iostream>
#include <vector>

int main() {
    std::vector<int> ivec;
    std::cout << ivec.size() << " - " << ivec.capacity() << '\n'; // 0 - 0
    ivec.reserve(100);
    std::cout << ivec.size() << " - " << ivec.capacity() << '\n'; // 0 - 100
    ivec.resize(30);
    std::cout << ivec.size() << " - " << ivec.capacity() << '\n'; // 30 - 100
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • The value of using `reserve()` is that it eliminates the resizing of the vector that otherwise occurs as you add elements to it. This involves accessing the heap to allocate more space and copying (or moving) the vector's elements to the new larger block then deleting the previous block. It can speed things up considerably. – doug Aug 09 '19 at 01:58
0

vector::reserve doesn't change the size of the vector. Instead, it just allocates additional memory, increasing the capacity of the vector for operations such as push_back.

For example:

std::vector<int> v;
// v.size() == 0, v.capacity() == 0
for(int i = 0; i < 100; i++) {
    v.push_back(i); // This will resize the vector a few times
}
// v.size() == 100, v.capacity() >= 100

Versus

std::vector<int> v;
v.reserve(100); 
// v.size() == 0, BUT v.capacity() >= 100

for(int i = 0; i < 100; i++) {
    v.push_back(i); // This won't resize the vector now
}

If you want to change the size of the vector, use vector::resize.

Alecto Irene Perez
  • 10,321
  • 23
  • 46
  • but sir when I try to access the elements like this printf("%d",arr[3]); they are accessible individually! why so? either they should not be accessible at all or they should be accessible by all means. – Shivam Kasat Aug 09 '19 at 12:29