目录
1、在C++98中,标准允许使用花括号{}对数组元素进行统一的列表初始值设定。对于一些自定义的类型,却无法使用这样的初始化。C++11扩大了用大括号括起的列表(初始化列表)的使用范围,使其可用于所有的内置类型和用户自定义的类型,使用初始化列表时,可添加等号(=),也可不添加。
2、多个对象想要支持列表初始化,需给该类(模板类)添加一个带有initializer_list类型参数的构造函数即可。注意:initializer_list是系统自定义的类模板,该类模板中主要有三个方法:begin()、end()迭代器以及获取区间中元素个数的方法size()。
- int main()
- {
- //initializer_list,主要用于容器初始化
- auto il1 = { 10,50,90 };
- std::initializer_list<int> il2 = { 10,50,80 };
- cout << typeid(il1).name() << endl;
-
- vector<int> v = {1,2,3,4,5};
- list<int> lt = { 1,2,3,4,5 };
- map
int> m = { make_pair("sort",1),{"insert",2} }; -
- return 0;
- }
1、C++11中,可以使用auto来根据变量初始化表达式类型推导变量的实际类型,可以给程序的书写提供许多方便。auto使用的前提是:必须要对auto声明的类型进行初始化,否则编译器无法推导出auto的实际类型。decltype是根据表达式的实际类型推演出定义变量时所用的类型。
- int main()
- {
- auto pf = {1,2,3,4};
- decltype(pf) px; //创建一个和pf类型一致的变量
-
- cout <<"pf: " << typeid(pf).name() << endl;
- cout <<"px: " << typeid(px).name() << endl;
-
- int x = 1;
- double y = 1.0;
-
- //自动推导类型
- decltype(x*y) ret;
- cout << typeid(ret).name() << endl;
- return 0;
- }
- void Fun(int& x) { cout << "左值引用" << endl; }
- void Fun(int&& x) { cout << "右值引用" << endl; }
- void Fun(const int& x) { cout << "const 左值引用" << endl; }
- void Fun(const int&& x) { cout << "const 右值引用" << endl; }
- template<typename T>
- void PerfectForward(T&& t)
- {
- Fun(std::forward
(t)); - }
- int main()
- {
- PerfectForward(10);
-
- int a;
- PerfectForward(a);
- PerfectForward(std::move(a));
- const int b = 10;
- PerfectForward(b);
- PerfectForward(std::move(b));
- return 0;
- }
右值引用出来以后,并不是直接使用右值引用去减少拷贝,提高效率。而是支持深拷贝的类,提供移动构造和移动赋值这时这些类的对象进行传值返回或者是参数为右值时,则可以用移动构造和移动赋值,转移资源,避免深拷贝,提高效率。
- int f(int i, int j)
- {
- return i + j;
- }
- //左值引用只能引用左值,不能引用右值(const左值可以引用右值)
- //右值引用不可以直接引用左值,可以引用move后的左值
- //左值引用使用场景:做参数、做返回值
-
- //移动构造
- //string(string&& s)
- //{
- // : _str(nullptr)
- // , _size(0);
- // , _capacity(0);
- // tihs->swap(s);
- //}
-
- //移动赋值
- //string& operator=(string&& s)
- //{
- // tihs->swap(s);
- // return *this;
- //}
-
- int main()
- {
- auto pf = { 1,2,3,4 };
- decltype(pf) px;
- int x = 1;
- double y = 1.0;
-
- //右值引用
- int&& rr1 = 100;
- int&& rr2 = x * y;
- int&& rr3 = f(x, y);
-
- int a = x;
- //int&& rr4 = x;
- int&& rr4 = std::move(x);
-
- return 0;
- }
- /*int main()
- {
- int a = 10, b = 20;
- //lambda表达式
- auto add = [](int x, int y)->int {return x + y; };
- cout << add(a,b) << endl;
- auto sub = [a, b]()->int {return a - b; };
- cout << sub() << endl;
- return 0;
- }*/
- int main()
- {
- int a = 10, b = 20;
-
- //标准写法
- //auto swap = [](int& x, int& y)->void {
- // int temp = x;
- // x = y;
- // y = temp;
- //};
- //swap(a,b);
-
- //利用捕捉列表(省略参数和返回值) 注意!!!!使用mutable后参数就算为空也不能省略
- //auto swap = [&a,&b]{
- // int temp = a;
- // a = b;
- // b = temp;
- //};
- //swap();
-
- //全捕捉
- auto swap = [&] {
- int temp = a;
- a = b;
- b = temp;
- };
- swap();
-
- return 0;
- }
-
- //终止函数
- void showListArg(){
- cout << endl;
- }
-
- //展开函数
- template <class T,class ...Args>
- void showListArg(T val,Args... args){
- cout << val << endl;
- showListArg(args...);
- }
-
- template <class ...Args>
- void showList(Args... args){
- showListArg(args...);
- }
-
- int main()
- {
- showList();
- showList(1,'A');
- showList(1, 'A', string("sort"));
-
- return 0;
- }
- //终止函数
- void showList() {
- cout << endl;
- }
-
- //展开函数
- template <class T>
- void printArg(T val) {
- cout << val << endl;
- }
-
- template <class ...Args>
- void showList(Args... args) {
- int arr[] = {(printArg(args),0)...};
- cout << endl;
- }
-
- int main()
- {
- showList();
- showList(1,2,3,4);
- showList(1, 2,3,4,5,6,7,8);
-
- return 0;
- }
- int f1(int a, int b)
- {
- return a + b;
- }
-
- struct Functor1
- {
- public:
- int operator() (int a, int b)
- {
- return a + b;
- }
- };
-
- class Plus
- {
- public:
- static int plusi(int a, int b)
- {
- return a + b;
- }
-
- double plusd(double a, double b)
- {
- return a + b;
- }
- };
-
- int main()
- {
- // 包装函数指针
- std::function<int(int, int)> ff1 = f1;
- cout << ff1(1, 2) << endl;
-
- // 包装仿函数
- std::function<int(int, int)> ff2 = Functor1();
- cout << ff2(1, 2) << endl;
-
- // 包装成员函数
- std::function<int(int, int)> ff3 = Plus::plusi;
- cout << ff3(1, 2) << endl;
-
- //std::function
ff4 = &Plus::plusd; - //cout << ff4(Plus(),1.1, 2.2) << endl;
-
- // 包装lambda表达式
- auto f5 = [](int a, int b){return a + b; };
- std::function<int(int, int)> ff5 = f5;
- cout << ff5(1, 2) << endl;
- }
- //bind
- int Plus(int a, int b)
- {
- return a + b;
- }
-
- class Sub
- {
- public:
- int sub(int a, int b)
- {
- return a - b;
- }
- };
-
- int main()
- {
- std::function<int(int, int)> f1 = bind(Plus, placeholders::_1, placeholders::_2);
- cout << f1(1, 2) << endl;
-
- // 想把plus绑定成一个值+10
- std::function<int(int)> f2 = bind(Plus, 10, placeholders::_1);
- cout << f2(5) << endl;
-
- // 绑定固定的可调用对象
- std::function<int(int, int)> f3 = bind(&Sub::sub, Sub(), placeholders::_1, placeholders::_2);
- cout << f3(1, 2) << endl;
-
- std::function<int(int, int)> f4 = bind(&Sub::sub, Sub(), placeholders::_2, placeholders::_1);
- cout << f4(1, 2) << endl;
-
- return 0;
- }

- //多线程(写法有问题,不推荐这样用)
- int main()
- {
- int n = 0;
- cin >> n;
- vector
vThreads; - vThreads.resize(n);
-
- mutex mtx;
- int N = 100;
- int x = 0;
- for (auto& th : vThreads)
- {
- th = thread([&mtx, &N, &x]
- {
- for (int i = 0; i < N; i++)
- {
- mtx.lock();
- cout << this_thread::get_id() << ":" << x << endl;
- ++x;
- this_thread::sleep_for(chrono::milliseconds(500));//500ms
- mtx.unlock();
- }
- });
- }
-
- for (auto& th : vThreads)
- {
- th.join();
- }
- return 0;
- }
- // RAII
- namespace zsd
- {
- template<class Lock>
- class lock_guard
- {
- public:
- lock_guard(Lock& lock)
- :_lock(lock)
- {
- _lock.lock();
- cout << "加锁" << endl;
- }
-
- ~lock_guard()
- {
- _lock.unlock();
- cout << "解锁" << endl;
- }
-
- lock_guard(const lock_guard
& lock) = delete; -
- private:
- Lock& _lock;
- };
- }
-
- int main()
- {
- mutex mtx;
- {
- zsd::lock_guard
lg(mtx) ; - }
- printf("sssss");
- return 0;
- }
- #include
- #include
- #include
- #include
- using namespace std;
-
- //int main()
- //{
- // mutex mtx;
- // int n = 100;
- // //如果线程二迟迟没有创建好或者没有排到时间片,就会导致t1连续打印奇数,不符合交替打印要求
- // thread t1([&]()
- // {
- // int i = 1;
- // for (; i < n; i += 2)
- // {
- // unique_lock
lock(mtx); - // cout << i << endl;
- // }
- // });
- //
- // thread t2([&]()
- // {
- // int j = 2;
- // for (; j < n; j += 2)
- // {
- // unique_lock
lock(mtx); - // cout << j << endl;
- // }
- // });
- //
- // t1.join();
- // t2.join();
- //
- // return 0;
- //}
-
- int main()
- {
- mutex mtx;
- condition_variable cv;
- int n = 100;
- bool flag = true;
-
- //打印奇数
- thread t1([&]()
- {
- int i = 1;
- for (; i < n;)
- {
- unique_lock
lock(mtx); - cv.wait(lock, [&flag]()->bool {return flag; });
- cout << i << endl;
- i += 2;
- flag = false;
- cv.notify_one();
- }
- });
-
- //打印偶数
- thread t2([&]()
- {
- int j = 2;
- for (; j <= n; )
- {
- unique_lock
lock(mtx); - cv.wait(lock, [&flag]()->bool {return !flag; });
- cout << j << endl;
- j += 2;
- flag = true;
- cv.notify_one();
- }
- });
-
- t1.join();
- t2.join();
-
- return 0;
- }