By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,836 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

Advantage of Vectors over Arrays

P: 11
Hi Guys,

In C++ , i was just wondering what is the advantage of Vectors over arrays?
I saw one sample program doing pretty much same way traditional vectors do then what the use of VECTORS??or what is their advantage??

Regards,
Mack
Mar 7 '07 #1
Share this Question
Share on Google+
7 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Hi Guys,

In C++ , i was just wondering what is the advantage of Vectors over arrays?
I saw one sample program doing pretty much same way traditional vectors do then what the use of VECTORS??or what is their advantage??

Regards,
Mack
According to Wikipedia, a Vector is a more generic array with some better options:

A vector is similar to an array but with extended functionality.
Citation
Mar 7 '07 #2

DeMan
100+
P: 1,806
It would depend on the implementation, but usually a Vector would be like a linked list. This means that its size is easily variable, adding elements so that some order is kept is relatively simple (you don't have to copy a lot of things further down the line), finding ordered elements is no more difficult than in an array, and a remove operation does not need everything to be reaaranged
Mar 7 '07 #3

P: 11
Friends,

thats really nice to hear from you , may i say that Vector is more advanced than Array ,
may be is it like template . just declare one data type and it would work for all different data types?

To add an elemet , just use push_back function ,to get size its very easy by size and pop_back etc operations.


Regards,
Mack.
Mar 7 '07 #4

Ganon11
Expert 2.5K+
P: 3,652
Once you declare a vector and give it the class type you'll be using it with, you shouldn't add any other data types to it. I'm not sure if it will give you an error when you push_back, but when you try to access, say, a double as an int, there will be errors - or, more seriously, accessing one object as a different class type, where trying to call a function could be catastrophic.

I generally use vectors when I'm going to have an array of unknown size, but in all other times, I use native arrays.
Mar 7 '07 #5

Banfa
Expert Mod 5K+
P: 8,916
but usually a Vector would be like a linked list.
I think most of the time a list would be a linked list.

Vectors are the C++ class implementation of a resizable array. They hold the data in a contiguous block. This has some disadvantages for instance if you are going to insert an item anywhere except at the end of the array it has to
  1. make sure there is room in allocated memory block
  2. copy all the entries above the insertion point up 1 index
  3. insert the new entry

Making sure there is room in the array (1) also effects push_back. When you push_back(or insert anywhere) if there not enough room in the array the first thing it has to do is reallocate the memory block, copy the data from the old block to the new block, release the old block.

If you are adding items to an array using push_back, say 100 items this would mean that 100 memory reallocations are required, again very time costly. This is why the resize method exists, it allows you to set the size of the array before starting all those push_backs so that only 1 memory allocation is done greatly speeding up the process.

And finally a vector does not srink the size of it's buffer when you reduce the number of items it holds so it you have a vector that at 1 time held 10000 ints but is currently holding 10 it still has allocated memory to hold 10000 ints. The is no method to release the memory, you have to create a new array and copy the current array to the new array to release it (well actually what happens is the new array only allocates memory for the data it actually needs to hold).

The 1 advantage of a vector is that it does hold a true array of data, that is the data appears in a memory block as it would in an defined array so you can pass a pointer to that data to a function expecting an array of data.


A list on the other hand does not suffer these problems. This is because it allocates a separate block of memory for every entry making it easier to insert anywhere and easier to delete anywhere. Also as you delete items in a list it deallocates the memory for them so you only every have allocated as much memory as you need.

However because each item in a list is allocated separately it's data is not held as a true array in memory.
Mar 7 '07 #6

Ganon11
Expert 2.5K+
P: 3,652
Also, the disadvantage of linked lists over arrays is no random access. There is no list[x] operator, so you have to traverse x nodes to get to the xth node. In arrays (and vectors) you can use the subscripts operator to get to the object you want.

Dunno if Banfa neglected to add that or was referring to it in his "true array" explanation, but I figured I'd add my two cents.
Mar 7 '07 #7

Banfa
Expert Mod 5K+
P: 8,916
Dunno if Banfa neglected to add that or was referring to it in his "true array" explanation, but I figured I'd add my two cents.
Nop I forgot about it.
Mar 7 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.