for
auto
decltype
#include #include #include #include #include using namespace std; void Feach() { vector 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; } } void Find() { unordered_multimap m = { {'a', 1}, {'a', 3}, {'b', 2}, }; auto v = m.equal_range('a'); 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; } } 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; } 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; }
模板:
using
#include #include #include #include #include class A { public: static int get() { return 0; } }; class B { public: static int* get() { return new int(3); } }; template void F() { auto v = T::get(); } template void F1() { T v = U::get(); } template R F2(X a, Y b) { return a + b; } template //推导返回类型 decltype((*(X*)0) + Y()) F3(X a, Y b) { return a + b; } template //返回类型后置 auto F4(X a, Y b) -> decltype(a+b) { return a + b; } int main() { F(); F(); F1(); F1(); int a = 1; float b = 1.2; std::cout << F2(a, b) << std::endl; std::cout << F2(a, b) << std::endl;//我们不需要知道返回类型 std::cout << F3(a, b) << std::endl; std::cout << F4(a, b) << std::endl; return 0; }
#include #include #include #include #include using namespace std; template class A { typename T::iterator it_; public: void F(T& it) { it_ = it.begin(); } }; //模板特化 template class A { typename T::const_iterator it_; public: void F(const T& it) { it_ = it.begin(); } }; //更简洁的方法 template class B { decltype(T().begin()) it_; public: void F(T& it) { it_ = it.begin(); } }; void Fiterator() { typedef vector V_CT; const V_CT v; //const V_CT::iterator it = v.begin(); error //A a; A a; a.F(v); V_CT v1; A b; b.F(v1); const V_CT v2; A c; c.F(v2); } template struct C { typedef map M_TYPE; }; //重定义模板 template using M_TYPE1 = map; void FMap() { C::M_TYPE s; M_TYPE1 s1; } void test(int v) { cout << v << endl; } int test1(int v) { cout << v << endl; } template struct FD { typedef void(*FUN_TYPE)(T); }; template using FUN_TYPE1 = void(*)(T); void Fpointer() { FD::FUN_TYPE f1 = test; (*f1)(2); //FUN_TYPE1 f2 = test1; error FUN_TYPE1 f2 = test; f2(2.2); } template R F(T v) { return v; } int main() { Fiterator(); FMap(); cout << F(2.2) << endl;//从右向左填充模板参数 return 0; }
function
bind
smart ptr
#include #include #include #include #include #include using namespace std; 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 p(new int(1)); shared_ptr p1 = p; cout << p.use_count() << endl; p1.reset(); cout << p.use_count() << endl;//weak count shared_ptr p2; p2.reset(new int(1)); if (p2) { int* sp = p2.get(); cout << "not null " << *sp << endl; } shared_ptr p3 = make_shared(2); unique_ptr u(new int(1)); //unique_ptr u1 = u; error unique_ptr u2 = move(u); unique_ptr u_arr(new int[10]); u_arr[1] = 2; cout << u_arr[0] << endl; weak_ptr 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 sh(h, [](void*p) { A::Destroy(p); }); } 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& f) { f(); } void Ffunc2() { function f = Fun; f(); Obj o; function f2 = o; f2(); function ff = bind(Obj()); ff(); function f1 = Obj2::F1; cout << f1(2) << endl; Obj2 o2; function f3 = bind(&Obj2::F, &o2); f3(); Ffunc1(f); Ffunc1(Fun); //函数参数必须const } 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 v = {2, 3, 10, 12}; cout << count_if(v.begin(), v.end(), bind1st(less(), 10)) << endl; cout << count_if(v.begin(), v.end(), bind2nd(less(), 10)) << endl; cout << count_if(v.begin(), v.end(), bind(less(), 10, placeholders::_1)) << endl; cout << count_if(v.begin(), v.end(), bind(less(), placeholders::_1, 10)) << endl; // (6, 12) auto f1 = bind(greater(), placeholders::_1, 6); auto f2 = bind(less(), placeholders::_1, 12); auto f3 = bind(logical_and(), f1, f2); cout << count_if(v.begin(), v.end(), f3) << endl; } int main() { //Fptr(); Ffunc(); Ffunc2(); Fbind(); return 0; }
初始化列表
右值引用
#include #include #include #include #include using namespace std; class A { vector v_; public: A(initializer_list l) { for(auto it = l.begin(); it != l.end(); ++it) { v_.push_back(*it); } } }; class B { map m_; using pair = map::value_type; public: B(initializer_list l) { for(auto it = l.begin(); it != l.end(); ++it) { m_.insert(*it); } } }; void Finit() { A a = {1, 2, 3}; vector v = {1, 2, 3}; B b = { {1, 2}, {3, 4}}; int c = {1.2}; //conversion warnning } 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 #include #include #include #include using namespace std; 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 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 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 v = {1, 2, 3, 4, 5}; cout << count_if(v.begin(), v.end(), C()) << endl; return 0; }
上一篇 成功解决AttributeError: module ‘tensorflow‘ has no attribute ‘contrib‘
下一篇 python中(5,)和(5,1)的区别以及如何转换
版权所有 (c)2021-2022 MSHXW.COM
ICP备案号:晋ICP备2021003244-6号