简单的代码不能运行
在看The C++ Standard Template Library的时候写了两段小小的测试代码,居然不能运行...帮忙看下为什么...我找不出了....
1.
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
typedef multimap<int,string> IntStringMMap;
IntStringMMap coll;
coll.insert(make_pair(5,"tagged"));
coll.insert(make_pair(2,"a"));
coll.insert(make_pair(1,"this"));
coll.insert(make_pair(4,"of"));
coll.insert(make_pair(6,"strings"));
coll.insert(make_pair(1,"is"));
coll.insert(make_pair(3,"multimap"));
IntStringMMap::iterator pos;
for(pos=coll.begin();pos!=coll.end();++pos)
{
cout<<pos->second<<' ';
}
cout<<endl;
return 0;
}
--------------------------------------------------------------
2.
#include <iostream>
#include <utility>
using namespace std;
class Pair
{
int x;
int y;
public:
bool operator< (Pair& p);
bool operator == (Pair& p);
friend istream& operator >> (istream& input,Pair& p);
};
bool Pair::operator < (Pair& p)
{
return (x<p.x)&&(y<p.y);
}
bool Pair::operator == (Pair& p)
{
return (x==p.x)&&(y==p.y);
}
istream& operator >> (istream& input,Pair& p)
{
input>>p.x>>p.y;
return input;
}
int main()
{
using namespace std::rel_ops;
Pair x,y;
cout<<"Please enter x and y: "<<endl;
cin>>x>>y;
if(x<y)
cout<<"x<y"<<endl;
if(x<=y)
cout<<"x<=y"<<endl;
if(x!=y)
cout<<"x!=y"<<endl;
if(x>y)
cout<<"x>y"<<endl;
if(x>=y)
cout<<"x>=y"<<endl;
return 0;
}
问题点数:20、回复次数:5Top
1 楼cunsh(村少)回复于 2005-12-07 00:54:15 得分 0
coll.insert(make_pair(5,string("tagged")));Top
2 楼cunsh(村少)回复于 2005-12-07 01:03:52 得分 10
bool operator< (const Pair& p)const;
bool operator == (const Pair& p)const;Top
3 楼Rick_ang(东方未名)回复于 2005-12-07 12:31:25 得分 0
第2个还是不行啊..Top
4 楼codearts(代码艺术)回复于 2005-12-07 23:21:53 得分 10
2错在这里:
if(x<y)
cout<<"x<y"<<endl;
if(x<=y) //这里错了,你没重载 <=
cout<<"x<=y"<<endl;
if(x!=y) //如前
cout<<"x!=y"<<endl;
if(x>y)
cout<<"x>y"<<endl;
if(x>=y)
cout<<"x>=y"<<endl;
Top
5 楼Rick_ang(东方未名)回复于 2005-12-14 14:14:50 得分 0
2是The C++ Standard Libary 中的例子
在main中用了
using namespace std::rel_ops;
据说这个是用template写的重载函数啊~~如果你定义了x==y和x<y就自动有x!=y和x<=y啊...难道是书上说错了么?Top




