ex10_03.cpp

来自「Wrox.Ivor.Hortons.Beginning.Visual.C.Plu」· C++ 代码 · 共 47 行

CPP
47
字号
// Ex10_03.cpp
// Storing pointers to objects in a vector

#include <iostream>
#include <vector>
#include "Person.h" 

using std::cin;
using std::cout;
using std::endl;
using std::vector;

int main()
{
  vector<Person*> people;               // Vector of Person objects
  const size_t maxlength = 50;
  char firstname[maxlength];
  char secondname[maxlength];
  while(true)
  {
    cout << "Enter a first name or press Enter to end: ";
    cin.getline(firstname, maxlength, '\n'); 
    if(strlen(firstname) == 0)
      break;
    cout << "Enter the second name: ";
    cin.getline(secondname, maxlength, '\n'); 
    people.push_back(new Person(firstname, secondname));
  }

  // Output the contents of the vector
  cout << endl;
  vector<Person*>::iterator iter = people.begin();  
  while(iter != people.end())
    (*(iter++))->showPerson();

  // Release memory for the people
  iter = people.begin();
  while(iter != people.end())
    delete *(iter++);

  // Pointers in the vector are now invalid
  // so remove the contents
  people.clear();

  return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?