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 is virtual methods?

765


What is class in c++ with example?

658


What do you mean by public protected and private in c++?

696


How do you clear a buffer in c++?

633


Can I uninstall microsoft c++ redistributable?

687






What is data hiding c++?

686


What is oop in c++?

688


Which software is used for c++ programming?

715


What is an operator function? Describe the function of an operator function?

718


How is new() different from malloc()?

738


Comment on local and global scope of a variable.

671


Why do we use structure in c++?

668


What is copy constructor? Can we make copy constructor private in c++?

680


What is the exit function in c++?

629


what is Member Functions in Classes?

706