如何沿着這些路線的東西:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <sstream>
class Range
{
public:
Range(double lower, double upper) : lower_(lower), upper_(upper) {};
Range(const Range& rhs) : lower_(rhs.lower_), upper_(rhs.upper_) {};
explicit Range(const double & point) : lower_(point), upper_(point) {};
Range& operator=(const Range& rhs)
{
lower_ = rhs.lower_;
upper_ = rhs.upper_;
return * this;
}
bool operator < (const Range& rhs) const
{
return upper_ <= rhs.lower_;
}
double lower_, upper_;
};
typedef std::string Thing;
typedef std::map<Range, Thing> Things;
std::string dump(const std::pair<Range,Thing> & p)
{
stringstream ss;
ss << "[" << p.first.lower_ << ", " << p.first.upper_ << ") = '" << p.second << "'" << endl;
return ss.str();
}
int main()
{
Things things;
things.insert(std::make_pair(Range(0.0, 5.0), "First"));
things.insert(std::make_pair(Range(5.0, 10.0), "Second"));
things.insert(std::make_pair(Range(10.0, 15.0), "Third"));
transform(things.begin(), things.end(), ostream_iterator<string> (cout,""), dump);
cout << "--------------------------------------" << endl;
things[Range(1.5)] = "Revised First";
transform(things.begin(), things.end(), ostream_iterator<string> (cout,""), dump);
return 0;
}
...程序的輸出:
[0, 5) = 'First'
[5, 10) = 'Second'
[10, 15) = 'Third'
--------------------------------------
[0, 5) = 'Revised First'
[5, 10) = 'Second'
[10, 15) = 'Third'
是區域相交? – 2009-04-21 16:40:37
不,他們不應該 – sdg 2009-04-21 17:14:08