目录
一、auto
在早期C/C++中auto的含义是:使用auto修饰的变量,是具有自动存储器的局部变量
C++11中,标准委员会赋予了auto全新的含义即:auto不再是一个存储类型指示符,而是作为一个新的类型指示符来指示编译器,auto声明的变量必须由编译器在编译时期推导而得。
现在已经被弃用
注意:
auto不能出现在函数参数中
auto不能作为类中的非静态成员
auto不能定义数组
auto不能作用模板参数
#include <iostream>
#include <list>
using namespace std;
int main()
{
auto a = "abcd";
cout << a << endl;
list<int> 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 的值,以此类推。
#include <iostream>
using 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)就是没有名字的函数。 最简单的匿名函数是 [] () {} ,它没有参数也没有返回值。 在匿名函数中, [] 里面用来 捕获函数外部的变量 ,而 () 里面就是匿名函数的 参数 , {} 里面就是函数的 执行代码 。
#include <iostream>
using 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;
}
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
int main()
{
vector<int> vec = { 1,2,3,4,5 };
::for_each(vec.begin(), vec.end(), [](int val){cout << val << " "; });
return 0;
}
四、智能指针
C++11 引入了 3 个智能指针类型:
std::unique_ptr<T> :独占资源所有权的指针。
std::shared_ptr<T> :共享资源所有权的指针。
std::weak_ptr<T> :共享资源的观察者,需要和 std::shared_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 对象。
#include <iostream>
using namespace std;
#include <memory>
int main()
{
/*shared_ptr<int> p1(new int(100));
cout << *p1 << endl;
*p1 = 200;
cout << *p1 << endl;*/
//int* p = new int(100);
//cout << *p << endl;
//shared_ptr<int> sp2(p);
//{
// //shared_ptr<int> sp2(p);
// shared_ptr<int> sp1 = sp2;
// cout << *sp1 << endl;
// cout << *sp2 << endl;
//}
//cout << *p << endl;
/*shared_ptr<int> sp1(new int(100));
shared_ptr<int> sp2 = sp1;
cout << *sp1 << endl;
cout << *sp2 << endl;
sp2.reset(new int(200));
cout << *sp2 << endl;*/
//unique_ptr<int> up1(new int(100));
//cout << *up1 << endl;
//int* p = up1.get();
up1.release();
//up1.reset();
//cout << *p << endl;
//cout << *up1 << endl;
shared_ptr<int> sp1(new int(100));
shared_ptr<int> sp2 = sp1;
weak_ptr<int> wp = sp1;
cout << wp.use_count() << endl;
*wp.lock() = 200;
cout << *sp1 << endl;
return 0;
}
实例:
#include<iostream>
using namespace std;
template <typename T>
class SharedPtr
{
private:
int* nCount;
T* p;
public:
SharedPtr();
SharedPtr(T* p);
SharedPtr(SharedPtr& rSp);
~SharedPtr();
public:
SharedPtr<T>& operator=(SharedPtr& rSp);
T operator*();
void reset(T* p = nullptr);
T* get();
int use_count();
};
template <typename T>
SharedPtr<T>::SharedPtr()
{
nCount=NULL;
p=NULL;
}
template <typename T>
SharedPtr<T>::SharedPtr(T* p)
{
this->p= p;
nCount = new int(1);
}
template <typename T>
SharedPtr<T>::SharedPtr(SharedPtr& rSp)
{
this->p = rSp.p;
this->nCount = rSp.nCount;
(*nCount)++;
}
template <typename T>
SharedPtr<T>::~SharedPtr()
{
(*nCount)--;
if (*nCount == 0)
{
delete p;
p = NULL;
delete nCount;
nCount = NULL;
}
}
template <typename T>
SharedPtr<T>& SharedPtr<T>::operator=(SharedPtr<T>& rSp)
{
this->p = rSp.p;
this->nCount = rSp.nCount;
(*nCount)++;
return *this;
}
template <typename T>
T SharedPtr<T>::operator*()
{
return *(this->p);
}
template <typename T>
T* SharedPtr<T>::get()
{
return this->p;
}
template <typename T>
int SharedPtr<T>::use_count()
{
if ((*nCount) == NULL)
{
return 0;
}
return *nCount;
}
template <typename T>
void SharedPtr<T>:: 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<int> sp1(p1);
SharedPtr<int> sp2 = sp1;
SharedPtr<int> sp3(new int(200));
sp3 = sp1;
cout << *sp1.get() << endl;
cout << sp1.use_count()<<endl;
sp3.reset(p2);
//sp3.reset(sp1.get());
//sp3用这个方式reset(sp1.get()) sp1和sp3虽然指向同一个空间 但是他们的nCount不相同 是分别独立的
//会重复释放两次同一个空间 还是不完善的
return 0;
}