CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  新手乐园

简单的代码不能运行

楼主Rick_ang(东方未名)2005-12-07 00:03:39 在 C/C++ / 新手乐园 提问

在看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

相关问题

  • 简单问题:为什么我的下载的C#代码不能运行??
  • 请给个能运行的简单的CS源代码例子.
  • 为什么我的这段javascript代码不能运行啊?
  • 帮我看看java为什么不能运行(源代码)
  • 急:下面代码为什么不能运行,为什么?
  • 在VBScript中用option explicit后,代码为什么不能运行
  • 我下的C# ASP。NET 源代码 怎么不能运行的?
  • 请问不能运行下面那段代码
  • 为什么在javaBean中的线程不能运行?(代码如下)
  • 这么一小段的源代码我也不能运行,为什么呀.

关键词

  • coll
  • pair
  • intstringmmap
  • insert
  • pos
  • make
  • cout
  • input
  • using namespace std
  • include

得分解答快速导航

  • 帖主:Rick_ang
  • cunsh
  • codearts

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo