C++高级编程(二)——C++11新特性

(1)关键字和语法

auto:可以根据右值,推导出右值的类型,然后左边变量的类型也就已知(容器迭代器常用)

nullptr:给指针专用(能够和整数进行区别) #define NULL 0

foreach:可以遍历数组、容器等 (底层通过指针或者迭代器实现)

for(Type val : container)
{
   cout<<val<<" ";
}

右值引用:move移动语义函数和forward类型完美转发函数

模板的一个新特性:typename … A 表示可变参(类型参数)

(2)绑定器和函数对象

函数对象

函数对象我们之前以及提到了,函数对象就是类方法中有operator()运算符重载的。在使用时和函数很类似。

绑定器

bind1st和bind2nd在STL中主要用于二元函数对象,将其中的一元绑定成一个固定的量,成为一元函数变量。

bind1st和bind2nd+二元函数对象 =》 一元函数对象

自定义实现my_find_if、mybind1st

#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
#include<ctime>

using namespace std;

template<typename Container>
void showContainer(Container &con) {
    typename Container::iterator it = con.begin();
    for(;it != con.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
}

template<typename Iterator, typename Compare>
Iterator my_find_if(Iterator first, Iterator last, Compare comp) {
    for(; first != last; ++first) {
        if(comp(*first)) {
            return first;
        }
    }
    return last;
}

template<typename Compare, typename T>
class _mybind1st {
public:
   _mybind1st(Compare comp, T val) 
      :_comp(comp), _val(val) {}
    bool operator() (const T &second) {
       return _comp(_val, second);
    }
private:
   Compare _comp;
   T _val;
};

template<typename Compare, typename T>
_mybind1st<Compare,T> mybind1st(Compare comp, const T &val) {
    return _mybind1st<Compare, T>(comp, val);
}

int main() {
    vector<int> vec;
    srand(time(nullptr));
    for (int i = 0; i < 20; ++i) {
        vec.push_back(rand() % 100 + 1);
    }

    showContainer(vec);
    sort(vec.begin(), vec.end());
    showContainer(vec);

    //greater 二元函数对象
    sort(vec.begin(), vec.end(), greater<int>());
    showContainer(vec);

    //插入70
    auto it1 = my_find_if(vec.begin(), vec.end(), 
         mybind1st(greater<int>(), 70));
    
    if(it1 != vec.end()) {
        vec.insert(it1, 70);
    }
    showContainer(vec);

    system("pause");
    return 0;
}

lambda表达式

(3) 智能指针

裸指针定义:

#include <iostream>
using namespace std;

int main() {
    //裸指针
    //data段 heap堆 Stack栈
    int *p = new int(10);
    cout << *p << endl;
    delete p;
    system("pause");
    return 0;
}
/*
内存泄露:
(1)没有delete
(2)delete前return掉了
(3)代码直接异常
*/

智能指针

保证能够做到资源的自动释放

利用上的对象出作用域自动析构的特征,来做到资源的自动释放

#include <iostream>
using namespace std;

template<typename T>
class CSmartPtr
{
public:
    CSmartPtr(T *ptr = nullptr)
       :mptr(ptr) {}
    ~CSmartPtr() {delete mptr; }
    T& operator*() {return *mptr; }
    T* operator->() {return mptr; }
private:
    T *mptr;
};

class Test
{
   public:
      void test() { cout << " call Test::test" << endl; }
};

int main() {

    CSmartPtr<int> ptr1(new int);
    *ptr1 = 20;

    CSmartPtr<Test> ptr2(new Test());
    ptr2->test(); 
    //(*ptr2).test();
    system("pause");
    return 0;
}

不带引用计数的智能指针

普通智能指针解决不了浅拷贝问题

CSmartPtr<int> p1(new int);
CSmartPtr<int> p2(p1);//报错

auto_ptr / scoped_ptr / unique_ptr

auto_ptr:浅拷贝时,p1赋nullptr (源码中的release方法)

auto_ptr<int> p1(new int);
auto_ptr<int> p2(p1);

image-20220703110653970

image-20220703110741475

image-20220703111210606

scoped_ptr:浅拷贝时,报错,直接将拷贝构造删除了

scoped_ptr(const scoped_ptr<T>&) = delete;
scoped_ptr<T>& operator=(const scoped_ptr<T>&) = delete;

unique_ptr:浅拷贝时,move移动语义,实现拷贝构造

unique_ptr<int> p1(new int);
unique_ptr<int> p2(std::move(p1));
//进行当前变量的右值引用的类型强转

带引用计数的智能指针

定义:

多个智能指针可以管理同一个资源

给每一个对象资源,匹配一个引用计数

智能指针 =》 资源的时候 =》 引用计数+1

智能指针 =》不使用资源的时候 =》 引用计数-1 =》 !=0 0

shared_ptr: 强指针 可以改变资源的引用计数

weak_ptr: 弱指针 不可以改变资源的引用计数 只是一个观察者 无法访问资源

weak_ptr => shared_ptr => 资源(内存)

(以下三种应用)

应用一:强智能指针循环引用(交叉引用)是什么问题?什么结果?怎么解决?

造成new出来的资源无法释放!!资源泄露问题!!

定义对象的时候用强智能指针,引用对象的时候用弱智能指针

image-20220703134119317

image-20220703134524015

应用二:调用A对象方法,需要提升弱指针等级lock函数

image-20220703135331634

应用三:多线程访问共享对象的线程安全问题:

对已经析构的对象仍然访问了他的方法(普通指针)

C++中的thread库就是Linux底层的Pthread库

image-20220703141230518

image-20220703141218765

(4)容器

set和map:红黑树 O(lgn)

unordered_setunordered_map:哈希表 O(1) 提高增删查的效率

array:数组 vector

forward_list:前向链表 list

(5)C++语言级别支持的多线程编程

createThread

pthread_create

uSSH1rYi-1657069250913)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-特立独行的猪-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值