修改set 容器默认的输出顺序。

getline 2008-01-22 04:00:28
在STL中文网站上看到的,一段话:
Specifying sort order
The other thing we can do when we create a set is specify the manner in which items are sorted. I could use string in this container also, but I've elected to use char * from here on instead. We are going to create a set with the same data as before, but sort it in reverse order through a functor as follows.

struct gtstr
{
bool operator()(const char * s1, const char * s2) const
{
return (strcmp(s1, s2) > 0);
}
};

now in our main function:

set<char gtstr*,> setString2;
setString2.insert("this");
setString2.insert("is");
setString2.insert("a");
setString2.insert("test");
setString2.insert("boys");
cout << endl << "setString2" << endl << endl;
copy(setString2.begin(), setString2.end(), ostream_iterator<char*>(cout, "\r\n"));

The output looks like this:
setString2
this
test
is
boys
a

As you can see, our policy has allowed the items to be sorted in reverse order. While this is a trivial case, user defined types will always require such a functor because no default sort order will be possible.

大意就是想改变set默认输出顺序吧,这里这句set<char gtstr*,> setString2;一直通不过。
我用DEV CPP编译的。。是他这个程序写得有问题还是咋的。。
...全文
255 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
getline 2008-01-22
  • 打赏
  • 举报
回复
回taodm,不是教材了。。

是学习STL的一个中文网站了:http://www.stlchina.org
可能是笔者的一个手误吧,还是谢谢你。
getline 2008-01-22
  • 打赏
  • 举报
回复
看了下set摸板:
template<class _K, class _Pr = less<_K>,
class _A = allocator<_K> >
class set {

class _Pr = less<_K> 默认是less<>排列的。
以后尽量避免这样的问题了,谢谢taodm关注,回答。。
taodm 2008-01-22
  • 打赏
  • 举报
回复
还是换本好点的纸版书作为初学者教材吧,至少下载“勘误”后,笔误可以少很多。
getline 2008-01-22
  • 打赏
  • 举报
回复
哎,谢谢taodm了。。

关键还是不晓得这些东西到底怎么实现的啊。作者一个小笔误搞得编译器报的错让人费解。
看来还是得把每个容器怎么实现的看下。。
taodm 2008-01-22
  • 打赏
  • 举报
回复
set <char *, gtstr>
兄弟,基本功不过关啊,这么显然的东西也看不出来。

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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