360面试准备

1)lambda表达式 for_each (array, array + SIZE, [] (int a){ cout << a << ” “; }); lambda 表达式可以方便地构造匿名函数,如果你的代码里面存在大量的小函数,而这些函数一般只被调用一次,那么不妨将他们重构成 lambda 表达式 作用:使得代码更加简洁

2)自动类型推导和 decltype auto x=0, 0是int类型,故x也是int类型 auto ci = vi.begin();

decltype用于从对象或表达式中俘获类型,如: const vector vi; typedef decltype (vi.begin()) CIT; CIT another_const_iterator;

3)统一的初始化语法 class C { int a; int b; public: C(int i, int j); }; C c {0,0}; //C++11 only. 相当于 C c(0,0); int* a = new int[3] { 1, 2, 0 }; /C++11 only class X { int a[4]; public: X() : a{1,2,3,4} {} //C++11, 初始化数组成员 };

4)还有一大好事就是对于容器来说,终于可以摆脱 push_back() 调用了,C++11中可以直观地初始化容器了: // C++11 container initializer vector vs={ “first”, “second”, “third”}; map singers = { {“Lady Gaga”, “+1 (212) 555-7890”}, {“Beyonce Knowles”, “+1 (212) 555-0987”}};

5)C++11 标准的两个新特性:defaulted 和 deleted 函数。 对于 defaulted 函数,编译器会为其自动生成默认的函数定义体,从而获得更高的代码执行效率, 也可免除程序员手动定义该函数的工作量。对于 deleted 函数, 编译器会对其禁用, 从而避免某些非法的函数调用或者类型转换,从而提高代码的安全性。

struct A { A()=default; //C++11 virtual ~A()=default; //C++11 };

struct NoCopy { NoCopy & operator =( const NoCopy & ) = delete; NoCopy ( const NoCopy & ) = delete; }; NoCopy a; NoCopy b(a); //编译错误,拷贝构造函数是 deleted 函数

6)nullptr类型 nullptr 是一个新的 C++ 关键字,它是空指针常量,它是用来替代高风险的 NULL 宏和 0 字面量的。nullptr 是强类型的

void f(int); //#1 void f(char *);//#2 //C++03 f(0); //调用的是哪个 f? //C++11 f(nullptr) //毫无疑问,调用的是 #2

所有跟指针有关的地方都可以用 nullptr,包括函数指针和成员指针

const char *pc=str.c_str(); //data pointers if (pc!=nullptr) cout<

例如:std::unique_ptr<int> p1(new int(5));std::unique_ptr<int> p2 = p1; // 编译会出错std::unique_ptr<int> p3 = std::move(p1); // 转移所有权, 现在那块内存归p3所有, p1成为无效的指针.

C++11或boost的shared_ptr,基于引用计数的智能指针。可随意赋值,直到内存的引用计数为0的时候这个内存会被释放。

C++11或boost的weak_ptr,弱引用。 引用计数有一个问题就是互相引用形成环,这样两个指针指向的内存都无法释放。 需要手动打破循环引用或使用weak_ptr。顾名思义,weak_ptr是一个弱引用,只引用,不计数。 如果一块内存被shared_ptr和weak_ptr同时引用,当所有shared_ptr析构了之后,不管还有没有weak_ptr引用该内存, 内存也会被释放。所以weak_ptr不保证它指向的内存一定是有效的,在使用之前需要检查weak_ptr是否为空指针。

3),智能指针的简单实现。

template <class T> class myAutoPtr{private:T *myPtr;public:explicit myAutoPtr( T *p = 0 ){myPtr = p;}myAutoPtr &operator=( myAutoPtr &a ){if( this == &a ){return *this;}delete myPtr;myPtr = a.relase();return *this;}~myAutoPtr(){delete myPtr;}T& operator*(){return *myPtr;}T* operator->(){return myPtr;}T *get(){return myPtr;}T *relase(){T *tmp = myPtr;myPtr = NULL;return tmp;}void reset( T *p ){if( p != myPtr ){delete myPtr;myPtr = p;}return;}};

5.非递归求树的高度,链表逆序

typedef struct Node{int data;struct Node *left;struct Node *right;struct Node(){data = 0;left = right = NULL;}}Node, *pNode;int treeDepth( pNode root ){int depth = 0;if( NULL == root ){return depth;}queue<pNode> qu;qu.push(root);while( !qu.empty() ){int curSize = qu.size();depth++;for( int i = 0; i < curSize; i++ ){pNode cur = qu.front();qu.pop();if( cur->left != NULL ){qu.push( cur->left );}if( cur->right != NULL ){qu.push( cur->right );}}}return depth;}// 逆序一个链表Node *reverseLink( Node *head ){if( NULL == head ){return head;}Node *cur = NULL;while( head != NULL ){Node *pNext = head->next;head->next = cur;cur = head;head = pNext;}return cur;}我想一个人旅行,可以不带相机,也不要带上手机,

360面试准备

相关文章:

你感兴趣的文章:

标签云: