Thread: Arrays vs Vectors

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Arrays vs Vectors

    A vector is a dynamic array, as it can shrink and grow in size when needed, so I was just wondering if a vector is just a addition to the array, hense a array with lots more features, why would you want to use an array at all if vectors are so much more powerful thab a standard array which you are ( mot of the time ) limited to the amount of data it can hold? I have always used vectors in game programming, as they are much more friendly.

    example of an array

    Code:
    const int MAX_ITEMS = 10   // defines max items
    
    string inventory[MAX_ITEMS];	// string array holding max items 
     		
     int numItems = 0;	// player has no items at the start
     		
    // give player some items using the array
     		
    inventory[numItems++] = "Sword";
    inventory[numItems++] = "Sheild";
    inventory[numItems++] = "Armor";
     		
    // display the items
     		
    for ( int i = 0; i < numItems; ++i )
     		
    cout << inventory[i] << endl;	// use inventory array
     		
    cout << "\n\nYou trade your sword for an axe\n\n";
     		
    inventory[0] = "Axe";	// change Sword for Axe
     		
    cout << "Your items:\n\n";
     		
    for ( int i = 0; i < numItems; ++i )
     		
    cout << inventory[i] << endl;
     		
    cout << "\n\nItems name " << inventory[0] << " has " << inventory[0].size() << " letters" << endl;
     		
    cout << "\nYou find a healing potion!\n\n";
     		
    if ( numItems < MAX_ITEMS )	// check if there is space
     		
    inventory[numItems++] = "Potion";	// if ok add item 
     		
    else	// no space
     		
    cout << "\nYou have too many items and cannot carry another"; 
     		
    cout << "Your items:\n\n";
     		
    for ( int i = 0; i < numItems; ++i )
     		
    cout << inventory[i] << endl;
    same sort of code using a vector

    Code:
    vector < string > inventory;
     		
    inventory.push_back("Sword");
    inventory.push_back("Armor");
    inventory.push_back("Sheild");
     		
    cout << "You have " << inventory.size() << " items" << endl;
     		
    cout << "\nYout items:\n\n";
     		
    for ( int i = 0; i < inventory.size(); ++i )
     		
    cout << inventory[i] << endl;
     		
    cout << "\nYou trade your sword for a battle axe\n\n";
     		
    inventory[0] = "Battle Axe";
     		
    cout << "Your items:\n\n";
     		
    for ( int i = 0; i < inventory.size(); ++i )
     		
    cout << inventory[i] << endl;
     						 
    cout << "\nYour sheild is destroyed!\n\n";
        
    inventory.pop_back();
        
    cout << "Your items:\n\n";
        
    for ( int i = 0; i < inventory.size(); ++i )
        
    cout << inventory[i] << endl;
        
    cout << "\nYou lose all items!\n\n";
        
    inventory.clear();
        
    if ( inventory.empty() )
    {
        cout << "\nYou have nothing" << endl;
    }
    		 	 
    else
    {
       cout << "\nYou have 1 item left" << endl;
    }
    It just seems to me that using a vector over a standard array is
    better. But this is only my reckoning.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    It is better to use vectors over arrays. In all cases.

    Arrays are still nessassary to learn though. This is because they're completely fundemental and found in almost all programming languages. Plus, there is still a lot of old code out there that contains arrays. If students didn't learn them, they'd have no idea what to do when they encountered them.
    Sent from my iPadŽ

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    All cases? Never say that Sly you should know by now there is not an all cases in anything C++
    Woop?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    It took me about a minute to decide whether or not to post that.

    Perhaps I should better say: In almost all cases, there is a standard container in C++ that works more efficiently than an array.

    Arrays play there part in small situations. For instance, if you had to map only a Boolean value to the index, an array of two elements would perhaps be more efficient than a container object simply because the reduction of overhead.
    Last edited by SlyMaelstrom; 05-03-2006 at 02:31 AM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Basically, you are correct. The vector (or some other container) is almost always a better choice because it is safer, more robust, and often easier to use.

    However, since vector is a dynamic array, you don't necessarily need its "power" if all you need is a simple constant sized local array. I would probably use a vector in those cases anyway (or std::tr1::array), but I wouldn't say its a bad idea to use the C style array.

    And in some (probably more rare than you think) situations, you might get a performance boost or executable size decrease from using the local array over the dynamic array used to implement a vector. If your situation requires maximum efficiency or minimum executable size, then you might consider those reasons for using the local array.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Vectors are based directly off arrays (resizing when necessary to maintain the illusion of dynamic-ity) and trades off speed for code readability and ease of use.

    I once ran a benchmark of new/delete versus vector, and the vector performed comparably to arrays in terms of initialization and access, sometimes outperforming arrays altogether. This is probably due to the compiler optimizing vector code, as a second benchmark compiled with high optimization showed a slightly larger array-vector performance gap.

    Also, the vector class uses optimized algorithms in itself.

    Moral? Use vectors when you want them, and arays when you need them.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Vectors versus normal arrays
    By _izua_ in forum C++ Programming
    Replies: 10
    Last Post: 08-12-2007, 01:59 AM
  2. vectors vs c style arrays
    By markucd in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2006, 11:11 AM
  3. byte arrays & vectors
    By kasun in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 09:10 AM
  4. arrays or vectors
    By Geo-Fry in forum C++ Programming
    Replies: 26
    Last Post: 04-17-2003, 07:08 PM
  5. arrays and vectors
    By volk in forum C++ Programming
    Replies: 1
    Last Post: 03-30-2003, 03:45 PM