C++ Tutorial/string/string array — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
(нет различий)
|
Текущая версия на 13:30, 25 мая 2010
String arrays
<source lang="cpp">#include <iostream>
- include <string>
using namespace std; int main() {
const int MAX_ITEMS = 10; string inventory[MAX_ITEMS]; int numItems = 0; inventory[numItems++] = "A"; inventory[numItems++] = "B"; inventory[numItems++] = "C"; for (int i = 0; i < numItems; ++i) cout << inventory[i] << endl; inventory[0] = "battle axe"; for (int i = 0; i < numItems; ++i) cout << inventory[i] << endl; cout << inventory[0] << "" has "; cout << inventory[0].size() << " letters in it.\n"; if (numItems < MAX_ITEMS) inventory[numItems++] = "healing potion"; else cout << "You have too many items and can"t carry another."; for (int i = 0; i < numItems; ++i) cout << inventory[i] << endl; return 0;
}</source>