C++编程思想 第2卷 第7章 通用容器 关联式容器 多重映像和重复的关键字

多重映像multimap是一个能包含重复的关键字map
电话号码本,同一个名字可以有很多条目

野生动植物,如需跟踪每一种有斑点的动物出现的时间和地点
可看到很多同一种类的动物
它们在不同的时间和不同的地点出现
如果动物的类型作为关键字 ,需要一个multimap

//: C07:WildLifeMonitor.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <ctime>
#include <iostream>
#include <iterator>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

class DataPoint {
  int x, y; // Location coordinates
  time_t time; // Time of Sighting
public:
  DataPoint() : x(0), y(0), time(0) {}
  DataPoint(int xx, int yy, time_t tm) :
    x(xx), y(yy), time(tm) {}
  // Synthesized operator=, copy-constructor OK
  int getX() const { return x; }
  int getY() const { return y; }
  const time_t* getTime() const { return &time; }
};

string animal[] = {
  "chipmunk", "beaver", "marmot", "weasel",
  "squirrel", "ptarmigan", "bear", "eagle",
  "hawk", "vole", "deer", "otter", "hummingbird",
};
const int ASZ = sizeof animal/sizeof *animal;
vector<string> animals(animal, animal + ASZ);

// All the information is contained in a
// "Sighting," which can be sent to an ostream:
typedef pair<string, DataPoint> Sighting;

ostream&
operator<<(ostream& os, const Sighting& s) {
  return os << s.first << " sighted at x= "
    << s.second.getX() << ", y= " << s.second.getY()
    << ", time = " << ctime(s.second.getTime());
}

// A generator for Sightings:
class SightingGen {
  vector<string>& animals;
  enum { D = 100 };
public:
  SightingGen(vector<string>& an) : animals(an) {}
  Sighting operator()() {
    Sighting result;
    int select = rand() % animals.size();
    result.first = animals[select];
    result.second = DataPoint(
      rand() % D, rand() % D, time(0));
    return result;
  }
};

// Display a menu of animals, allow the user to
// select one, return the index value:
int menu() {
  cout << "select an animal or 'q' to quit: ";
  for(size_t i = 0; i < animals.size(); i++)
    cout <<'['<< i <<']'<< animals[i] << ' ';
  cout << endl;
  string reply;
  cin >> reply;
  if(reply.at(0) == 'q') return 0;
  istringstream r(reply);
  int i;
  r >> i; // Converts to int
  i %= animals.size();
  return i;
}

int main() {
  typedef multimap<string, DataPoint> DataMap;
  typedef DataMap::iterator DMIter;
  DataMap sightings;
  srand(time(0));  // Randomize
  generate_n(inserter(sightings, sightings.begin()),
    50, SightingGen(animals));
  // Print everything:
  copy(sightings.begin(), sightings.end(),
    ostream_iterator<Sighting>(cout, ""));
  // Print sightings for selected animal:
  for(int count = 1; count < 10; count++) {
    // Use menu to get selection:
    // int i = menu();
    // Generate randomly (for automated testing):
    int i = rand() % animals.size();
    // Iterators in "range" denote begin, one
    // past end of matching range:
    pair<DMIter, DMIter> range =
      sightings.equal_range(animals[i]);
    copy(range.first, range.second,
      ostream_iterator<Sighting>(cout, ""));
  }
  getchar();
} ///:~


输出
bear sighted at x= 74, y= 64, time = Sat Sep 08 18:31:23 2018
bear sighted at x= 91, y= 45, time = Sat Sep 08 18:31:23 2018
bear sighted at x= 24, y= 90, time = Sat Sep 08 18:31:23 2018
bear sighted at x= 11, y= 29, time = Sat Sep 08 18:31:23 2018
bear sighted at x= 70, y= 99, time = Sat Sep 08 18:31:23 2018
bear sighted at x= 91, y= 6, time = Sat Sep 08 18:31:23 2018
beaver sighted at x= 56, y= 82, time = Sat Sep 08 18:31:23 2018
beaver sighted at x= 24, y= 1, time = Sat Sep 08 18:31:23 2018
chipmunk sighted at x= 84, y= 13, time = Sat Sep 08 18:31:23 2018
chipmunk sighted at x= 48, y= 53, time = Sat Sep 08 18:31:23 2018
chipmunk sighted at x= 66, y= 93, time = Sat Sep 08 18:31:23 2018
deer sighted at x= 21, y= 47, time = Sat Sep 08 18:31:23 2018
deer sighted at x= 40, y= 22, time = Sat Sep 08 18:31:23 2018
deer sighted at x= 3, y= 5, time = Sat Sep 08 18:31:23 2018
deer sighted at x= 62, y= 96, time = Sat Sep 08 18:31:23 2018
deer sighted at x= 17, y= 2, time = Sat Sep 08 18:31:23 2018
deer sighted at x= 50, y= 89, time = Sat Sep 08 18:31:23 2018
eagle sighted at x= 74, y= 52, time = Sat Sep 08 18:31:23 2018
eagle sighted at x= 88, y= 42, time = Sat Sep 08 18:31:23 2018
hawk sighted at x= 56, y= 72, time = Sat Sep 08 18:31:23 2018
hawk sighted at x= 21, y= 8, time = Sat Sep 08 18:31:23 2018
marmot sighted at x= 4, y= 48, time = Sat Sep 08 18:31:23 2018
marmot sighted at x= 4, y= 46, time = Sat Sep 08 18:31:23 2018
marmot sighted at x= 13, y= 5, time = Sat Sep 08 18:31:23 2018
marmot sighted at x= 29, y= 41, time = Sat Sep 08 18:31:23 2018
marmot sighted at x= 19, y= 60, time = Sat Sep 08 18:31:23 2018
marmot sighted at x= 86, y= 67, time = Sat Sep 08 18:31:23 2018
marmot sighted at x= 77, y= 33, time = Sat Sep 08 18:31:23 2018
marmot sighted at x= 10, y= 40, time = Sat Sep 08 18:31:23 2018
otter sighted at x= 85, y= 86, time = Sat Sep 08 18:31:23 2018
otter sighted at x= 94, y= 96, time = Sat Sep 08 18:31:23 2018
otter sighted at x= 11, y= 39, time = Sat Sep 08 18:31:23 2018
ptarmigan sighted at x= 58, y= 15, time = Sat Sep 08 18:31:23 2018
ptarmigan sighted at x= 36, y= 85, time = Sat Sep 08 18:31:23 2018
ptarmigan sighted at x= 43, y= 22, time = Sat Sep 08 18:31:23 2018
squirrel sighted at x= 32, y= 10, time = Sat Sep 08 18:31:23 2018
squirrel sighted at x= 42, y= 42, time = Sat Sep 08 18:31:23 2018
squirrel sighted at x= 46, y= 18, time = Sat Sep 08 18:31:23 2018
squirrel sighted at x= 97, y= 42, time = Sat Sep 08 18:31:23 2018
squirrel sighted at x= 58, y= 4, time = Sat Sep 08 18:31:23 2018
squirrel sighted at x= 92, y= 77, time = Sat Sep 08 18:31:23 2018
vole sighted at x= 23, y= 30, time = Sat Sep 08 18:31:23 2018
vole sighted at x= 97, y= 33, time = Sat Sep 08 18:31:23 2018
vole sighted at x= 77, y= 99, time = Sat Sep 08 18:31:23 2018
vole sighted at x= 16, y= 4, time = Sat Sep 08 18:31:23 2018
vole sighted at x= 76, y= 72, time = Sat Sep 08 18:31:23 2018
weasel sighted at x= 16, y= 45, time = Sat Sep 08 18:31:23 2018
weasel sighted at x= 20, y= 0, time = Sat Sep 08 18:31:23 2018
weasel sighted at x= 48, y= 95, time = Sat Sep 08 18:31:23 2018
weasel sighted at x= 85, y= 2, time = Sat Sep 08 18:31:23 2018
ptarmigan sighted at x= 58, y= 15, time = Sat Sep 08 18:31:23 2018
ptarmigan sighted at x= 36, y= 85, time = Sat Sep 08 18:31:23 2018
ptarmigan sighted at x= 43, y= 22, time = Sat Sep 08 18:31:23 2018
weasel sighted at x= 16, y= 45, time = Sat Sep 08 18:31:23 2018
weasel sighted at x= 20, y= 0, time = Sat Sep 08 18:31:23 2018
weasel sighted at x= 48, y= 95, time = Sat Sep 08 18:31:23 2018
weasel sighted at x= 85, y= 2, time = Sat Sep 08 18:31:23 2018
ptarmigan sighted at x= 58, y= 15, time = Sat Sep 08 18:31:23 2018
ptarmigan sighted at x= 36, y= 85, time = Sat Sep 08 18:31:23 2018
ptarmigan sighted at x= 43, y= 22, time = Sat Sep 08 18:31:23 2018
hawk sighted at x= 56, y= 72, time = Sat Sep 08 18:31:23 2018
hawk sighted at x= 21, y= 8, time = Sat Sep 08 18:31:23 2018
squirrel sighted at x= 32, y= 10, time = Sat Sep 08 18:31:23 2018
squirrel sighted at x= 42, y= 42, time = Sat Sep 08 18:31:23 2018
squirrel sighted at x= 46, y= 18, time = Sat Sep 08 18:31:23 2018
squirrel sighted at x= 97, y= 42, time = Sat Sep 08 18:31:23 2018
squirrel sighted at x= 58, y= 4, time = Sat Sep 08 18:31:23 2018
squirrel sighted at x= 92, y= 77, time = Sat Sep 08 18:31:23 2018
weasel sighted at x= 16, y= 45, time = Sat Sep 08 18:31:23 2018
weasel sighted at x= 20, y= 0, time = Sat Sep 08 18:31:23 2018
weasel sighted at x= 48, y= 95, time = Sat Sep 08 18:31:23 2018
weasel sighted at x= 85, y= 2, time = Sat Sep 08 18:31:23 2018
marmot sighted at x= 4, y= 48, time = Sat Sep 08 18:31:23 2018
marmot sighted at x= 4, y= 46, time = Sat Sep 08 18:31:23 2018
marmot sighted at x= 13, y= 5, time = Sat Sep 08 18:31:23 2018
marmot sighted at x= 29, y= 41, time = Sat Sep 08 18:31:23 2018
marmot sighted at x= 19, y= 60, time = Sat Sep 08 18:31:23 2018
marmot sighted at x= 86, y= 67, time = Sat Sep 08 18:31:23 2018
marmot sighted at x= 77, y= 33, time = Sat Sep 08 18:31:23 2018
marmot sighted at x= 10, y= 40, time = Sat Sep 08 18:31:23 2018

将观察到的所有数据都封装到一个DataPoint类中
该类非常简单足以使用综合赋值和拷贝构造函数来对其进行操作
用标准C库时间函数来记录观察的时间

猜你喜欢

转载自blog.csdn.net/eyetired/article/details/82532107