在下列用法方面,请用C中的执行时间进行评分.在一些访谈中,我被问到我应该在这些变化中使用哪些以及为什么.
a++
++a
a=a+1
a+=1
Run Code Online (Sandbox Code Playgroud) 我有以下场景.实时是应用程序的实现.
1)我需要在容器中存储最多20个条目(STL Map,STL List等).2)如果有新条目并且已经存在20个条目,则必须使用新条目覆盖最旧的条目.
考虑到第2点,我觉得如果容器已满(最多20个条目)'list'是最好的选择,因为我总是可以删除列表中的第一个条目并最后添加新条目(push_back).但是,搜索效率不高.
对于只有20个条目,如果我使用列表代替地图,它在搜索效率方面是否真的有很大差异?
另外考虑在地图中插入的费用我觉得我应该去列表?
你能告诉我什么是更好的选择吗?
一种时间有效的程序,用于在二维矩阵中查找元素,其行和列单调增加.(行和列从上到下,从左到右).
如果2D数组已经排序,我只能想到二分搜索.
请参阅下面的代码示例.return (&i)函数中的语句fun_ret_loc_ptr()返回一个警告:"函数返回局部变量的地址".另一方面return a,函数fun_ret_loc_var()中的语句不会这样做.
#include <stdio.h>
int* fun_ret_loc_ptr()
{
int i = 10;
return (&i);
}
int fun_ret_loc_var()
{
int a = 20;
return a;
}
int main()
{
printf("val frm local ptr = %d\n", *fun_ret_loc_ptr());
printf("val frm local var = %d\n", fun_ret_loc_var());
}
Run Code Online (Sandbox Code Playgroud)
据我所知,在第一个函数中,返回的地址(return (&i);)被引用到一个内存位置,该位置是与函数对应的堆栈帧的一部分fun_ret_loc_ptr().一旦此函数返回,堆栈帧(激活记录)将被销毁.同样的事情应该适用return a;于函数中的变量'a'()fun_ret_loc_var().即使它被返回,当它在main中使用时,对应于'a'的内存将会死亡.
从" return"声明的功能角度来看,为什么会出现这种差异?
我知道签名值的2s补码表示.但二进制'10000000'如何成为-128十进制(使用%d).
for +64 binary rep ='01000000'for -64 binary rep ='11000000',这是'01000000'的2的补码
有人可以解释一下吗?
程序:
int main()
{
char ch = 1;
int count = 0;
while(count != 8)
{
printf("Before shift val of ch = %d,count=%d\n",ch,count);
ch = ch << 1;
printf("After shift val of ch = %d,count=%d\n",ch,count);
//printBinPattern(ch);
printf("*************************************\n");
count++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Before shift val of ch = 1, count=0
After shift val of ch = 2, count=0
*************************************
...
... /* Output not shown */
Before shift …Run Code Online (Sandbox Code Playgroud)