re: 與臨時對象的斗爭(下) Wang Feng 2009-12-07 11:36
ans = a+b+c+d+e+f+....;
如果可以并行的話,這樣很有優勢
ans = ((a+b)+(c+d))+((e+f)+(g+h));
你這個+=雖然省去了一些臨時對象,不過也只好一個一個乖乖地+=了,單個cpu的時候沒有事情,多個cpu的時候,大好時間都浪費了。
re: 我所理解的歸并排序算法(新) Wang Feng 2009-06-09 11:54
void Merge(T a[], int left, int center, int len)
{
T *t = new T[len-left];//存放被合并后的元素
----------------------------------------------------------------
C++支持這樣的語法
T t[len-left];
我猜想效率會比
T *t = new T[len-left];
更高
re: 自動求導模板庫[未登錄] wang feng 2009-05-02 13:50
能否返回一個函數指針?
比如:
typedef double (fp*) ( const vector<double>&, const double);
fp f1;
........
fp f2 = d(f1);
re: C++ 代碼技巧 (續 02)[未登錄] wang feng 2009-04-14 20:30
class A
{
private:
struct Handle;
Handle * handle;
public:
.........
};
這樣的話,連一開始的兩個
class StreamFilter;
class EmitModeConfig;
都不需要了:)
@guest
確實是這樣的,要解決的話也不是很困難,只要partation時候把數組random shuffle一下就可以了:)
re: 對一類因偷換概念而引發的悖論的思考 Wang Feng 2008-12-03 15:19
@zfv
偶高中的時候數學競賽全國得過獎的,不太可能沒學好,更不會丟人。
re: 對一類因偷換概念而引發的悖論的思考 Wang Feng 2008-11-25 12:19
@doyle
拜托,我定量計算之前還有一個定性分析,那個是相當好理解的。
re: 對一類因偷換概念而引發的悖論的思考 Wang Feng 2008-11-25 10:28
@doyle
我定量分析中,使用的是一個數學模型;
我不認為一個有著億萬身家的人,還會像乞丐一樣熱衷于一元錢;
就是說“如果"酷斃"和"帥呆"的貪婪程度完全精確的一樣呢?”這種可能在我的考慮中幾乎是不存在的。
@zuhd
3,5,7,4,0,1
第一次調用partation(arr, 0, 5)是以1為界分的;
結果數組被排成
0 1 7 4 3 5
返回1
@zuhd
如果arr已經給定不可更改,那么這個結果就是唯一的
re: 紅黑樹的實現源碼(第二次修訂版) Wang Feng 2008-11-11 15:42
我暈倒,有沒有優雅一點的,至少短一點的實現?
@天策魂之音
我用隨機數測試過42憶次,沒有出錯過;
不知道錯誤是什么
@zuhd
"程序的運行結果可以有很多種啊"里邊的程序是指 partation還是nth?
@zuhd
不是,這段代碼目的是:以A為界,把數組分為兩個部分,前邊部分數值都是不大于A的,后邊的部分的數值都是不小于A的
@zuhd
你可以這樣理解:
1) 找到一個arbitrary value A(這個數值應該是任意的,我圖寫起來利落,直接指定為數組的最后一個元素----比較好的做法是,執行之前,在數組中任意尋找一個元素與數組最后一個元素交換)
2)一個指針指向數組的第一個元素,往向后掃描,找到第一個大于A的元素,標記為arr[a];
3)另外一個指針指向數組的最后一個元素,向前掃面,找到第一個小于A的元素arr[b];
4)交換arr[a] arr[b]
5)若兩個指針沒有重合,轉到2)
6)將A與兩個指針重合地點的元素交換
這樣以來,以A為界,把數組分為兩個部分,前邊部分數值都是不大于A的,后邊的部分的數值都是不小于A的
re: pku 2253 Frogger Wang Feng 2008-11-06 22:51
acm中如果涉及到圖的算法,可否直接使用boost graph library?
@cdy20
這樣寫的話,用g++編譯后交換失敗;
用icpc編譯后交換成功,非常奇怪
可有人給出更多測試?
我的編譯器:
$ g++ -v
Using built-in specs.
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --prefix=/usr --enable-shared --enable-languages=c,c++,fortran,objc,obj-c++,treelang --enable-threads=posix --mandir=/usr/share/man --infodir=/usr/share/info --enable-__cxa_atexit --disable-multilib --libdir=/usr/lib --libexecdir=/usr/lib --enable-clocale=gnu --disable-libstdcxx-pch --with-tune=generic
Thread model: posix
gcc version 4.3.2 (GCC)
$ icpc -v
Version 10.1
copy( arr, arr+size, ostream_iterator<int>( cout, " " ) );
我覺得這樣寫,看起來容易一點
因為逗號運算符優先級最低,于是arr+size中的加號在逗號之前;
我的習慣是先運行的更緊密一些;
如你比較喜歡的這個
if (a == b) a = a + b;
我會寫成
if (a == b) a = a+b;
或者
if (a == b)
{
a = a+b;
}
或者
if (a == b)
++a;
或者
if (a == b)
{
++a;
}
re: stl奇怪的copy問題[已解決] Wang Feng 2008-11-06 16:39
不要當真,當我是純粹灌水就好了:)
re: stl奇怪的copy問題[已解決] Wang Feng 2008-11-06 15:58
5 #include <iostream>
6 #include <string>
7 #include <vector>
8 #include <sstream>
9 #include <utility>
10 #include <algorithm>
11 #include <list>
12 #include <map>
13 #include <set>
14 #include <queue>
15 #include <stack>
16 #include <bitset>
17 #include <memory>
18 #include <cstdio>
19 #include <cmath>
20 #include <cstdlib>
21 #include <cctype>
22 #include <cstring>
23 #include <climits>
還有比這更可怕的么?19個頭文件…………
re: 【分享】軟件工程規范-軟件工程文檔模板 Wang Feng 2008-10-23 18:51
敢問.dot文檔如何打開?
re: 模版函數指針,C++委托的實現-原創 Wang Feng 2008-10-06 16:20
boost中bind這樣來做:
class button
{
public:
boost::function<void> onClick;
};
class player
{
public:
void play();
void stop();
};
button playButton, stopButton;
player thePlayer;
void connect()
{
playButton.onClick = boost::bind(&player::play, &thePlayer);
stopButton.onClick = boost::bind(&player::stop, &thePlayer);
}
re: 今天找了一個編輯LUA的好東西,你猜猜她是誰? Wang Feng 2008-06-20 12:50
用慣了vim了,對別的編輯器無愛
re: RamDisk好處! Wang Feng 2008-06-16 18:00
我這邊是/dev/shm
re: (C++)一個愚蠢的錯誤 Wang Feng 2008-06-09 18:05
#include <cstring>
#include <string>
re: 5.12汶川大地震(一) Wang Feng 2008-05-13 22:23
blessing
re: 今天去the9.com面試,一些考題,一些想法 Wang Feng 2008-05-09 14:48
貌似我是第一個回帖的,當時看錯的題目,貽笑大方了,今天過來翻翻,居然這么多回應。
看到面試官說:
另外告訴其他各位朋友,那個計算7744這個數字的題目,是要用一支筆一張紙嚴禁的推理出來,用來考查人的分析和推理能力的,不然讓人寫代碼實現的。
于是就推理一番如下:
-------------------------------------------------------------------------
題目:
1000~10000里面的4位平方數你給我找出來,數字的規則是 abcd, a=b c=d
推理:
1. 依照題意,這個數字可以寫為
x = 1100a + 11b = s * s ( 0 < a, b < 10 )
由于1000<= x = s * s <= 10000,故
31 < s <= 100
2. 由于 100a + b = s * s / 11
故s必定能夠被11整除,記
s = 11c ( 2 < c < 10 )
于是得到
11*c * c = 100a + b = y
很顯然y必定為11的倍數
3. 由于一個數,如果能夠被11整除,那么它的奇位數之和與偶位數之和之差必定能為11所整除(貌似是初中學過的一個定理,很容易推導,我猜想這個才是整個推理過程的要點)
很顯然y的“奇位數之和與偶位數之和之差”
sub = a + b 必須能夠被11整除
由于限制條件 ( 0 < a, b < 10 )的存在
a+b =11 必定成立
4. 將它代入到
11*c * c = 100a + b
得到
9a = c*c - 1
于是 a = (c-1)(c+1) / (3*3)
5. 很顯然要讓c-1跟c+1同時都可被3整除是不可能的,所以c+1 c-1這兩個數字中,必定有一個能夠被9整除,另一個是質數
由于 2 < c < 10
故c = 8
因此s = 88
6. x = 88 * 88
re: 今天去the9.com面試,一些考題,一些想法 Wang Feng 2008-05-06 23:22
@zellux
是我看錯了題意,不好意思
re: 今天去the9.com面試,一些考題,一些想法 Wang Feng 2008-05-06 20:26
1000~10000里面的4位平方數你給我找出來,數字的規則是 abcd, a=b c=d
--------------------------------------------------------------------------
可能你記錯了范圍了,這個范圍里邊不存在
int
strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2)
{
if (*s1 == 0)
return 0;
++s1;
++s2;
}
return *(unsigned const char *)s1 - *(unsigned const char *)(s2);
}
re: TopPlayer播放器源碼發布 Wang Feng 2008-05-04 14:45
對了,編譯器
$ gcc -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../configure --prefix=/usr --enable-shared --enable-languages=c,c++,fortran,objc,obj-c++,treelang --enable-threads=posix --mandir=/usr/share/man --enable-__cxa_atexit --disable-multilib --libdir=/usr/lib --libexecdir=/usr/lib --enable-clocale=gnu --disable-libstdcxx-pch --with-tune=generic
Thread model: posix
gcc version 4.3.0 (GCC)
re: TopPlayer播放器源碼發布 Wang Feng 2008-05-04 14:44
第一個文件就編譯不過去
部分輸出:
CLV_ListView.c:2831: error: ‘CIs_ListViewData’ has no member named ‘m_pColumns’
CLV_ListView.c:2834: error: ‘rInvalid’ undeclared (first use in this function)
CLV_ListView.c:2835: error: ‘CIs_ListViewData’ has no member named ‘m_rClient’
CLV_ListView.c:2837: error: ‘CIs_ListViewData’ has no member named ‘m_pColumns’
CLV_ListView.c:2838: error: ‘CIs_ListViewData’ has no member named ‘m_hWnd’
CLV_ListView.c:2838: error: ‘FALSE’ undeclared (first use in this function)
CLV_ListView.c: In function ‘CLV_Invalidate’:
CLV_ListView.c:2849: error: ‘CIs_ListViewData’ has no member named ‘m_hWnd’
CLV_ListView.c:2849: error: ‘FALSE’ undeclared (first use in this function)
CLV_ListView.c: In function ‘RemoveSelectedItem’:
CLV_ListView.c:2859: error: ‘BOOL’ undeclared (first use in this function)
CLV_ListView.c:2859: error: expected ‘;’ before ‘bMoved’
CLV_ListView.c:2864: error: ‘struct <anonymous>’ has no member named ‘m_hPlaylistViewControl’
CLV_ListView.c:2865: error: ‘struct <anonymous>’ has no member named ‘m_hPlaylistViewControl’
CLV_ListView.c:2868: error: ‘BROWSEINFO’ undeclared (first use in this function)
CLV_ListView.c:2868: error: expected ‘;’ before ‘browseinfo’
CLV_ListView.c:2869: error: ‘LPITEMIDLIST’ undeclared (first use in this function)
CLV_ListView.c:2869: error: expected ‘;’ before ‘itemlist’
CLV_ListView.c:2873: error: ‘browseinfo’ undeclared (first use in this function)
CLV_ListView.c:2873: error: ‘HWND’ undeclared (first use in this function)
CLV_ListView.c:2873: error: expected ‘;’ before ‘G_MTabCtrl’
CLV_ListView.c:2877: error: ‘BIF_EDITBOX’ undeclared (first use in this function)
CLV_ListView.c:2882: error: ‘itemlist’ undeclared (first use in this function)
CLV_ListView.c:2882: warning: implicit declaration of function ‘SHBrowseForFolder’
CLV_ListView.c:2885: warning: implicit declaration of function ‘SHGetPathFromIDList’
CLV_ListView.c:2885: error: ‘struct <anonymous>’ has no member named ‘main_text_last_browsed_dir’
CLV_ListView.c:2871: warning: unused variable ‘directorychoice’
CLV_ListView.c:2890: error: ‘struct <anonymous>’ has no member named ‘m_hPlaylistViewControl’
CLV_ListView.c:2893: warning: implicit declaration of function ‘lstrcpyn’
CLV_ListView.c:2901: warning: implicit declaration of function ‘lstrcpy’
CLV_ListView.c:2901: error: ‘struct <anonymous>’ has no member named ‘main_text_last_browsed_dir’
CLV_ListView.c:2902: warning: implicit declaration of function ‘lstrcat’
CLV_ListView.c:2903: error: ‘bMoved’ undeclared (first use in this function)
CLV_ListView.c:2903: warning: implicit declaration of function ‘MoveFile’
CLV_ListView.c:2906: warning: implicit declaration of function ‘CPLI_SetPath’
CLV_ListView.c:2912: error: ‘struct <anonymous>’ has no member named ‘m_hPlaylistViewControl’
CLV_ListView.c:2914: error: ‘struct <anonymous>’ has no member named ‘m_hPlaylistViewControl’
CLV_ListView.c:2858: warning: unused variable ‘cNewPath’
CLV_ListView.c:2857: warning: unused variable ‘cPath’
CLV_ListView.c: At top level:
CLV_ListView.c:2916: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘CPlaylistWindow_CB_onAppMessage’
CLV_ListView.c:2958: error: expected ‘)’ before ‘hWnd’
CLV_ListView.c:3018: error: expected ‘)’ before ‘hWnd’
make: *** [CLV_ListView.o] Error 1
re: 模板參數名命名慣例 Wang Feng 2008-04-21 22:12
說起宏定義,其實在文件前面定義,用完了之后在文件最后取消是個很好的習慣
比如
#define PI 3.1415926535897932384626433
.....
#undef PI
re: 編程之美:讓CPU占用率曲線聽你指揮 Wang Feng 2008-04-18 17:15
unix上怎么辦?