2023年9月10日,周日上午
目录
如果需要定义多行代码的宏函数,可以在宏函数中使用反斜杠\
来表示该行代码还未结束,继续在下一行继续编写代码。
- #define MY_MACRO(x) \
- cout << "Value of x is: " << x << endl; \
- x *= 2; \
- cout << "New value of x is: " << x << endl;
- #include
-
- #define PRINT(text) std::cout<
-
- int main(){
- PRINT("巨龙之路")
- }
用宏函数实现只需一行代码调用,就能统计一个函数的执行时间。
- #include
- #include
-
- #define TIME(func) \
- { \
- auto start_time= std::chrono::high_resolution_clock::now(); \
- func; \
- auto end_time = std::chrono::high_resolution_clock::now(); \
- auto duration = std::chrono::duration_cast
(end_time - start_time).count(); \ - std::cout<<"耗时"<
"毫秒" < - }
-
- void testFunction(){
- for(int i=0;i<10000000;i++);
- }
-
- int main(){
- TIME(testFunction())
- }