posts - 12,  comments - 54,  trackbacks - 0
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的时候,大好时间都浪费了。
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一下就可以了:)
@zfv
偶高中的时候数学竞赛全国得过奖的,不太可能没学好,更不会丢人。
@doyle
拜托,我定量计算之前还有一个定性分析,那个是相当好理解的。
@doyle
我定量分析中,使用的是一个数学模型;
我不认为一个有着亿万身家的人,还会像乞丐一样热衷于一元钱;
就是说“如果"酷毙"和"帅呆"的贪婪程度完全精确的一样呢?”这种可能在我的考虑中几乎是不存在的。
@zuhd
3,5,7,4,0,1
第一次调用partation(arr, 0, 5)是以1为界分的;
结果数组被排成
0 1 7 4 3 5
返回1
@zuhd
如果arr已经给定不可更改,那么这个结果就是唯一的
我晕倒,有没有优雅一点的,至少短一点的实现?
@天策魂之音
我用随机数测试过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
@空明流转
@fish_autumn
请求指点
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个头文件…………
敢问.dot文档如何打开?
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);
}
用惯了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
貌似我是第一个回帖的,当时看错的题目,贻笑大方了,今天过来翻翻,居然这么多回应。
看到面试官说:
另外告诉其他各位朋友,那个计算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
@zellux
是我看错了题意,不好意思
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
unix上怎么办?

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(4)

随笔分类

随笔档案

Link List

搜索

  •  

最新评论

阅读排行榜

评论排行榜