Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,960 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,590 people online right now. Registration is fast and FREE... Join Now!




vectors/arrays?

 
Reply to this topicStart new topic

vectors/arrays?, which one

manzoor
11 May, 2008 - 06:44 AM
Post #1

D.I.C Regular
Group Icon

Joined: 7 Aug, 2007
Posts: 398



Thanked: 2 times
Dream Kudos: 50
My Contributions
when to use vectors or arrays ?? and if vectors, then with iterator or without it ?

can you explain their advantages/disadvantages ? situations where one fits the best ??

thanks tongue.gif
User is online!Profile CardPM
+Quote Post

Cerolobo
RE: Vectors/arrays?
11 May, 2008 - 10:55 AM
Post #2

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
Vectors are essentially arrays that automatically grow and shrink.

Vectors are good for various things. If you are writing code, and you have to have a variable amount of memory, vectors are a quick fix. When you add elements to the vector, if it needs to expand the memory, it will. When you remove elements, if you get below a specific percent, the vector will shrink. A lot of the implementations of vectors also have various debugging features, to prevent you from doing bad things (removing to much data, accessing a bad index, ect...).

While all of the above may sound nice, there are some catches. First, there is a lot of overhead (function call overhead, addition memory, error checking). IE, a vector is a lot slower then a array. Other then that, vectors are compiler specific. The C++ standard only specifics the complexity of each vector function. So, while one compilers vector implementation may be fast, another may be really slow.

So, to cap it up

Vector
Pros
- Array Based
- Dynamic
- Error checking (compiler specific, so there might not be any)

Cons
- A lot of overhead/Slow (compared to a array)
- Complier specific implementations


QUOTE
if vectors, then with iterator or without it ?

This is more of a personal opinion. I personally prefer to just use indexes, since its a lot less typing, a lot more array like, and a lot more memory efficient. The differences of access the data via a index and a iterator is going to be compiler specific, so I can't really go into the specifics.

So, to cap everything up.

You should use a vector when
- You need a variable sized of array, and want to write minimal code

You should use a array when
- You need the fastest possible code
- You don't need a dynamically sized array
User is offlineProfile CardPM
+Quote Post

manzoor
RE: Vectors/arrays?
11 May, 2008 - 11:22 AM
Post #3

D.I.C Regular
Group Icon

Joined: 7 Aug, 2007
Posts: 398



Thanked: 2 times
Dream Kudos: 50
My Contributions
thanks man biggrin.gif
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:26AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month