c++小知识点清单

*.h和*.hpp的区别?

答:.hpp=.h+.cpp,hpp它相当与把函数变量的申明和实现都放到一个文件了。而.h只是申明函数和变量。

NULLnullptr的区别?

nullptr是c++11才有的[1]。在以前NULL空指针就是0.但是在c++11后空指针是nullptr。如果要把原先的代码切换到c++11这种风格也很简单,直接使用宏定义覆盖NULL的取值即可 。建议都用nullptr表示空指针,因为c++11后有些代码空指针是0,有些代码空指针是nullptr所以都用nullptr就没问题。

#define NULL nullptr
//////////////////////
int* x = nullptr;
int* y = NULL;
int* z = 0;

程序运行时间计时

#include <iostream>
#include <chrono>//程序运行计时
using namespace std;
int main(int argc, char** argv){

    //使用std::chrono对程序进行计时
    chrono::steady_clock::time_point start = chrono::steady_clock::now();
/////////////你要计时的那部分代码///////////////////////
    chrono::steady_clock::time_point end = chrono::steady_clock::now();
    chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<double> >(end-start);
    cout<<"用时:"<< time_used.count()<<"秒"<<endl;
    return 0;
}

a[i]与头指针之间的联系

我们知道头指针是数组的第一个元素a[0]的地址,这个地址值存储在变量a中。那么a[i]与a有什么联系呢?答:a[i]==*(a+i)。即a[i]的地址等于a+i。

参考文献:
[1] https://embeddedartistry.com/blog/2017/2/28/migrating-from-c-to-c-null-vs-nullptr

posted @ 2019-06-29 12:02  varyshare|李韬  阅读(140)  评论(0编辑  收藏  举报