IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> c++11总结 -> 正文阅读

[C++知识库]c++11总结

for

auto

decltype


#include<iostream>
#include<vector>
#include<map>
#include<unordered_map>
#include<algorithm>

using namespace std;

/********
 * for( : )
 *
*********/
void Feach() {
        vector<int> v = {1, 2, 3};
        for(auto t : v) {
                cout << t << endl;
        }

        for(auto& t : v) {
                t += 1;
        }

        for(const auto& t : v) {
                cout << t << endl;
        }
}


/********
 *for_each
 *lamda
 *auto
*********/
void Find() {
        unordered_multimap<char, int> m = {
                {'a', 1},
                {'a', 3},
                {'b', 2},
        };
        auto v = m.equal_range('a');

/*
        for_each(v.first, v.second, [](unordered_map<char, int>::value_type& x) { 
                        cout << x.second << endl;
                });
*/


        for_each(v.first, v.second, [](decltype(m)::value_type& x) { 
                        cout << x.second << endl;
                });

        for(auto it = v.first; it != v.second; ++it) {
                cout << it->first << " " << it->second << endl;
        }
}



/********
 *decltype
 *
*********/

int F() {
        return 0;
}
void Fdecl() {

        int a = 0;
        decltype(a) b = 2;

        const int& a1 = a;
        decltype(a1) a2 = a;
        //a2 = 2; read-only

        decltype(2 * 5.0) c = 2.2; //double

        int* p = &a;
        decltype(p) p1 = p; //int*

        decltype(a + c) d = c;  //double
        decltype(a += c) e = a; //int&
        e = 1;
        cout << "a " << a << endl;

        decltype(F()) f = 0;
}

/********
 *auto
 *
*********/
void Fauto() {
        auto a = 2;
        auto b = new auto(2.2);
        cout << typeid(b).name() << endl;
        const auto* p = &a;
        auto p1 = p;

        auto& a1 = a;
        a1 = 3;
        const int& a2 = a;
        auto& a3 = a2;
        //a3 = 4; 保留const,编译报错:1

        const int c = 3;
        auto c1 = c;//去掉const
        c1 = 2;

}

int main() {

        int a = 0;
        int&& b = 0;
        cout << &b << endl;
        //int&& c = b;

        Fauto();
        //Feach();
        //Find();
        Fdecl();
        return 0;
}

模板:?

decltype

using

#include<iostream>
#include<vector>
#include<map>
#include<unordered_map>
#include<algorithm>

class A {
public:
        static int get() {
                return 0;
        }
};

class B {
public:
        static int* get() {
                return new int(3);
        }
};

/********
 *auto推导
 *原始方法
*********/
template <class T>
void F() {
        auto v = T::get();
}

template <class T, class U>
void F1() {
        T v = U::get();
}


/********
 *返回类型推导
 *
*********/
template <class R, class X, class Y>
R F2(X a, Y b) {
        return a + b;
}

template <class X, class Y> //推导返回类型
decltype((*(X*)0) + Y()) F3(X a, Y b) {
        return a + b;
}

template <class X, class Y> //返回类型后置
auto F4(X a, Y b) -> decltype(a+b) {
        return a + b;
}

int main() {
        F<A>();
        F<B>();

        F1<int,A>();
        F1<int*,B>();

        int a = 1;
        float b = 1.2;
        std::cout << F2<int>(a, b) << std::endl;
        std::cout << F2<decltype(a+b)>(a, b) << std::endl;//我们不需要知道返回类型
        std::cout << F3(a, b) << std::endl;
        std::cout << F4(a, b) << std::endl;

        return 0;
}
#include<iostream>
#include<vector>
#include<map>
#include<unordered_map>
#include<algorithm>
using namespace std;


/********
 *decltype处理模板特化
 *
*********/
template <class T>
class A {
        typename T::iterator it_;
public:
        void F(T& it) {
                it_ = it.begin();
        }
};


//模板特化
template <class T>
class A<const T> {
        typename T::const_iterator it_;
public:

        void F(const T& it) {
                it_ = it.begin();
        }
};


//更简洁的方法
template <class T>
class B {
        decltype(T().begin()) it_;
public:
        void F(T& it) {
                it_ = it.begin();
        }

};

void Fiterator() {
        typedef vector<int> V_CT;

        const V_CT v;
        //const V_CT::iterator it = v.begin(); error

        //A<const V_CT> a;
        A<decltype(v)> a;
        a.F(v);


        V_CT v1;
        A<V_CT> b;
        b.F(v1);

        const V_CT v2;
        A<decltype(v)> c;
        c.F(v2);


}

/********
 * using
 *
*********/
template<class T>
struct C {
        typedef map<int, T> M_TYPE;

};


//重定义模板
template<class T>
using M_TYPE1 = map<int, T>;


void FMap() {
        C<string>::M_TYPE s;
        M_TYPE1<string> s1;
}

void test(int v) {
        cout << v << endl;
}

int test1(int v) {
        cout << v << endl;
}

template<class T>
struct FD {
        typedef void(*FUN_TYPE)(T);
};

template<class T>
using FUN_TYPE1 = void(*)(T);


void Fpointer() {
        FD<int>::FUN_TYPE f1 = test;
        (*f1)(2);

        //FUN_TYPE1<int> f2 = test1; error
        FUN_TYPE1<int> f2 = test; 
        f2(2.2);
}

template <class R = int, class T>
R F(T v) {
        return v;
}

int main() {
        Fiterator();
        FMap();
        cout << F(2.2) << endl;//从右向左填充模板参数
        return 0;
}

function

bind

smart ptr

#include<iostream>
#include<vector>
#include<map>
#include<unordered_map>
#include<algorithm>
#include<memory>
using namespace std;


/********
 *smart prt
 *
*********/
class A {
public:
        static void* Create() {
                return (void*) new int(3);
        }

        static void Destroy(void* p) {
                if (p != NULL) {
                        cout << "Destroy" << endl;
                        delete (int*)p;
                }
        }
};

void Fptr() {
        shared_ptr<int> p(new int(1));
        shared_ptr<int> p1 = p;
        cout << p.use_count() << endl;
        p1.reset();
        cout << p.use_count() << endl;//weak count

        shared_ptr<int> p2;
        p2.reset(new int(1));
        if (p2) {
                int* sp = p2.get();
                cout << "not null " << *sp << endl;
        }

        shared_ptr<int> p3 = make_shared<int>(2);

        unique_ptr<int> u(new int(1));
        //unique_ptr<int> u1 = u; error
        unique_ptr<int> u2 = move(u);
        unique_ptr<int []> u_arr(new int[10]);
        u_arr[1] = 2;
        cout << u_arr[0] << endl;

        weak_ptr<int> w(p);
        cout << w.use_count() << endl;

        if (!w.expired()) {
                auto tmp = w.lock();
                cout << "success " << *tmp << endl;
        }
        p.reset();
        if (w.expired()) {
                cout << "failed" << endl;
        }


        void* h = A::Create();
        shared_ptr<void> sh(h, [](void*p) { A::Destroy(p); });
}


/********
 *function
 *
*********/
void Fun() {
}

class Obj {
public:
        void operator()() {
        }
};


class Obj1 {
        using fun = void(*)();
public:
        static void F() {
        }
        operator fun() {
                return F;
        }
};

class Obj2 {
public:
        void F() {
        }

        static int F1(int a) {
                return a+2;
        }
};

void Ffunc() {
        void (*pf)() = &Fun;
        pf();

        Obj o;
        o();

        Obj1 o1;
        o1();

        void (Obj2::*mpf)() = &Obj2::F;
        Obj2 o3;
        (o3.*mpf)();
}

void Ffunc1(const function<void(void)>& f) {
        f();
} 

void Ffunc2() {
        function<void(void)> f = Fun;
        f();

        Obj o;
        function<void(void)> f2 = o;
        f2();

        function<void(void)> ff = bind(Obj());
        ff();

        function<int(int)> f1 = Obj2::F1;
        cout << f1(2) << endl;

        Obj2 o2;
        function<void(void)> f3 = bind(&Obj2::F, &o2);
        f3();

        Ffunc1(f);
        Ffunc1(Fun); //函数参数必须const
}


/********
 *bind
 *
*********/
void Add(int x, int y) {
        cout << x << "+" << y << endl;
}

void Fbind() {
        bind(Add, 1, 2)();
        bind(Add, placeholders::_1, 2)(1);
        bind(Add, 2, placeholders::_1)(1);
        //bind(Add, 2, placeholders::_2)(1);
        bind(Add, 2, placeholders::_2)(1, 2); //2 2
        bind(Add, placeholders::_2, placeholders::_1)(1, 2); //2 1
}

void Fbind1() {
        vector<int> v = {2, 3, 10, 12};
        cout << count_if(v.begin(), v.end(), bind1st(less<int>(), 10)) << endl;
        cout << count_if(v.begin(), v.end(), bind2nd(less<int>(), 10)) << endl;
        cout << count_if(v.begin(), v.end(), bind(less<int>(), 10, placeholders::_1)) << endl; 
        cout << count_if(v.begin(), v.end(), bind(less<int>(), placeholders::_1, 10)) << endl; 

        // (6, 12)
        auto f1 = bind(greater<int>(),  placeholders::_1, 6);
        auto f2 = bind(less<int>(),  placeholders::_1, 12);
        auto f3 = bind(logical_and<bool>(), f1, f2);
        cout << count_if(v.begin(), v.end(), f3) << endl;
}

int main() {
        //Fptr();
        Ffunc();
        Ffunc2();
        Fbind();
        return 0;
}

初始化列表

右值引用

#include<iostream>
#include<vector>
#include<map>
#include<unordered_map>
#include<algorithm>
using namespace std;


/********
 *initializer_list
 *
*********/
class A {
        vector<int> v_;
public:
        A(initializer_list<int> l) {
                for(auto it = l.begin(); it != l.end(); ++it) {
                        v_.push_back(*it);
                }
        }
};

class B {
        map<int, int> m_;
        using pair = map<int, int>::value_type;
public:
        B(initializer_list<pair> l) {
                for(auto it = l.begin(); it != l.end(); ++it) {
                        m_.insert(*it);
                }
        }
};

void Finit() {
        A a = {1, 2, 3};
        vector<int> v = {1, 2, 3};
        B b = { {1, 2}, {3, 4}};
        int c = {1.2}; //conversion warnning

}

/********
 *-fno-elide-constructors
 *
*********/
class C {
public:
        C() {
                cout << "construct" << endl;
        }

        C(const C&) {
                cout << "copy construct" << endl;
        }

        ~C() {
                cout << "destruct" << endl;
        }
};

C GetC() {
        return C();
}

class D {
static const int CNT = 2;
int arr_[CNT];
};
int main() {
        Finit();
        //C&& c = GetC();       //减少一次拷贝构造
        //const C& c = GetC();  //减少一次拷贝构造
        auto&& c = GetC();      //减少一次拷贝构造

        //自动推导左or右
        auto&& c1 = c;
        auto&& d = 2;           


        return 0;
}

lambda

#include<iostream>
#include<vector>
#include<map>
#include<unordered_map>
#include<algorithm>
using namespace std;

/********
 *lambda 
 *
*********/
class A {
public:
        int v_ = 0;
        void F(int a, int b) {
                //auto f1 = [] { return v_; } error
                auto f1 = [this] { return v_; };
                f1();
                auto f2 = [&] { return v_; };
                f2();
                auto f3 = [=] { return v_; };
                f3();
                auto f4 = [=](int c = 3) { F1(c); };
                f4();
                f4(2);

                auto f5 = [=]() { F1(a); };
                auto f6 = [&]() { F1(b); };
        }
        int F1(int a) {
                v_ = a;
        }

};

void Flambda() {
        auto f = [](int a) -> float { return a + 0.2; };
        cout << f(2) << endl;

        auto f1 = [](int a) { return a + 0.2; };
        cout << f1(3) << endl;

        int a = 0, b = 1;
        //auto f2 = [] { return a; }; error
        auto f2 = [a] { return a; }; //按值捕获,捕获的时候值已经复制
        a = 1;
        cout << "a=" << a << ", b=" << b << ", f2=" << f2() << endl;//f2输出?

        auto f2_0 = [&a] { return a; };
        a = 22;
        cout << "a=" << a << ", b=" << b << ", f2_0=" << f2_0() << endl;//引用没问题

        auto f3 = [&a] { a = 3; };
        f3();

        //auto f4 = [=,&a] { b = 10; a = 22; }; b read-only
        auto f4 = [=,&a] { a = b; }; 
        f4();
        auto f5 = [&] { a = 10; b = 11; }; 
        f5();
        cout << "a=" << a << ", b=" << b << endl;
}

void Flambda1() {
        vector<int> v = {1, 2, 6, 10};
        cout << count_if(v.begin(), v.end(), [](int x) { return (x < 10 && x > 5); }) << endl;
}
class B {
        int& v_;
public:
        B(int& v) : v_(v) {
        }
        void operator()(int v) {
                if (v & 1) {
                        v_++;
                }
        }
};

void Flambda2() {
        vector<int> v = {1, 2, 3, 4, 5};
        int cnt = 0;
        for_each(v.begin(), v.end(), B(cnt));
        cout << cnt << endl;

        int cnt1 = 0;
        for_each(v.begin(), v.end(), [&cnt1](int a) { if (a & 1) cnt1++; });
        cout << cnt1 << endl;
}

struct C {
        bool operator()(int v) {
                return true;
        }
};


int main() {
        //Flambda();
        Flambda2();

        vector<int> v = {1, 2, 3, 4, 5};
        cout << count_if(v.begin(), v.end(), C()) << endl;
        return 0;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-10-12 23:15:23  更:2021-10-12 23:16:41 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 23:49:15-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码