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;
}
|