// vectortest.cpp : Defines the entry point for the console application. //
#include "stdafx.h" #include <iostream> #include <vector>
using namespace std;
class Monster { public: int id; char name[50]; void display(void); };
void Monster::display(void) { cout << "ID: " << id << ", Name: " << name << endl; }
void changeID(Monster &monster, int id) { monster.id = id; }
void changeName(Monster &monster, char *name) { strcpy_s(monster.name, name); }
int _tmain(int argc, _TCHAR* argv[]) { vector<Monster> monsters; Monster monster;
int i; for (i = 0; i < 10; i++) { changeID(monster, i + 1); sprintf_s(monster.name, "Monster%d", monster.id); //changeName(monster, "haha"); monsters.push_back(monster); }
for (i = 0; i < 10; i++) { monsters[i].display(); }
changeName(monster, "haha"); cout << endl << endl; changeName(monsters[5], "New Name"); for (i = 4; i <= 6; i++) { monsters[i].display(); }
system("pause"); return 0; }
|