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 does new return if there is insufficient memory to make your new object?
What c++ is used for?
What is difference between class and function?
What is null pointer and void pointer?
Do inline functions improve performance?
Comment on assignment operator in c++.
How do I run a program in notepad ++?
What is lvalue?
What is a virtual destructor? Explain the use of it?
Explain the benefits of proper inheritance.
What is the hardest coding language to learn?
When are exception objects created?
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.
Write about an iterator class?
What is the use of register keyword with the variables?