您的位置:程序门 -> c/c++ -> c++ 语言



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


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


修改set 容器默认的输出顺序。[已结贴,结贴人:getline]
发表于:2008-01-22 16: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编译的。。是他这个程序写得有问题还是咋的。。
发表于:2008-01-22 16:01:561楼 得分:20
set   <char     *,   gtstr>
兄弟,基本功不过关啊,这么显然的东西也看不出来。      
发表于:2008-01-22 16:05:442楼 得分:0
哎,谢谢taodm了。。

关键还是不晓得这些东西到底怎么实现的啊。作者一个小笔误搞得编译器报的错让人费解。
看来还是得把每个容器怎么实现的看下。。
发表于:2008-01-22 16:10:263楼 得分:0
还是换本好点的纸版书作为初学者教材吧,至少下载“勘误”后,笔误可以少很多。
发表于:2008-01-22 16:12:354楼 得分:0
看了下set摸板:
template <class   _k,   class   _pr   =   less <_k> ,
class   _a   =   allocator <_k>   >
class   set   {

class   _pr   =   less <_k>   默认是less <> 排列的。  
以后尽量避免这样的问题了,谢谢taodm关注,回答。。
发表于:2008-01-22 16:14:315楼 得分:0
回taodm,不是教材了。。

是学习stl的一个中文网站了:http://www.stlchina.org
可能是笔者的一个手误吧,还是谢谢你。


快速检索

最新资讯
热门点击