WildLifeMonitor.cpp VS2005编译问题

 //: C04:WildLifeMonitor.cpp
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <ctime>
using namespace std;
class DataPoint {
int x, y; // Location coordinates
time_t time; // Time of Sighting
Chapter 15: Multiple Inheritance
245
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() { return x; } should add const, because 1)
int getY() { return y; }should add const, because 1)
time_t* getTime() { return &time; }should add const, because 1)
};
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 ) { 1)
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;
static const int d = 100;
public:
SightingGen(vector<string>& an) :
animals(an) { srand(time(0)); }
Sighting operator()() {
Sighting result;
int select = rand() % animals.size();
result.first = animals[select];
result.second = DataPoint(
rand() % d, rand() % d, time(0));
return result;
Chapter 15: Multiple Inheritance
246
}
};
typedef multimap<string, DataPoint> DataMap;
typedef DataMap::iterator DMIter;
int main() {
DataMap sightings;
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:
while(true) {
cout << "select an animal or 'q' to quit: ";
for(int 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();
// 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, ""));
}
} ///:~

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值