LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 769|回复: 6

STL编译结果出现错误

[复制链接]
发表于 2006-3-7 11:04:35 | 显示全部楼层 |阅读模式
源代码如下:先初始化list1->然后打印, 删除前后元素->然后打印, 删除全部->然后打印。

  1. #include <iostream>
  2. #include <list>
  3. #include <bits/stl_list.h>
  4. #include <string>
  5. #include <algorithm>

  6. using namespace std;

  7. int main(void)
  8. {
  9.     list<int> list1;

  10.     for (int i=0; i<10; ++i) list1.push_back(i);
  11.     list<int>::iterator Ite ;
  12.     for (Ite=list1.begin(); Ite!=list1.end(); Ite++);
  13.          cout <<"The result is:" << *Ite <<endl;

  14.     list1.pop_front();
  15.     list1.pop_back();
  16.     for (Ite=list1.begin(); Ite!=list1.end(); ++Ite);
  17.          cout <<"The result is:" << *Ite <<endl;

  18.     list1.erase(list1.begin());
  19.     list1.erase(list1.begin(), list1.end());
  20.     for (Ite=list1.begin(); Ite!=list1.end(); ++Ite);
  21.          cout <<"The result is:" << *Ite <<endl;

  22.     cout <<"List contains" << list1.size() <<"Elements" <<endl;

复制代码

编译运行的结果如下:

  1. [root@root STL]# g++ pop_back_front.cpp -o pop_back_front
  2. [root@root STL]# ./pop_back_front
  3. The result is:134523824
  4. The result is:134523824
  5. The result is:134523824
  6. List contains0Elements
  7. [root@root STL]#
复制代码

问题出在哪里呢?
发表于 2006-3-7 11:28:23 | 显示全部楼层
for (Ite=list1.begin(); Ite!=list1.end(); Ite++);
                                                                         最后有一个';'不对吧。呵,经常发生的事。
         cout <<"The result is:" << *Ite <<endl;
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-7 14:41:07 | 显示全部楼层
程序是这样的:这个程序是这样工作的:定义一个函数对象类IsAToothbrush,这个类的对象能判断出卖出的是否是牙刷 。如果这个记录是卖出牙刷的记录的话,函数调用operator()返回一个true,否则返回false。

  1. #include <iostream>
  2. #include <list>
  3. #include <bits/stl_list.h>
  4. #include <string>
  5. #include <algorithm>


  6. using namespace std;

  7. const string ToothbrushCode ("003");

  8. class IsAToothbrush     //定义一个函数类对象。
  9. {
  10. public:
  11.       bool operator () (string& SalesRecord )
  12.       {
  13.            return SalesRecord.substr(0,4) == ToothbrushCode;
  14.       }
  15. };

  16. int main(void)
  17. {
  18.     list<string> SalesRecords;

  19.     SalesRecords.push_back("001, Soap");
  20.     SalesRecords.push_back("002, Shampoo");
  21.     SalesRecords.push_back("001, Toothbrush");
  22.     SalesRecords.push_back("001, Tootthpaste");
  23.     SalesRecords.push_back("001, Toothbrush");

  24.     int NumberOfToothbrushes(0);
  25.     count_if (SalesRecords.begin(), SalesRecords.end(), IsAToothbrush(), NumberOfToothbrushes);

  26.     cout << "There were "<<NumberOfToothbrushes<<"toothbrushes sold " << endl;
  27. }
复制代码

用通用算法:count_If()时,编译出现这样的错误,是不是count_if()是Stl的新组件。

  1. [root@root STL]# g++ count_if.cpp -o count_if
  2. count_if.cpp: In function `int main()':
  3. count_if.cpp:32: error: no matching function for call to `count_if(std::_List_iterator<std::string>, std::_List_iterator<std::string>, IsAToothbrush, int&)'
  4. [root@root STL]#
复制代码
回复 支持 反对

使用道具 举报

发表于 2006-3-7 19:48:15 | 显示全部楼层
你的程序是从哪来的?
少一个 include, ext/algorithm
回复 支持 反对

使用道具 举报

发表于 2006-3-8 12:26:15 | 显示全部楼层
怎么还要 bits/stl_list.h 这个头?`#include <list>'就够了。楼上版主那个直接`#include <algorithm>’就好了,ext那个是非标准扩展。
回复 支持 反对

使用道具 举报

发表于 2006-3-8 19:47:52 | 显示全部楼层
Post by manphiz
楼上版主那个直接`#include <algorithm>’就好了,ext那个是非标准扩展。

楼主的程序中用的那个 count_if 确实是扩展

btw: 你不用那个扩展试过了吗?
回复 支持 反对

使用道具 举报

发表于 2006-3-19 11:38:29 | 显示全部楼层
楼主怎么for后门用分号了呀?
哈哈,笔误了吧~~~
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表