c++11新特性的简单介绍
2017-09-08 16:24
323 查看
#include #include #include #include #include #include #include #include using std::cout; using std::cin; using std::endl; using std::vector; using std::string; using std::for_each; using std::shared_ptr; using std::thread; std::mutex g_mutex; void my_thread1(int num, const string& str){ g_mutex.lock(); for(int i=0; i<10; i++){ cout << "num:" << num << ", name:" << str << endl; } g_mutex.unlock(); } void my_thread2(){ int g_num = 20; for (int i=0; i<10; i++){ cout << "num:" << g_num << endl; } } int main(int argc,char * argv [ ]){ //类型推导 //auto自动根据初始化子的类型产生参数,首先应有被明确初始化的参数 vector myvec(3, 4); for (auto itr = myvec.cbegin();itr != myvec.cend();itr++){ cout << *itr << endl; } int my_array[4] = {1,2,3,4}; for (auto &x : my_array){ x *= 2; cout << x << endl; } //lambda函数,用[&]声明 vector someList(2, 5); int total = 0; int value = 10; for_each(someList.begin(), someList.end(), [&, value](int x){ total += (x * value); }); cout << total << endl; //空指针nullptr char *pc = nullptr; char str[] = "hello world"; pc = str; cout << pc << endl; //通用智能指针 shared_ptr p_first(new double); shared_ptr p_copy = p_first; *p_copy = 21.1; cout << *p_first << endl; //c++11线程类std::thread,独占式互斥量std::mutex,允许超时的互斥量 int num = 1234; string str_td = "qwer"; thread t(my_thread1,num,str_td); thread t1(my_thread2); t.join(); t1.join(); return 0; }
相关文章推荐
- C++11的新特性简单汇总介绍 (一)
- C++11的新特性简单汇总介绍 (二)
- C++11新特性应用--介绍几个新增的便利算法(用于分区的几个算法)
- JDK7新特性简单翻译介绍
- c++11future简单使用及介绍
- JDK1.8新特性简单介绍
- C++11新特性应用--介绍几个新增的便利算法(用于分区的几个算法)
- JDK7新特性简单翻译介绍
- C++11新特性(三)Strongly-typed enums 强类型枚举简单使用
- C++11新特性应用--介绍几个新增的便利算法(用于分区的几个算法)
- 这是C++11新特性介绍的第六部分
- 一个实例介绍C++复制控制(有一个C++11新特性要注意)——复制构造函数、赋值操作符、析构函数
- 【笔记】RxJava2.0新特性简单介绍并实现RxBus
- C++11新特性(四)static_assert, share_ptr, unique_ptr简单使用
- PHP 7的一些引人注目的新特性简单介绍
- C++11最新标准的新特性介绍(1)
- 2015.12.22 类的三大特性 协议的简单介绍
- InnoDB 5.6 新特性之一:FullTEXT Indexes[1.简单介绍]
- C++11新特性应用--介绍几个新增的便利算法(stl中的heap使用,最大堆)
- C++11新特性应用--介绍几个新增的便利算法(关于最大值和最小值的算法)