看Effective STL Item21时遇到的小问题

kingofark 2002-04-25 11:44:26
以下是我看Scott Meyers 《Effective STL》Item21的时候,顺手写的例子程序以实验文章中corrupt container的方法。例子程序中用到了STL泛型算法algorithm中的for_each()函数,但偶然发现用Borland C++ Compiler 5.5编译时,不需要#include<algorithm>就可以编译运行通过;而MSVC6的编译器则很正常,一定需要#include<algorithm>才能编译成功。

哪位大虾知道其中奥妙因果,请不吝赐教。谢谢!

// Test demo according to Effective STL Item 21:
// Always have comparison function return 'false' for equal values.
//
// "... the set concludes that 10A and 10B are NOT equivalent,
// hence NOT the same, and it thus goes about inserting 10B into
// the container alongside 10A. Technically, this action yields
// undefined behavior, but the nearly universal outcome is that
// the set ends up with TWO copies of the value 10, and that means
// it's not a set any longer. By using less_equal as our comparison
// type, we've corrupted the container!"
// ---- Scott Meyers

#include <iostream> // cout
#include <set> // set
#include <functional> // less_equal

// In the case of Borland C++ Compiler 5.5 (BCB5/bcc32.exe),
// the demo still compiles even if we don't add this #include
// for 'for_each', while the help says (the help of course does)
// we must #include<algorithm> for the use of 'for_each'.
// In the case of Visual C++ Compiler (MSVC6/cl.exe), we must
// #include<algorithm> for the use of 'for_each' or we'll get
// error message during compilling.
#include <algorithm> // for_each

using namespace std;

typedef set< int, less_equal<int> > Int_set;

void printout(int& i)
{
cout << i << " ";
}

int main()
{
Int_set s; // s is sorted by "<="

s.insert(10); // insert the value 10

// print out all of 's'
cout << "First insertion: ";
for_each(s.begin(), s.end(), printout);
cout << endl;

s.insert(10); // now try inserting 10 again

cout << "Second insertion: ";
for_each(s.begin(), s.end(), printout);
cout << endl;

return 0;
}
...全文
136 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingofark 2002-04-27
  • 打赏
  • 举报
回复
// Possible reason for BCC55's case:
// In set.h, there's "#include <rw/tree>",
// and in tree.h from 'rw' directory,
// there's "#include <algorithm>",
// thus make 'algorithm' implicitly functions.

我仔细查了一下BCB5目录下的Include目录:
set.h里面有“#include <rw/tree>”;
rw目录的tree.h里面有“#include <algorithm>”;
BCC55似乎就是因此才可以在主程序没有#include <algorithm>的情况下编译通过的。

不知道我说的对不对。请有意者大家明察。谢谢!

再次谢谢大家!
kingofark 2002-04-26
  • 打赏
  • 举报
回复
> 这个问题去看看bcc55的stl实作就可以了,
> 说不定在某个头文件中有#include<algorithm>这条指令

我大略的找过,好像也没找到——当然也可能是我看花了眼。

谢谢大家的关心!
cber 2002-04-26
  • 打赏
  • 举报
回复
这个问题去看看bcc55的stl实作就可以了,说不定在某个头文件中有#include<algorithm>这条指令
chrisXP 2002-04-26
  • 打赏
  • 举报
回复
mark
babysloth 2002-04-26
  • 打赏
  • 举报
回复
其实这跟编译器倒没什么关系,主要是STL实作的问题。BCB5用的RogueWave STL,不够标准。BCB6采用了STLport,就不存在上述的问题了。
garfield_82 2002-04-26
  • 打赏
  • 举报
回复
和tc中不用包含<stdio.h>就能用printf and scanf是不是一样的道理?
garfield_82 2002-04-26
  • 打赏
  • 举报
回复
都是五星级以上的,自卑中.

70,021

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧