Q:

Write a program to explore how vectors grow in the library you use

0

Write a program to explore how vectors grow in the library you use.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <vector>
#include <iostream>

int main() {
  std::vector<int> vi;
  std::cout << "Size: " << vi.size()
            << "\tCapacity : " << vi.capacity() << std::endl;

  vi.push_back(1);
  std::cout << "Size: " << vi.size()
            << "\tCapacity : " << vi.capacity() << std::endl;

  for (std::vector<int>::size_type ix = 0; ix != 100; ++ix)
    vi.push_back(ix);
  std::cout << "Size: " << vi.size()
            << "\tCapacity : " << vi.capacity() << std::endl;

  vi.shrink_to_fit();
  std::cout << "Size: " << vi.size()
            << "\tCapacity : " << vi.capacity() << std::endl;

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now