1.sizeof("hello") = 6; 放入常量区,编译器不用频繁读取内存
2.sizeof(类型),只关心类型而不关心内容:int i = 0; sizeof(1/i) = 4; 编译运行正常
3.只有有类型的指针才能通过指针值确定指针指向的内容
4.数组的引用 int (&p)[10] = int a[10]; 不使用
5.void *p; 只知道指针地址不知道指针类型,无法确定变量,可以强制转为相应类型
6.等待一秒的函数实现:
#include <ctime>
void WaitAMinite()
{
time_t t = time(0);
while (time(0) == t);
}
7.cout输出有缓冲不是立即输出,cerr立即输出或者,cout<<"hello"<<flush,cout<<"hello"<<endl;
8.setw(),设置输出宽度;right靠右;setfill('0'),设置宽度填充字符:
#include <iomanip>
cout<<setw(2)<<right<<setfill('0')<<2<<endl;
9.vi中替换使用::% s/旧串/新串/g,替换全文;:%^, $ s/old/new/g,替换从行首到行尾;
10.pwd,当前工作目录
11.倒计时程序:
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
class Clock
{
public:
Clock(int h = 0, int m = 0, int s = 0):h(h), m(m), s(s){}
void tick()
{
time_t t = time(0);
while (time(0) == t);
if (--s < 0)
{
s = 59;
if (--m < 0)
{
m = 59;
--h;
}
}
}
void show()
{
cerr<<'\r'
<<setw(2)<<right<<setfill('0')<<h<<":"
<<setw(2)<<right<<setfill('0')<<m<<":"
<<setw(2)<<right<<setfill('0')<<s;
}
void run()
{
while(m | h | s)
{
tick();
show();
}
cout<<"\ngame over!!!"<<endl;
}
private:
int h;
int m;
int s;
};
int main()
{
Clock c(0, 1, 0);
c.run();
system("pause");
return 0;
}
12.cout<<"abc\rd"<<endl;显示dbc;
13.A a();定义类对象时,如果不传参不要加括号,会认为是一个函数声明
14.常量只能初始化不能赋值,类中常量不能在构造函数体中赋值,要在初始化列表中初始化
class A
{
const int i;
public:
A(int i = 0):i(i){}
};
15.初始化列表不能初始化数组和结构变量
16.void f(int i = 0); void f();编译错误
17.类对象调用构造的顺序为:先全局变量,之后按顺序;注意static只是整个程序运行期间存在调用构造不优先
18.类中先执行成员构造函数,再执行本身构造函数
19.由于全局变量的构造在main函数之前,所以可以在main函数之前做一些操作
20.腾讯面试加分题:
给定一数组a[N],我们希望构造数组b [N],其中b[j]=a[0]*a[1]…a[N-1] / a[j],在构造过程中,不允许使用除法:
要求O(1)空间复杂度和O(n)的时间复杂度;
除遍历计数器与a[N] b[N]外,不可使用新的变量(包括栈临时变量、堆空间和全局静态变量等);
实现程序(主流编程语言任选)实现并简单描述。
#include <iostream>
using namespace std;
void fun(int a[], int b[], int len)
{
b[0] = 1;
for (int i = 1; i < len; ++i)
{
b[i] = b[i-1] * a[i-1];
}
for (int i = len - 1; i > 0; --i)
{
b[i] *= b[0];
b[0] *= a[i];
}
}
void show(int b[], int len)
{
for (int i = 0; i < len; ++i)
{
cout<<b[i]<<'\t';
}
cout<<endl;
}
int main()
{
int a[] = {1,2, 4, 5, 8};
int b[sizeof(a)/sizeof(a[0])] = {};
fun(a, b, sizeof(a)/sizeof(a[0]));
show(a, sizeof(a)/sizeof(a[0]));
show(b, sizeof(a)/sizeof(a[0]));
system("pause");
return 0;
}