C++ 现代编程风格速查表
来源:互联网 发布:淘宝店不刷信誉可以吗 编辑:程序博客网 时间:2024/05/01 02:15
栈上数组
// naive:int arr[10];memset(arr, 0, sizeof(a));
// modern:// #include <array>std::array<int, 10> arr;arr.fill(0);
堆上数组
// naive:int *arr = new int[10];memset(arr, 0, 10 * sizeof(int));
// modern:// #include <vector>std::vector<int> arr(10);
字符串
// naive:char str[] = "Hello, C++!";
// modern:// #include <string>std::string str = "Hello, C++!";// or auto str = "Hello, C++!"s;
指向栈的指针
// naive:int ival;int *p = &ival;
// modern:int ival;int &rval = ival;
堆上对象
// naive:MyClass *obj = new MyClass;obj->someMethod(args);
// modern:auto obj = std::make_unique<MyClass>();// or auto obj = std::make_shared<MyClass>();obj->someMethod(args);
函数指针
// naive:typedef int (*func_t)(int, int);func_t func = some_func;
// modern:// #include <function>std::function<int(int, int)> func = some_func;
函数对象
// naive:struct func_t{ int operator() (int arg1, int arg2) { // statements }};func_t func;
// modern:// #include <function>std::function<int(int, int)> func = [](int arg1, int arg2) { // statements };
宏定义常量
// naive:#define PI 3.14
// modern:const double PI = 3.14;
宏定义类型
// naive:#define uint unsigned int
// modern:typedef unsigned int uint;// or using uint = unsigned int;
宏定义函数
// naive:#define max(a, b) ((a)>(b)?(a):(b))
// modern:template<T>inline T max(T a, T b){ return a>b? a: b;}
原生类型转换
// naive:int ival;double dval = (double)ival;char *pc1 = ...;const char *cpc = (char *)pc1;char *pc2 = (const char *)cpc;Derived *pd1 = ...;Base *pb = (Base *)pd1;Derived *pd2 = (Derived *)pb;void *pv1 = ...;long lval = (long)pv1;void *pv2 = (void *)lval;
// modern:int ival;double dval = static_cast<double>(ival);char *pc1 = ...;const char *cpc = const_cast<const char *>(pc1);char *pc2 = const_cast<char *>(cpc);Derived *pd1 = ...;Base *pb = dynamic_cast<Base *>(pd1);Derived *pd2 = dynamic_cast<Derived *>(pb);void *pv1 = ...;long lval = reinpreter_cast<long>(pv1);void *pv2 = reinpreter_cast<void *>(lval);
线程
// naive:// #include <pthread.h>pthread_t tid;pthread_create(&tid, func, arg);
// modern:// #include <thread>std::thread thr(func, arg);
未完待续…
0 0
- C++ 现代编程风格速查表
- C语言编程风格
- C编程风格
- C语言编程风格
- c语言编程风格
- c/c++编程风格
- C/C++编程风格
- C++/C编程风格规范
- C现代实现方法编程题
- C现代实现方法编程题2
- 《C语言现代方法》第一章编程题
- C 语言现代编程一 HelloWorld
- 现代C++风格
- 摸索现代C++风格
- 设计风格-现代简约
- c语言的编程风格
- c语言的编程风格
- c语言的编程风格
- IO流4(IO中的其他流、编码)
- Android中的颜色
- Android Instrumention.sendPointerSync发送Event 失败源码分析
- LintCode Jump Game 跳跃游戏
- HDU 3567 - Eight II
- C++ 现代编程风格速查表
- 前端优化整理
- 数据的管理
- 插入排序
- cygwin显示设置---调用windows命令显示不正常
- Linux Java 学习记_1
- 【NYOJ】[41]三个数从小到大排序
- 使用spring mvc遇到的问题
- python学习之5 多线程threading