| 发表于: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编译的。。是他这个程序写得有问题还是咋的。。 |
|
|
|
|