What is the Difference between "vector" and "array"?

Answer Posted / sumit arora

In C++,vectors are used for dynamically allocating space
(run time)as per your need,you do not need to hard code for
eg: consider a <vector> v;
for(int i=0;i<=v.size();i++)
cout<<v[i];

and that size can be increased with v.pushback(element)for
eg.
so there is no need to hard code whatever element you
pushed back only that memory is used.

but in array you do only have a fixed size
for(i=0;i<=100;i++)
cin<<a[i];

and if you want to access a[101] positions also it will
show you a garbage value ,bcoz in array there is no bound
checking and memorywize its a compile time allocation not a
dynamic one.

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What does new return if there is insufficient memory to make your new object?

593


What c++ is used for?

589


What is difference between class and function?

587


What is null pointer and void pointer?

631


Do inline functions improve performance?

652






Comment on assignment operator in c++.

718


How do I run a program in notepad ++?

608


What is lvalue?

703


What is a virtual destructor? Explain the use of it?

553


Explain the benefits of proper inheritance.

642


What is the hardest coding language to learn?

609


When are exception objects created?

613


I want to write a C++ language program that: 1. Reads in the size of a square from the screen; 2. Prints a hollow square of that size out of “-“, “|” and blanks on screen; 3. Prints the same hollow square onto a text file. The program should work for squares of all side sizes between 1 and 20.

2297


Write about an iterator class?

616


What is the use of register keyword with the variables?

553