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 c++ hiding?
What is a memory leak c++?
Refer to a name of class or function that is defined within a namespace?
how to access grid view row?
What are the types of pointer?
What is the best c++ book for beginners?
Difference between struct and class in terms of access modifier.
What are the basic data types used in c++?
What is a base class?
Why are arrays usually processed with for loop?
What is the role of C++ shorthand's?
How does list r; differs from list r();?
what are the iterator and generic algorithms.
Explain operator overloading.
What is rtti in c++?