Arrays vs. Vectors??

what advantages do arrays have over vectors? from what i have been reading, it seems that vectors can do everything that an array can do, but alot easier. so why would anybody use arrays instead of vectors?
Sometimes, a vector is overkill. A vector does do everything an array does, but lets you add new elements beyond its current size, resize it, search through it, erase elements, etc. etc. All these things are nice (heck, that's why the vector class was made), but not always necessary.

Let's say I want to have an array of strings holding the names of the months:

string months[12] = {"January", "February", "March", /* ... */ "December" }; // I think this will compile?

I know they will be constant - I don't need to add new elements, erase current elements, sort, resize, etc. etc...so why include them?
Not really, a vector can not do everything an array can. A vector is a container class while an array is an allocated memory.

You can directly access an element of an array from memory while in vector or other container classes, you need to use certain methods. Do not confuse this with the random access operation "[]".

Here is a small example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <vector>

using namespace std;

int main(void)
{
    vector<int> v;
    int a[10];
    
    for(int i = 0; i < 10; ++i){
        v.push_back(i);
        a[i] = i;
    }
    
    for(int i = 0; i < 10; ++i){
        cout<<" "<<*(v + i);    //This will not and should not compile because v is a container class. It's not possible to perform direct address accessing.
    }
    
    for(int i = 0; i < 10; ++i){
        cout<<" "<<*(a + i);    //This is ok.
    }
    
    return 0;
}
@Ganon11

If that is the case, I would rather recommend to use enumerated types instead.
True, but I used an array in this example purely for...well, an example. There are other uses for plain arrays without the need for vector functionality.
@Vince

you can directly access an index in a vector by "v[index]" right? why is this if you say it's not possible to perform direct address accessing? what is the difference between that and how you tried to access the vector? correct me if im wrong, but i thought i remember reading that this was perfectly ok to do when researching about vectors a couple nights ago.
Last edited on
Look at the code I posted above. To see for yourself, try and compile it. Compile it again by replacing "*(v + i)" with v[i] instead.

Like I said, do not confuse the random access operation(square brackets) to a direct address access operation(asterisk).

Try to remember addressing operations(pointers and arrays).

Consider passing a vector vs. passing an array to a parameter. I hope that you learned that in C, everything is passed by value. But this is not true in an array's case. If you pass an array to a parameter, you do not pass the value, you pass the address of the first element. So you do not make a copy.

Here is a sample code to back that theory:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*This is C, not C++*/
#include <stdio.h>

/*note that the parameter can be int [].*/
void modify(int* a){

    int i;

    for(i = 0; i < 10; ++i)
        a[i] *= 2;
}

int main(void){

    int a[10];
    int i;

    for(i = 0; i < 10; ++i)
        a[i] = i;

    /*prints "0 1 2 3 4 5 6 7 8 9"*/
    for(i = 0; i < 10; ++i)
        fprintf(stdout, "%d ", a[i]);

    printf("\n");
    modify(a);

    /*prints "0 2 4 6 8 10 12 14 16 18"*/
    for(i = 0; i < 10; ++i)
        fprintf(stdout, "%d ", a[i]);

    return 0;
}


In C++, references were introduced so passing by value is not a problem for vectors anymore. But generally, if both arrays and vectors are passed to an argument in the same manner, vectors will be less efficient because of copying. Top of that, you may not be able to modify the values of the original vector passed to the function.

Vectors are provided to make it easy to perform array operations without the worry of getting many problematic exceptions(exceptions still occur in vectors though). If you are programming Java, it is pretty much an Arraylist.

If you program in lower levels, you'll see why, in some cases, arrays are preferred(because of address access) than other container classes.

EDIT:
I forgot to mention that operators in C++ are overloaded. The operator[]() of a vector works differently than an array's square brackets. Both, however, provides the same general purpose.

An array's square brackets can be used like this:
1
2
3
//some code
if(a[2] == *(a + 2))
//more code 
Last edited on
For simpler understanding array is static and vector is dynamic, which means the allocated memory for the vector can grow at any time by adding new elements but for array if u have fixed the array u cant add new elements any more to it.
Topic archived. No new replies allowed.