Tag: C#的

最快的固定长度6 int数组

回答另一个堆栈溢出问题( 这一个 )我偶然发现了一个有趣的子问题。 对6个整数的数组进行sorting的最快方法是什么? 由于问题是非常低的水平: 我们不能假定图书馆是可用的(而且这个调用本身也有其成本),只有普通的C 为避免清空指令stream水线(成本非常高),我们应该尽量减less分支,跳转以及其他任何types的控制stream程中断(例如隐藏在&&或||中序列点后面的stream程)。 空间受到限制,最大限度地减less寄存器和内存使用是一个问题,理想情况下,sorting可能是最好的。 真的这个问题是一种高尔夫,目的不是要尽量减less来源长度,而是执行时间。 我将其称为“Zening”代码,用于Michael Abrash所着的“代码优化之禅”及其续篇的书名。 至于为什么它很有趣,有几层: 这个例子很简单,容易理解和测量,涉及的C技能不多 它显示了对于这个问题select一个好的algorithm的效果,还显示了编译器和底层硬件的效果。 这是我的参考(天真,没有优化)的实现和我的testing集。 #include <stdio.h> static __inline__ int sort6(int * d){ char j, i, imin; int tmp; for (j = 0 ; j < 5 ; j++){ imin = j; for (i = j + 1; i < 6 ; i++){ if (d[i] […]

如何从其他视图模型调用主视图模型中的函数?

我的程序由一个TreeView和两个位于地面的contentPresenters组成。 mainWindow, TreeView和每个contentPresenter都有自己的viewModels。 我想从TreeViewViewModel的mainWindowViewModel调用一个函数。 我需要这样做,因为mainWindowViewModel控制contentPresenters显示的内容,我想手动更新显示。 我猜我会做这样的事情… TreeViewViewModel : public class TreeViewViewModel { //Do I need to declare the MainWindowVM? public TreeViewViewModel() { … } private void function() { //Command that affects display //Manually call function in MainWindowVM to refresh View } } 我试图从TreeViewViewModel访问MainWindowVM ,使用: public MainWindowViewModel ViewModel { get { return DataContext as MainWindowViewModel; } […]

一个谜语(C)

一位朋友给了我一个谜语: #include<stdio.h> #define TOTAL_ELEMENTS ((sizeof(array) / sizeof(array[0]))) int array[] = {23,34,12,17,204,99,16}; int main() { int d; for(d=-1;d <= (TOTAL_ELEMENTS-2);d++) printf("%d\n",array[d+1]); getchar(); return 0; } 上面的代码应该是打印所有的数组元素,代码中的问题是什么(输出是什么)? 我认为循环不会迭代一次? 我发现下面的代码可以工作: #include<stdio.h> #define TOTAL_ELEMENTS ((sizeof(array) / sizeof(array[0]))) int array[] = {23,34,12,17,204,99,16}; int main() { int d; int x = (TOTAL_ELEMENTS-2); for(d=-1;d <= x;d++) printf("%d\n",array[d+1]); getchar(); return 0; } 我有一个理论,这是macros观的事情,但我不能把我的手指在这个问题上。

我无法刷新标准input

如何刷新stdin ? 为什么它不能在下面的代码片段中工作? #include <string.h> #include <stdio.h> #include <malloc.h> #include <fcntl.h> int main() { int i=0,j=0, sat; char arg[256]; char * argq; argq = malloc(sizeof(char)*10); printf("Input the line\n"); i=read(0, arg, sizeof(char)*9); arg[i-1]='\0'; fflush(stdin); i=read(0, argq, sizeof(char)*5); argq[i-1]='\0'; puts(arg); puts(argq); return 0; } 现在,如果我把input作为11个字符,只能读取9,但标准input中剩余的两个字符不会刷新,并在argq中再次读取。 为什么? input:123 456 789 产量:123 456 89 为什么我得到这个89作为输出?

C扩展名:<? 和>? 运营商

我观察到有一点<? 和>? GCC的运营商。 我怎样才能在GCC 4.5下使用这些? 他们被删除了,如果是的话,什么时候? Offset block_count = (cpfs->geo.block_size – block_offset) <? count; cpfs.c:473: error: expected expression before '?' token

带有Nullable <value>types的条件运算符赋值?

EmployeeNumber = string.IsNullOrEmpty(employeeNumberTextBox.Text) ? null : Convert.ToInt32(employeeNumberTextBox.Text), 我经常发现自己希望做这样的事情( EmployeeNumber是一个Nullable<int>因为它是列允许NULL值的LINQ到SQL的dbml对象的属性)。 不幸的是,编译器认为“虽然这两种types在赋值操作中都是有效的,但它们之间没有”null“和”int“之间的隐式转换。 由于需要在.Textstring上进行内联转换(如果它不为null),我可以看到空合并运算符不是一个选项。 据我所知,唯一的方法是使用if语句和/或分两步进行分配。 在这种特殊情况下,我觉得非常令人沮丧,因为我想使用对象初始值设置语法,而这个赋值就在初始化块中。 任何人都知道更优雅的解决scheme

主函数可以在C ++中调用自己吗?

有人可以告诉下面的代码有什么问题吗? int main () { return main(); } 我testing过,它编译正确。 它永远在运行。 幕后的诡计呢?

在函数C中分配内存2d数组

如何为函数中的2d数组分配dynamic内存? 我试过这种方式: int main() { int m=4,n=3; int** arr; allocate_mem(&arr,n,m); } void allocate_mem(int*** arr,int n, int m) { *arr=(int**)malloc(n*sizeof(int*)); for(int i=0;i<n;i++) *arr[i]=(int*)malloc(m*sizeof(int)); } 但它不起作用。 有人可以纠正我的错误? 更新:更正的function(快速回答) void allocate_mem(int*** arr, int n, int m) { *arr = (int**)malloc(n*sizeof(int*)); for(int i=0; i<n; i++) (*arr)[i] = (int*)malloc(m*sizeof(int)); }

如何在C ++循环中生成不同的随机数字?

是否有可能产生不同的随机数,每次循环运行。 例如,我有: for (int t=0;t<10;t++) { int random_x; srand ( time(NULL) ); random_x = rand() % 100; cout<<"\nRandom X = "<<random_x; } 但问题是,它每次都会产生相同的随机数。 每次循环运行时是否可以生成不同的随机数字? 有没有可能重置随机数初始化?

如何用cout打印函数指针?

我想用cout打印出一个函数指针,发现它不起作用。 但是,在将函数指针转换为(void *)之后,它起作用,printf与%p也是如此,例如 #include <iostream> using namespace std; int foo() {return 0;} int main() { int (*pf)(); pf = foo; cout << "cout << pf is " << pf << endl; cout << "cout << (void *)pf is " << (void *)pf << endl; printf("printf(\"%%p\", pf) is %p\n", pf); return 0; } 我用g ++编译,得到这样的结果: cout […]