栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

(1)关键字和语法

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

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

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

for(Type val : container)
{
   cout< 

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

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

(2)绑定器和函数对象 函数对象

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

绑定器

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

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

自定义实现my_find_if、mybind1st:

#include
#include
#include
#include
#include

using namespace std;

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

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

template
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
_mybind1st mybind1st(Compare comp, const T &val) {
    return _mybind1st(comp, val);
}

int main() {
    vector 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());
    showContainer(vec);

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

    system("pause");
    return 0;
}
lambda表达式 (3) 智能指针

裸指针定义:

#include 
using namespace std;

int main() {
    //裸指针
    //data段 heap堆 Stack栈
    int *p = new int(10);
    cout << *p << endl;
    delete p;
    system("pause");
    return 0;
}

智能指针:

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

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

#include 
using namespace std;

template
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 ptr1(new int);
    *ptr1 = 20;

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

不带引用计数的智能指针

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

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

auto_ptr / scoped_ptr / unique_ptr

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

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

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

scoped_ptr(const scoped_ptr&) = delete;
scoped_ptr& operator=(const scoped_ptr&) = delete;

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

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

带引用计数的智能指针

定义:

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

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

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

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

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

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

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

(以下三种应用)

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

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

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

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

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

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

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

(4)容器

set和map:红黑树 O(lgn)

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

array:数组 vector

forward_list:前向链表 list

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

createThread

pthread_create

uSSH1rYi-1657069250913)]

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1010243.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号