STL源码 怎一“妙”字了得 (运用高手请进)

ambition2005 2004-03-18 12:35:13
最近在看《STL源码剖析》

发觉STL的实现果然精妙,难怪,喉结说:
“我的确认为99.99%的程序员所写的程序,在SGI STL面前都是三流水准”

可是妙归妙,我该怎么样来运用这些技巧呢?
请高手不惜指教

比如:
list里sort算法的实现:

template <class T, class Alloc>
void list<T, Alloc>::sort() {

if(node->next == node || link_type(node->next)->next == node)
return;

list<T, Alloc> carry;
list<T, Alloc> counter[64];
int fill = 0;
while (!empty()) {
carry.splice(carry.begin(), *this, begin());
int i = 0;
while (i < fill && !counter[i].empty()) {
counter[i].merge(carry);
carry.swap(counter[i++]);
}
caryy.swap(counter[i]);
if (i == fill) ++fill;
}

for(int i = 1; i < fill; ++i)
counter[i].merge(counter[i - 1]);
swap(counter[fill - 1]);
}

《STL源码剖析》P.142

其是用quick sort来实现的,我是看了整整一节课才看懂啊
看懂后,感慨: 他怎么就能想到的呢?!

我该怎么运用这种技巧,或方法呢?
如果只图看懂,而不去用,那还不如不看:(

请高手就以该排序算法为例,来讲讲 如何在实际中运用

谢谢了
...全文
57 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
superever 2004-03-22
  • 打赏
  • 举报
回复
受益非浅!
ambition2005 2004-03-21
  • 打赏
  • 举报
回复
自己顶一下:)
xjp6688 2004-03-20
  • 打赏
  • 举报
回复
ambition2005 2004-03-20
  • 打赏
  • 举报
回复
一牛人提供了一个排序的网站,相当不错
http://www.users.csbsju.edu/~cburch/proj/sortanim/index.html

我将其中的算法都实现了一下

发觉,看 与 做 是完全不一样的

只有自己去写了,才能体味到其中的“味”
fangrk 2004-03-19
  • 打赏
  • 举报
回复
对于一般人而言,只要知道如何运用这些工具就行了,至于这些工具内部如何运作不是必要的。其中很多算法都很精妙,不过我也感觉单独几个写得不是很好,可能是还没有领会到书上的奥妙。

比如P229中在RB-Tree中搜索元素,我就觉得没有必要等到x==0才退出循环,我觉得可以这么写:

while(x!=0){
if(key_compare(k,key(x)) x=left(x);
else if(key_compare(key(x),k) x=right(x);
else return iterator(x);
}
return end();


如果你要实现一个功能而现成的没有,那么自己动手编写函数的话就有用了。比如要求你使用C语言来编写某个函数,那你就可以利用STL的思想来打造自己的工具了。

当然,读一遍对数据结构也有很好的复习作用。对于大多数人来说,不看此书也能较好的运用STL。熟能生巧,运用多了便左右逢源,手到擒来,融会贯通——我自己还远远没有达到这个地步。
oo 2004-03-19
  • 打赏
  • 举报
回复
在实际应用中,因为要排序的数据不同,数据量不同,还有可用的资源(cpu速度,ram数量)不同,选的算法都会不一样的,就算一样的算法,也会在某些地方有侧重的。
ambition2005 2004-03-19
  • 打赏
  • 举报
回复
fangrk(加把油,伙计!)

你好,那算法是归并算法,不是什么快速

你看了三遍?
有什么感想啊?

书上的那些是很精妙,可是,我该怎么来运用到自己的程序中呢?

觉得那些太“悬了” :(
yjh1982 2004-03-19
  • 打赏
  • 举报
回复
to fangrk(加把油,伙计!) :
同感!我相信list的排序算法是错的!list长度超过64+63+...1就会溢出.
请参考VC6.0的代码.
Darkay_Lee 2004-03-19
  • 打赏
  • 举报
回复
STL不是一个人,一天写成的,都是些学计算机理论专业的人写的啦。
fangrk 2004-03-19
  • 打赏
  • 举报
回复
老实说,我看了三遍书,这个算法没看懂。
侯捷说是quick sort,但我觉得像是merge sort。
quick sort要求随机迭代器,list没有随机的,而且代码中没有对迭代器进行比较的代码。至于counter[64];我不知道这个64是怎么出来的,而且while循环中也没有和64比较/判断的代码。

while循环比较难却没有注释,还有rotate的随机迭代器版本我也没看懂,侯捷也没有注解。
ambition2005 2004-03-19
  • 打赏
  • 举报
回复
谢谢大家的回复

我是想问:如何从别人的优秀代码中学到好的,要学了能用

太谢谢了

更正:书上错了,这个不是什么quick sort,喉结说错了
yzwpf 2004-03-18
  • 打赏
  • 举报
回复
找到一个:
// reverse.cpp
//
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
//Initialize a vector with an array of ints
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
vector<int> v(arr, arr+10);
//Print out elements in original (sorted) order
cout << "Elements before reverse: " << endl << " ";
copy(v.begin(),v.end(),
ostream_iterator<int,char>(cout," "));
cout << endl << endl;
//Reverse the ordering

reverse(v.begin(), v.end());
//Print out the reversed elements
cout << "Elements after reverse: " << endl << " ";
copy(v.begin(),v.end(),
ostream_iterator<int,char>(cout," "));
cout << endl;
return 0;
}

Program Output

Elements before reverse:
1 2 3 4 5 6 7 8 9 10
Elements after reverse:
10 9 8 7 6 5 4 3 2 1
A reverse_copy to cout:
1 2 3 4 5 6 7 8 9 10

Warnings

If your compiler does not support default template parameters, then you always need to supply the Allocator template argument. For instance, you need to write:

vector<int, allocator<int> >

instead of:

vector<int>

If your compiler does not support namespaces, then you do not need the using declaration for std.
yzwpf 2004-03-18
  • 打赏
  • 举报
回复
估计楼主问的是如何使用STL,例如如何用STL实现
char list[5][4] = { "cat", "car", "cab", "cap", "can" };
的排序。
最好能把简单的可运行源码贴出来。

继续关注。
lovelifelaugh 2004-03-18
  • 打赏
  • 举报
回复
>这个sort也不一定是一次就写成这样的

这句话说得很实际:)
lovelifelaugh 2004-03-18
  • 打赏
  • 举报
回复
写一个搜索、排序程序,对一个程序员来说,并不是很容易的事。-- 程序设计实践
柯本 2004-03-18
  • 打赏
  • 举报
回复
如果只是排序,我还是喜欢用C的qsort
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int sort_function( const void *a, const void *b);
char list[5][4] = { "cat", "car", "cab", "cap", "can" };

int main(void)
{
int x;

qsort((void *)list, 5, sizeof(list[0]), sort_function);
for (x = 0; x < 5; x++)
printf("%s\n", list[x]);
return 0;
}

int sort_function( const void *a, const void *b)
{
return( strcmp((char *)a,(char *)b) );
}
wingfiring 2004-03-18
  • 打赏
  • 举报
回复
你是要研究排序算法本身呢,还是研究编程序的技巧?
当然,不可能人人都一下子写出这样好的代码来(这个sort也不一定是一次就写成这样的),如果懂得quick sort,看懂应该不是什么问题吧?
依我来看,你要是想写排序算法,还是多研究算法本身;要是只想运用,还是看看别人都怎么运用好了。
yjh1982 2004-03-18
  • 打赏
  • 举报
回复
喉结?
quick sort写得太囉索了.性能未必高.

24,855

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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