// Inventory Displayer // Demonstrates constant references #include #include #include using namespace std; // Function Prototypes: // // Notice: parameter vec is a constant reference to a vector of strings void display(const vector& inventory); int main() { vector inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); display(inventory); return 0; } // parameter vec is a constant reference to a vector of strings // pass by reference here code save time as it does not need to allocate // memory to make a copy of the inventory vector (assume it is really large) // The use of const means that the values in the vector cannot be // changed by this function... it works as a failsafe feature // to prevent intelligence failures void display(const vector& vec) { cout << "Your items:\n"; for (vector::const_iterator iter = vec.begin(); iter != vec.end(); ++iter) { cout << *iter << endl; } }