C++ 11常用特性

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/glw0223/article/details/101935160

感谢原作者:https://blog.csdn.net/weixin_38416696/article/details/90205128


auto关键字及用法

A、auto关键字能做什么?

  auto并没有让C++成为弱类型语言,也没有弱化变量什么,只是使用auto的时候,编译器根据上下文情况,确定auto变量的真正类型。


  
  
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <list>
  4. #include <iostream>
  5. using namespace std;
  6. auto addTest(int a, int b) {
  7. return a + b;
  8. }
  9. int main()
  10. {
  11. auto index = 10;
  12. auto str = "str";
  13. auto ret = addTest( 1, 2);
  14. cout << index << str << ret << endl;
  15. system( "pause");
  16. return 0;

B、auto不能做什么?

  auto作为函数返回值时,只能用于定义函数,不能用于声明函数


  
  
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <list>
  4. #include <iostream>
  5. using namespace std;
  6. class Test {
  7. public:
  8. auto add(int a, int b);
  9. // {
  10. // return a + b;
  11. // }
  12. };
  13. int main()
  14. {
  15. Test a;
  16. a.add( 3, 4);
  17. system( "pause");
  18. return 0;
  19. }

但如果把实现写在头文件中,可以编译通过,因为编译器可以根据函数实现的返回值确定auto的真实类型。


  
  
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <list>
  4. #include <iostream>
  5. using namespace std;
  6. class Test {
  7. public:
  8. auto add(int a, int b)
  9. {
  10. return a + b;
  11. }
  12. };
  13. int main()
  14. {
  15. Test a;
  16. a.add( 3, 4);
  17. system( "pause");
  18. return 0;
  19. }

nullptr关键字及用法

nullptr是为了解决原来C++中NULL的二义性问题而引进的一种新的类型,由于NULL实际上代表的是0,


  
  
  1. void F(int a){
  2. cout<<a<< endl;
  3. }
  4. void F(int *p){
  5. assert(p != NULL);
  6. cout<< p << endl;
  7. }
  8. int main(){
  9. int *p = nullptr;
  10. int *q = NULL;
  11. bool equal = ( p == q ); // equal的值为true,说明p和q都是空指针
  12. int a = nullptr; // 编译失败,nullptr不能转型为int
  13. F( 0); // 在C++98中编译失败。有二义性。在C++11中调用F(int)
  14. F( nullptr);
  15. return 0;
  16. }

for循环语法


  
  
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <list>
  4. #include <iostream>
  5. #include <map>
  6. using namespace std;
  7. int main()
  8. {
  9. int numbers[] = { 1, 2, 3, 4, 5, 6};
  10. for ( auto number : numbers)
  11. cout << number << endl;
  12. map< string, int>m{ { "a", 1},{ "b", 2},{ "c", 3} };
  13. for ( auto p : m)
  14. cout << p.first << ":" << p.second << endl;
  15. system( "pause");
  16. return 0;
  17. }

long long 类型

扩展精度浮点数,10位有效数字

列表初始化

花括号初始化器列表

int static_arr[5] = { 1, 2, 3, 4 };
int static_arr2[]{ 1, 2, 3, 4 }; // 等号要以省略
int* dynamic_arr = new int[5]{ 1, 2, 3, 4 };
vector<int> stl_vec{ 1, 2, 3, 4 };
set<int> stl_set{ 1, 2, 3, 3 };

constexpr 变量

将变量声明为constexpr类型以便由编译器来验证变量的值是否是一个常量表达式;

声明为constexpr的变量一定是一个常量,而且必须用常量表达式来初始化,比如说下面的情况则是不正确的:


  
  
  1. int t = 10;
  2. constexpr int q = t + 20;
  3. cout << "q" << q << endl;

需要将t声明为 const 才是正确的;

一般来说,如果你认定变量是一个常量表达式,那就把它声明为constexpr类型;

std::array

std::array除了有传统数组支持随机访问、效率高、存储大小固定等特点外,还支持迭代器访问、获取容量、获得原始指针等高级功能.


  
  
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <list>
  4. #include <iostream>
  5. #include <map>
  6. #include <array>
  7. using namespace std;
  8. int main()
  9. {
  10. array< int, 4> arrayDemo = { 5, 6, 7, 8};
  11. for ( auto itor : arrayDemo)
  12. cout << itor << endl;
  13. int arraySize = sizeof(arrayDemo);
  14. cout << arraySize << endl;
  15. system( "pause");
  16. return 0;
  17. }

std::forward_list

td::forward_list为C++ 11新增的线性表,与list区别在于它是单向链表。我们在学习数据结构的时候都知道,链表在对数据进行插入和删除是比顺序存储的线性表有优势,因此在插入和删除操作频繁的应用场景中,使用list和forward_list比使用array、vector和deque效率要高很多。


  
  
  1. #include <forward_list>
  2. using namespace std;
  3. int main()
  4. {
  5. forward_list< int> numbers = { 1, 2, 3, 4, 5};
  6. for ( auto number : numbers)
  7. cout << number << endl;
  8. numbers.remove( 4);
  9. for ( auto number : numbers)
  10. cout << number << endl;
  11. system( "pause");
  12. return 0;
  13. }

std::unordered_map

std::unordered_map与std::map用法基本差不多,但STL在内部实现上有很大不同,std::map使用的数据结构为二叉树,而std::unordered_map内部是哈希表的实现方式,哈希map理论上查找效率为O(1)。但在存储效率上,哈希map需要增加哈希表的内存开销。

std::unordered_set

std::unordered_set的数据存储结构也是哈希表的方式结构,除此之外,std::unordered_set在插入时不会自动排序,这都是std::set表现不同的地方。


  
  
  1. #include <list>
  2. #include <iostream>
  3. #include <map>
  4. #include <array>
  5. #include <forward_list>
  6. #include <unordered_set>
  7. #include <set>
  8. using namespace std;
  9. int main()
  10. {
  11. unordered_set< int> unorder_set;
  12. unorder_set.insert( 7);
  13. unorder_set.insert( 5);
  14. unorder_set.insert( 3);
  15. unorder_set.insert( 4);
  16. unorder_set.insert( 1);
  17. for ( auto itor : unorder_set)
  18. cout << itor << endl;
  19. set< int> set;
  20. set.insert( 7);
  21. set.insert( 5);
  22. set.insert( 3);
  23. set.insert( 4);
  24. set.insert( 1);
  25. for ( auto itor : set)
  26. cout << itor << endl;
  27. system( "pause");
  28. return 0;
  29. }

cbegin和cend

begin():Return iterator to beginning (public member function )
cbegin():Return const_iterator to beginning (public member function )

后者返回的是一个不可修改的迭代器,前者可修改。


在C++11以前,C++的多线程编程均需依赖系统或第三方接口实现,一定程度上影响了代码的移植性。C++11中,引入了boost库中的多线程部分内容,形成C++标准,形成标准后的boost多线程编程部分接口基本没有变化,这样方便了以前使用boost接口开发的使用者切换使用C++标准接口,把容易把boost接口升级为C++接口。

  我们通过如下几部分介绍C++11多线程方面的接口及使用方法。

std::thread

std::thread为C++11的线程类,使用方法和boost接口一样,非常方便,同时,C++11的std::thread解决了boost::thread中构成参数限制的问题.


  
  
  1. #include <thread>
  2. using namespace std;
  3. void fun(int a) {
  4. a++;
  5. cout << a << endl;
  6. }
  7. int main()
  8. {
  9. int a = 0;
  10. thread t(fun, a); //创建一个线程 t,t 调用函数fun,a作为fun的参数
  11. t.join(); //启动线程t,并阻塞主线程,等到线程t运行结束后,再继续运行主线程
  12. system( "pause");
  13. return 0;
  14. }

            thread类当中的两个成员函数,join()和detach()。这两个成员的作用就像上面代码的注释那样,启动新生成的线程的,但是区别在于join()函数是启动子线程而阻塞主线程,当子线程运行结束后,才会继续运行主线程。相比之下,detach()函数的作用是启动子线程,并且让子线程和主线程分离,子线程和主线程各运行各的,虽然两个线程会因为共享内存池的原因在操作系统的层面发生发生阻塞等关系,但是在代码层次上,两个线程并不存在谁阻塞谁,很可能主线程已经运行结束了,子线程还在运行。

std::atomic

所谓的原子操作,取的就是“原子是最小的、不可分割的最小个体”的意义,它表示在多个线程访问同一个全局资源的时候,能够确保所有其他的线程都不在同一时间内访问相同的资源。也就是他确保了在同一时刻只有唯一的线程对这个资源进行访问。这有点类似互斥对象对共享资源的访问的保护,但是原子操作更加接近底层,因而效率更高。
在以往的C++标准中并没有对原子操作进行规定,我们往往是使用汇编语言,或者是借助第三方的线程库,例如intel的pthread来实现。在新标准C++11,引入了原子操作的概念,并通过这个新的头文件提供了多种原子操作数据类型,例如,atomic_bool,atomic_int等等,如果我们在多个线程中对这些类型的共享资源进行操作,编译器将保证这些操作都是原子性的,也就是说,确保任意时刻只有一个线程对这个资源进行访问,编译器将保证,多个线程访问这个共享资源的正确性。从而避免了锁的使用,提高了效率。
std::atomic对int, char, bool等数据结构进行原子性封装,在多线程环境中,对std::atomic对象的访问不会造成竞争-冒险。利用std::atomic可实现数据结构的无锁设计

 


  
  
  1. #include <thread>
  2. #include <atomic>
  3. using namespace std;
  4. atomic_bool bIsReady = false;
  5. atomic_int iCount = 100;
  6. void fun() {
  7. if (!bIsReady) {
  8. this_thread::yield();
  9. }
  10. while (iCount > 0)
  11. {
  12. printf( "iCount:%d\r\n", iCount--);
  13. }
  14. }
  15. int main()
  16. {
  17. list<thread> lstThread;
  18. for ( int i = 0; i < 10; ++i) {
  19. lstThread.push_back(thread(fun));
  20. }
  21. for ( auto &th : lstThread)
  22. {
  23. th.join();
  24. }
  25. system( "pause");
  26. return 0;
  27. }

std::condition_variable

C++11中的std::condition_variable就像Linux下使用pthread_cond_wait和pthread_cond_signal一样,可以让线程休眠,直到被唤醒,再从新执行。线程等待在多线程编程中使用非常频繁,经常需要等待一些异步执行的条件的返回结果。


  
  
  1. #include <set>
  2. #include <thread>
  3. #include <atomic>
  4. #include <condition_variable>
  5. #include <mutex>
  6. using namespace std;
  7. mutex mtx;
  8. condition_variable cv;
  9. bool ready = false;
  10. void print_id(int id) {
  11. unique_lock<mutex> lck(mtx);
  12. while (!ready)
  13. cv.wait(lck);
  14. cout << "thread" << id << endl;
  15. }
  16. void go() {
  17. unique_lock<mutex> lck(mtx);
  18. ready = true;
  19. cv.notify_all();
  20. }
  21. int main()
  22. {
  23. thread threads[ 10];
  24. for ( int i = 0; i < 10; ++i)
  25. threads[i] = thread(print_id, i);
  26. cout << "10 threads ready to race..." << endl;
  27. go();
  28. for ( auto &th : threads)
  29. th.join();
  30. system( "pause");
  31. return 0;
  32. }

上面的代码,调用cv.wait(lck)的时候,线程将进入休眠,在调用go函数之前,10个线程都处于休眠状态,当cv.notify_all()运行后,休眠将结束,继续往下运行,最终输出如上结果。


在内存管理方面,C++11的std::auto_ptr基础上,移植了boost库中的智能指针的部分实现,如std::shared_ptr、std::weak_ptr等,当然,想boost::thread一样,C++11也修复了boost::make_shared中构造参数的限制问题。

  什么是智能指针?网上已经有很多解释,个人觉得“智能指针”这个名词似乎起得过于“霸气”,很多初学者看到这个名词就觉得似乎很难。

  简单地说,智能指针只是用对象去管理一个资源指针,同时用一个计数器计算当前指针引用对象的个数,当管理指针的对象增加或减少时,计数器也相应加1或减1,当最后一个指针管理对象销毁时,计数器为1,此时在销毁指针管理对象的同时,也把指针管理对象所管理的指针进行delete操作。

  如下图所示,简单话了一下指针、智能指针对象和计数器之间的关系:

  

std::shared_ptr

std::shared_ptr包装了new操作符动态分别的内存,可以自由拷贝复制,基本上是使用最多的一个智能指针类型


  
  
  1. // ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <string>
  5. #include <memory>
  6. #include <iostream>
  7. using namespace std;
  8. class Test {
  9. public:
  10. Test()
  11. {
  12. cout << "Test()" << endl;
  13. }
  14. ~Test()
  15. {
  16. cout << "~Test()" << endl;
  17. }
  18. };
  19. int main()
  20. {
  21. shared_ptr<Test> p1 = make_shared<Test>();
  22. cout << "1 ref" << p1.use_count() << endl;
  23. shared_ptr<Test> p2 = p1;
  24. cout << "2 ref" << p1.use_count() << endl;
  25. cout << "3 ref" << p1.use_count() << endl;
  26. system( "pause");
  27. return 0;
  28. }

1、std::make_shared封装了new方法,boost::make_shared之前的原则是既然释放资源delete由智能指针负责,那么应该把new封装起来,否则会让人觉得自己调用了new,但没有调用delete,似乎与谁申请,谁释放的原则不符。C++也沿用了这一做法。

2、随着引用对象的增加std::shared_ptr<Test> p2 = p1,指针的引用计数有1变为2,当p2退出作用域后,p1的引用计数变回1,当main函数退出后,p1离开main函数的作用域,此时p1被销毁,当p1销毁时,检测到引用计数已经为1,就会在p1的析构函数中调用delete之前std::make_shared创建的指针。

std::weak_ptr

std::weak_ptr网上很多人说其实是为了解决std::shared_ptr在相互引用的情况下出现的问题而存在的,C++官网对这个只能指针的解释也不多,那就先甭管那么多了,让我们暂时完全接受这个观点。

  std::weak_ptr有什么特点呢?与std::shared_ptr最大的差别是在赋值是,不会引起智能指针计数增加。

  我们下面将继续如下两点:

  1、std::shared_ptr相互引用会有什么后果;

  2、std::weak_ptr如何解决第一点的问题。

  A、std::shared_ptr相互引用的问题示例:


  
  
  1. //示例代码1.0 http://www.cnblogs.com/feng-sc/p/5710724.html
  2. #include <memory>
  3. class TestB;
  4. class TestA
  5. {
  6. public:
  7. TestA()
  8. {
  9. std:: cout << "TestA()" << std:: endl;
  10. }
  11. void ReferTestB(std::shared_ptr<TestB> test_ptr)
  12. {
  13. m_TestB_Ptr = test_ptr;
  14. }
  15. ~TestA()
  16. {
  17. std:: cout << "~TestA()" << std:: endl;
  18. }
  19. private:
  20. std:: shared_ptr<TestB> m_TestB_Ptr; //TestB的智能指针
  21. };
  22. class TestB
  23. {
  24. public:
  25. TestB()
  26. {
  27. std:: cout << "TestB()" << std:: endl;
  28. }
  29. void ReferTestB(std::shared_ptr<TestA> test_ptr)
  30. {
  31. m_TestA_Ptr = test_ptr;
  32. }
  33. ~TestB()
  34. {
  35. std:: cout << "~TestB()" << std:: endl;
  36. }
  37. std:: shared_ptr<TestA> m_TestA_Ptr; //TestA的智能指针
  38. };
  39. int main()
  40. {
  41. std:: shared_ptr<TestA> ptr_a = std::make_shared<TestA>();
  42. std:: shared_ptr<TestB> ptr_b = std::make_shared<TestB>();
  43. ptr_a->ReferTestB(ptr_b);
  44. ptr_b->ReferTestB(ptr_a);
  45. return 0;
  46. }

运行结果:

    

  大家可以看到,上面代码中,我们创建了一个TestA和一个TestB的对象,但在整个main函数都运行完后,都没看到两个对象被析构,这是什么问题呢?

  原来,智能指针ptr_a中引用了ptr_b,同样ptr_b中也引用了ptr_a,在main函数退出前,ptr_a和ptr_b的引用计数均为2,退出main函数后,引用计数均变为1,也就是相互引用。

  这等效于说:

    ptr_a对ptr_b说,哎,我说ptr_b,我现在的条件是,你先释放我,我才能释放你,这是天生的,造物者决定的,改不了。

    ptr_b也对ptr_a说,我的条件也是一样,你先释放我,我才能释放你,怎么办?

  是吧,大家都没错,相互引用导致的问题就是释放条件的冲突,最终也可能导致内存泄漏。

  B、std::weak_ptr如何解决相互引用的问题

  我们在上面的代码基础上使用std::weak_ptr进行修改:


  
  
  1. //示例代码1.0 http://www.cnblogs.com/feng-sc/p/5710724.html
  2. #include <memory>
  3. class TestB;
  4. class TestA
  5. {
  6. public:
  7. TestA()
  8. {
  9. std:: cout << "TestA()" << std:: endl;
  10. }
  11. void ReferTestB(std::shared_ptr<TestB> test_ptr)
  12. {
  13. m_TestB_Ptr = test_ptr;
  14. }
  15. void TestWork()
  16. {
  17. std:: cout << "~TestA::TestWork()" << std:: endl;
  18. }
  19. ~TestA()
  20. {
  21. std:: cout << "~TestA()" << std:: endl;
  22. }
  23. private:
  24. std::weak_ptr<TestB> m_TestB_Ptr;
  25. };
  26. class TestB
  27. {
  28. public:
  29. TestB()
  30. {
  31. std:: cout << "TestB()" << std:: endl;
  32. }
  33. void ReferTestB(std::shared_ptr<TestA> test_ptr)
  34. {
  35. m_TestA_Ptr = test_ptr;
  36. }
  37. void TestWork()
  38. {
  39. std:: cout << "~TestB::TestWork()" << std:: endl;
  40. }
  41. ~TestB()
  42. {
  43. std:: shared_ptr<TestA> tmp = m_TestA_Ptr.lock();
  44. tmp->TestWork();
  45. std:: cout << "2 ref a:" << tmp.use_count() << std:: endl;
  46. std:: cout << "~TestB()" << std:: endl;
  47. }
  48. std::weak_ptr<TestA> m_TestA_Ptr;
  49. };
  50. int main()
  51. {
  52. std:: shared_ptr<TestA> ptr_a = std::make_shared<TestA>();
  53. std:: shared_ptr<TestB> ptr_b = std::make_shared<TestB>();
  54. ptr_a->ReferTestB(ptr_b);
  55. ptr_b->ReferTestB(ptr_a);
  56. std:: cout << "1 ref a:" << ptr_a.use_count() << std:: endl;
  57. std:: cout << "1 ref b:" << ptr_a.use_count() << std:: endl;
  58. return 0;
  59. }

运行结果:

      

  由以上代码运行结果我们可以看到:

  1、所有的对象最后都能正常释放,不会存在上一个例子中的内存没有释放的问题。

  2、ptr_a 和ptr_b在main函数中退出前,引用计数均为1,也就是说,在TestA和TestB中对std::weak_ptr的相互引用,不会导致计数的增加。在TestB析构函数中,调用std::shared_ptr<TestA> tmp = m_TestA_Ptr.lock(),把std::weak_ptr类型转换成std::shared_ptr类型,然后对TestA对象进行调用。


std::function、std::bind封装可执行对象

std::bind和std::function也是从boost中移植进来的C++新标准,这两个语法使得封装可执行对象变得简单而易用。此外,std::bind和std::function也可以结合我们一下所说的lamda表达式一起使用,使得可执行对象的写法更加“花俏”。

  此处省略很多字。。。。。。

lamda表达式

形式:

[capture](parameters) mutable ->return-type{statement}

1.[var]表示值传递方式捕捉变量var;
2.[=]表示值传递方式捕捉所有父作用域的变量(包括this);
3.[&var]表示引用传递捕捉变量var;
4.[&]表示引用传递方式捕捉所有父作用域的变量(包括this);
5.[this]表示值传递方式捕捉当前的this指针。

如:


  
  
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <memory>
  4. #include <iostream>
  5. #include <functional>
  6. using namespace std;
  7. int main()
  8. {
  9. auto add = []( int a, int b)-> int {
  10. return a + b;
  11. };
  12. int ret = add( 1, 2);
  13. cout << "ret" << ret << endl;
  14. return 0;
  15. }

如:


  
  
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. auto func = [] () { cout << "Hello world"; };
  6. func(); // now call the function
  7. }

如: 


  
  
  1. //Qt5 Lambda表达式
  2. //这里需要注意 Lambda表达式是C++ 11 的内容,所以,需要再Pro项目文件中加入 CONFIG += C++ 11
  3. QObject::connect(ui->pushButton_3,&QPushButton::clicked,[=](){qDebug()<< "lambda 表达式";});

参考:https://www.cnblogs.com/mengfanrong/p/5230558.html

https://www.cnblogs.com/feng-sc/p/5710724.html

https://blog.csdn.net/yhc166188/article/details/80572108

https://www.cnblogs.com/yuankaituo/p/5135750.html

猜你喜欢

转载自blog.csdn.net/glw0223/article/details/101935160
今日推荐