ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage   interview questions urls   External Links  Contact Us     Login  |  Sign Up                      
tip       Ask Questions on ANYTHING, that arise in your Daily Life at     FORUM9.COM
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C++  >>  C++ General
 
 


 

 
 STL interview questions  STL Interview Questions
 OOPS interview questions  OOPS Interview Questions
 C++ General interview questions  C++ General Interview Questions
Question
What is the Difference between "vector" and "array"?
 Question Submitted By :: Guest
I also faced this Question!!     Rank Answer Posted By  
 
  Re: What is the Difference between "vector" and "array"?
Answer
# 1
a vector is also like an array i.e sequentially accessed but
we can increase/decrease size of a vector in a simple way
than it is done for arrays.
 
Is This Answer Correct ?    6 Yes 0 No
Guest
 
  Re: What is the Difference between "vector" and "array"?
Answer
# 2
vector is also an array but the size of a vector can change
dynamically where in array its fixed.

we can create some reserve space in vector where in array we
cannot.
 
Is This Answer Correct ?    3 Yes 0 No
Vishwa
 
 
 
  Re: What is the Difference between "vector" and "array"?
Answer
# 3
as we know in C++  the size of array  is fixed in compile
time .so  the size of array  cannot be adjusted at run time
to accommodate changeling program .
but  vector can  solve  this problem  allocating memory as
needed.  although vector is dynamic
 
Is This Answer Correct ?    4 Yes 0 No
Saroj Das ,
 
  Re: What is the Difference between "vector" and "array"?
Answer
# 4
A growable array is calld vector while array is of fixed size,
 
Is This Answer Correct ?    4 Yes 0 No
Sanish Joseph
 
  Re: What is the Difference between "vector" and "array"?
Answer
# 5
First, a vector is a template class but not the array is.
So obviously there are members to access the elements of 
the vector like, range checking, iterators, 
insertion/deletion, predicates, etc. 

Since it is a template class, the same implementation can 
be used for any type including pointers, objects/user 
defined types.

Second, as said by others, vector automatically grow as we 
invoke push_back(), we can reserve additional space for 
elements, etc.
 
Is This Answer Correct ?    2 Yes 0 No
Mms Zubeir
 
  Re: What is the Difference between "vector" and "array"?
Answer
# 6
We know that arrays and Vectors in Java are different kinds
of data structures for storing information in a
random-access fashion. In Java, an object-oriented language,
the two structures are implimented as classes. This
presentation will demonstrate some of the principle
differences between Java arrays and Java Vectors. It will
also provide some examples of the cool methods that come
with them.

Arrays

Arrays in Java are static lists declared to store a certain
number of a certain kind of variables. It stores these
values at specific, numbered locations in a list that starts
at zero and goes to N-1:

myArray Index 	Data
0 	14
1 	79
2 	32
3 	92
4 	102

This array can be instantiated in Java with either the
shorthand syntax:

int[] myArray = new int[5];
int myArray[] = new int[5];

or an explicit method call to a Java "Array" class object:

int[] myArray = Array.newInstance[int, 5];

When an array is instantiated, the array's constructor
method allocates 5 integer-sized memory slots. The
allocation is static- the array size cannot change after it
is created. In this sense, it is analagous to a C or C++
style array. But the resemblence doesn't end here. Java
Arrays, like C arrays, have undefined contents until they
are initialized. One can set the contents of an array at the
time of instantiation with the simple syntax:

int[] myArray = {14,34,21,42,22};

We have already seen that, in addition to the built-in
C-style array operations syntax there Java provides
additional classes for manipulating arrays. This represents
a desire on the Java authors' part to both a) retain a
grounding in C to attract those programmers already familiar
with the popular language, and b) provide true
object-oriented support for array operations. This is true
for array referencing, as well. Various array-handling
objects exist in the class library java.lang. This provides
interface functions for various array operations For
example, one could refer to the individual array as in C:

fourth_item = myArray[3];

or, one could use the explicit method call:

fourth_item = myArray[3];

Java arrays can also be passed as parameters to functions.
Java permits either a copy of the original array or a
pointer to be sent.
2D-arrays are also supported. This is a statically-allocated
array of array pointers:

int[][] 2D_array = new int[x_length][y_length];

Finally, note that vectors of objects can exist. This does
not mean, however, that the new objects are instantiated
when the array is declared. Arrays take up a fixed size in
memory, and the size of an object isn't defined when it is
instantiated. Therefore, the objects must be instantiated in
a seperate command. Therefore, to create an array of 5
objects, the following code could be used:

object_list = new Object[5];
object_list[0] = new Object();
object_list[1] = new Object();
.
.
.
Vectors

The key difference between Arrays and Vectors in Java is
that Vectors are dynamically-allocated. They aren't declared
to contain a type of variable; instead, each Vector contains
a dynamic list of references to other objects. The Vector
class is found in the java.util package, and extends
java.util.Abstractlist.

The big advantage of using Vectors is that the size of the
vector can change as needed. Vectors handle these changes
through the "capacity" and "capacityIncrement" fields. When
a Vector is instantiated, it declares an object array of
size initialCapacity. Whenever this array fills, the vector
increases the total buffer size by the value
capacityIncrement. Thus, a Vector represents a compromise.
It doesn't allocate each object dynamically, which would
make access of middle list-items exceedingly slow; but it
does allow for growth in the size of the list. The default
constructor, Vector(), creates an empty Vector of initial
capacity zero that doubles in size whenever it fills.

Vectors can be filled using its method addElement(Object
obj). This method appends the given object to the end of the
Vector. Because the Vector stores pointers to the objects,
and not the objects themselves, these Vector items could be
any kind of object. Moreover, several different kinds of
objects could exist in the same Vector. Vector methods allow
all sorts of nifty ways to manipulate data. For example, one
can reference a list item not just by its index, but also by
name. Elements can be removed from or added to the middle of
the list:

myVector.insertElementAt(my_object, 5);
success_flag = myVector.remove(my_object);

So you're thinking, "GREAT! But if vectors are so wonderful,
why even bother with arrays?"
Comparative Advantages of Arrays and Vectors

Because Vectors are dynamically-allocated, they offer a
relatively memory-efficient way of handling lists whose size
could change drastically. The line buffer of a text editor,
for example, could be anywhere from zero to thousands of
entries in size. A Vector might be a good choice for such an
arrangement. Moreover, Vectors have some useful member
functions that make coding simpler (see the insertElementAt
and remove methods above). But because the Vector class is
based on an array of object references, these methods are
generally no more efficient than array-based algorithms. The
insert method must perform multiple "swap" operations just
as an array 'insert' algorithm would. Thus, Vectors are
easier to use than arrays for most applications, but they do
not offer all the performance advantages of fully-dynamic
storage.
 
Is This Answer Correct ?    2 Yes 0 No
Raju
 

 
 
 
Other C++ General Interview Questions
 
  Question Asked @ Answers
 
What does '\r' and '\b' mean? Please explain with example.  1
what is the behaviour of C and C++ compiler for the below statements. int *p; p = malloc(100); Is the behaviour same ? or different ?  1
Find out the bug in this code,because of that this code will not compile....... #include <iostream> #include <new> #include <cstring> using namespace std; class balance { double cur_bal; char name[80]; public: balance(double n, char *s) { cur_bal = n; strcpy(name, s); } ~balance() { cout << "Destructing "; cout << name << "\n"; } void set(double n, char *s) { cur_bal = n; strcpy(name, s); } void get_bal(double &n, char *s) { n = cur_bal; strcpy(s, name); } }; int main() { balance *p; char s[80]; double n; int i; try { p = new balance [3]; // allocate entire array } catch (bad_alloc xa) { cout << "Allocation Failure\n"; return 1; }  2
Write the program form Armstrong no in c++?  3
"How will you merge these two arrays? Write the program Array: A 1 18 22 43 Array: B 3 4 6 20 34 46 55 Output Array: C 1 3 4 6 18 20 22 34 43 46 55" HCL5
class X { private: int a; protected: X(){cout<<"X constructor was called"<<endl;} ~X(){cout<<"X destructor was called"<<endl} }; Referring to the code above, which one of the following statements regarding "X" is TRUE? a) X is an abstract class. b) Only subclasses of X may create X objects. c) Instances of X cannot be created. d) X objects can only be created using the default copy constructor. e) Only friends can create instances of X objects. Quark2
What are the different types of polymorphism?  2
What is size of null class? HP3
When copy constructor can be used? Symphony4
What are the different types of Storage classes?  3
When volatile can be used? Symphony2
what is meaning of isa and hsa  1
In C++ cout is: a) object b) class c) something else Lehman-Brothers10
How to write Multithreaded applications using C++? Honeywell1
Is structure can be inherited? HP2
how to swap two numbers with out using temp variable  6
implement stack using stack.h headerfile functions Subex1
What is the Difference between "C structure" and "C++ structure"?  3
How Virtual functions call up is maintained?  2
Please post the model question paper of hal?  1
 
For more C++ General Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com