免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1934 | 回复: 5
打印 上一主题 下一主题

求教: for_each跟STL问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-10-02 09:44 |只看该作者 |倒序浏览
我有个class A;

void print(pair<string, A*>; p)
{
    p.second->;print();
}


pair<string, A*>; mypair;
map<string, A*>; mymap;

for_each(mymap.begin(), mymap.end(), print);

~~~~~~~~~~~~~~~~~~

在g++编译后没问题,但是在CC下就会提示
instantiated from non-template code;

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2005-10-02 09:50 |只看该作者

求教: for_each跟STL问题

void print(string &p)
{
   cout<<p<<endl;
}

论坛徽章:
0
3 [报告]
发表于 2005-10-02 15:54 |只看该作者

求教: for_each跟STL问题

仍然提示下面这行有错误,
for_each(mymap.begin(), mymap.end(), print);

还是这个错:
Where: Instantiated from non-template code.

@@

论坛徽章:
0
4 [报告]
发表于 2005-10-02 19:03 |只看该作者

求教: for_each跟STL问题


  1. #include <iostream>;
  2. #include <cstdlib>;
  3. #include <algorithm>;
  4. #include <map>;
  5. #include <functional>;
  6. #include <string>;


  7. using namespace std;

  8. //for_each要求print functor是一个从unary_function继承而来的。
  9. //unary_function在这里使用TYPE作为模板参数,返回void类型,
  10. //返回值的类型也是unary_function要求的。
  11. template <class TYPE>;
  12. struct print  : public unary_function<TYPE, void>;
  13. {
  14.         void operator()(TYPE& x)
  15.         {
  16.                 cout  << x.first << "\t"<< *(x.second) << endl;
  17.         }
  18. };

  19. //逐项清除动态分配的对象,防止内存泄漏
  20. template <class TYPE>;
  21. struct deleteFrom : public unary_function<TYPE, void>;
  22. {
  23.         void operator()(TYPE& x)
  24.         {
  25.                 delete x.second;
  26.         }
  27. } ;


  28. class Test
  29. {
  30.          //前两个友元函数无用,只是考虑Test作为键时Test必须实现的
  31.         friend bool operator<(const Test& test1, const Test& test2);
  32.         friend bool operator==(const Test& test1, const Test& test2);
  33.          //输出流运算符重载
  34.         friend ostream& operator<<(ostream& os, const Test& test);
  35. public:
  36.         Test(int val = 0)
  37.         {
  38.                 value_ = val;
  39.         }
  40.        
  41. private:
  42.         int value_;       
  43. };

  44. bool operator<(const Test& test1, const Test& test2)
  45. {
  46.         return (test1.value_ < test2.value_);
  47. }

  48. bool operator==(const Test& test1, const Test& test2)
  49. {
  50.         return (test1.value_ == test2.value_);
  51. }

  52. ostream& operator<<(ostream& os, const Test& test)
  53. {
  54.         os << test.value_ << " ";
  55.         return os;
  56. }

  57. int main(int argc, char *argv[])
  58. {

  59.   map<string, Test*>; mymap;
  60.         string str = "first";
  61.         mymap[str] = new Test(1);
  62.         str = "second";
  63.         mymap[str] = new Test(2);
  64.         str = "third";
  65.         mymap[str] = new Test(3);
  66.         /**
  67.         *        假如你使用
  68.         * mymap["first"] = new Test(1);
  69.         * mymap["second"] = new Test(2);
  70.         * mymap["third"] = new Test(3)
  71.         * 这时
  72.         * 要注意const属性
  73.         * for_each应当写成
  74.         * for_each(mymap.begin(), mymap.end(), print< map< const string, Test*>;::value_type >; () );
  75.         **/
  76.   //map的每一项是一个<键,值>;对
  77.         for_each(mymap.begin(), mymap.end(), print< map<  string, Test* >;::value_type >;());
  78.         for_each(mymap.begin(), mymap.end(), deleteFrom< map< string, Test *>;::value_type>;());
  79.         mymap.clear(); //这句可以不调用,mymap析构函数应当自动调用。
  80.         cout << "Now mymap has " << mymap.size() << " items"<< endl;
  81.   system("PAUSE");       
  82.   return 0;
  83. }

复制代码

论坛徽章:
0
5 [报告]
发表于 2005-10-02 19:33 |只看该作者

求教: for_each跟STL问题

[quote]原帖由 "叨叨323"]pair<string, A*>; mypair[/quote 发表:

pair是什么?

论坛徽章:
0
6 [报告]
发表于 2005-10-03 10:29 |只看该作者

求教: for_each跟STL问题



   /**
   *   假如你使用
   * mymap["first"] = new Test(1);
   * mymap["second"] = new Test(2);
   * mymap["third"] = new Test(3)
   * 这时
   * 要注意const属性
   * for_each应当写成
   * for_each(mymap.begin(), mymap.end(), print< map< const string, Test*>;::value_type >; () );


Thanks very much, you solve my problem.

the little "const"!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP