目录
一、auto
二、范围for
三、lambda匿名函数
四、智能指针
一、auto
在早期C/C++中auto的含义是:使用auto修饰的变量,是具有自动存储器的局部变量
C++11中,标准委员会赋予了auto全新的含义即:auto不再是一个存储类型指示符,而是作为一个新的类型指示符来指示编译器,auto声明的变量必须由编译器在编译时期推导而得。
现在已经被弃用
注意:
auto不能出现在函数参数中
auto不能作为类中的非静态成员
auto不能定义数组
auto不能作用模板参数
#include#include using namespace std; int main() { auto a = "abcd"; cout << a << endl; list
lst; lst.push_back(1); lst.push_back(2); lst.push_back(3); lst.push_back(4); auto ite = lst.begin(); while (ite != lst.end()) { cout << *ite << endl; ite++; } system("pause"); return 0; }
二、范围for
C++ 11提供了一个特殊版本的 for 循环,在很多情况下,它都可以简化数组的处理,这就是基于范围的 for 循环。在使用基于范围的 for 循环处理数组时,该循环可以自动为数组中的每个元素迭代一次。
例如,如果对一个 8 元素的数组使用基于范围的 for 循环,则该循环将迭代 8 次。因为基于范围的 for 循环可以自动知道数组中元素的个数,所以不必使用计数器变量控制其迭代,也不必担心数组下标越界的问题。
基于范围的 for 循环使用了一个称为范围变量的内置变量。每次基于范围的 for 循环迭代时,它都会复制下一个数组元素到范围变量。例如,第一次循环迭代,范围变量将包含元素 0 的值;第二次循环迭代,范围变量将包含元素 1 的值,以此类推。
#includeusing namespace std; int main() { int arr[10] = { 0,1,2,3,4,5,6,7,8,9 }; for (int i = 0; i < 10; i++) { cout << arr[i] << " "; } cout << endl; for (int val : arr) { cout << val << " "; } return 0; }
三、lambda匿名函数
匿名函数(英文名:lambda)就是没有名字的函数。 最简单的匿名函数是 [] () {} ,它没有参数也没有返回值。 在匿名函数中, [] 里面用来 捕获函数外部的变量 ,而 () 里面就是匿名函数的 参数 , {} 里面就是函数的 执行代码 。
#includeusing namespace std; //[]()mutable noexcet / throw()-> 返回值类型 {函数体} int main() { int a = 10; int b = 20; auto fun = [=]()mutable->int {a = 100; return a + b; }; cout << fun() << endl; cout << a << endl; return 0; }
#includeusing namespace std; #include #include int main() { vector vec = { 1,2,3,4,5 }; ::for_each(vec.begin(), vec.end(), [](int val){cout << val << " "; }); return 0; }
四、智能指针
C++11 引入了 3 个智能指针类型:
std::unique_ptr
std::shared_ptr
std::weak_ptr
std::auto_ptr 已被废弃。
智能指针,本质上是对资源所有权和生命周期管理的抽象:
- 当资源是被独占时,使用 std::unique_ptr 对资源进行管理。
- 当资源会被共享时,使用 std::shared_ptr 对资源进行管理。
- 使用 std::weak_ptr 作为 std::shared_ptr 管理对象的观察者。
- 通过继承 std::enable_shared_from_this 来获取 this 的 std::shared_ptr 对象。
#includeusing namespace std; #include int main() { //int* p = new int(100); //cout << *p << endl; //shared_ptr sp2(p); //{ // //shared_ptr sp2(p); // shared_ptr sp1 = sp2; // cout << *sp1 << endl; // cout << *sp2 << endl; //} //cout << *p << endl; //unique_ptr up1(new int(100)); //cout << *up1 << endl; //int* p = up1.get(); up1.release(); //up1.reset(); //cout << *p << endl; //cout << *up1 << endl; shared_ptr sp1(new int(100)); shared_ptr sp2 = sp1; weak_ptr wp = sp1; cout << wp.use_count() << endl; *wp.lock() = 200; cout << *sp1 << endl; return 0; }
实例:
#includeusing namespace std; template class SharedPtr { private: int* nCount; T* p; public: SharedPtr(); SharedPtr(T* p); SharedPtr(SharedPtr& rSp); ~SharedPtr(); public: SharedPtr & operator=(SharedPtr& rSp); T operator*(); void reset(T* p = nullptr); T* get(); int use_count(); }; template SharedPtr ::SharedPtr() { nCount=NULL; p=NULL; } template SharedPtr ::SharedPtr(T* p) { this->p= p; nCount = new int(1); } template SharedPtr ::SharedPtr(SharedPtr& rSp) { this->p = rSp.p; this->nCount = rSp.nCount; (*nCount)++; } template SharedPtr ::~SharedPtr() { (*nCount)--; if (*nCount == 0) { delete p; p = NULL; delete nCount; nCount = NULL; } } template SharedPtr & SharedPtr ::operator=(SharedPtr & rSp) { this->p = rSp.p; this->nCount = rSp.nCount; (*nCount)++; return *this; } template T SharedPtr ::operator*() { return *(this->p); } template T* SharedPtr ::get() { return this->p; } template int SharedPtr ::use_count() { if ((*nCount) == NULL) { return 0; } return *nCount; } template void SharedPtr :: reset(T* p) { (*nCount)--; if (*nCount == 0) { delete this->p; delete this->nCount; } this->p = NULL; this->nCount = NULL; if (p != NULL) { this->p = p; this->nCount =new int (1); } } int main() { int* p1 = new int(100); int* p2 = new int(300); SharedPtr sp1(p1); SharedPtr sp2 = sp1; SharedPtr sp3(new int(200)); sp3 = sp1; cout << *sp1.get() << endl; cout << sp1.use_count()<



