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
What is virtual methods?
What is class in c++ with example?
What do you mean by public protected and private in c++?
How do you clear a buffer in c++?
Can I uninstall microsoft c++ redistributable?
What is data hiding c++?
What is oop in c++?
Which software is used for c++ programming?
What is an operator function? Describe the function of an operator function?
How is new() different from malloc()?
Comment on local and global scope of a variable.
Why do we use structure in c++?
What is copy constructor? Can we make copy constructor private in c++?
What is the exit function in c++?
what is Member Functions in Classes?