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.