.

  • ​​SFINAE​​
  • ​​c++ Log封装​​
  • ​​\_\_FILE_\_ \_\_LINE_\_​​
  • ​​可以输出变量名的函数​​
  • ​​一个方便debug的多行宏定义函数​​
  • ​​封装spdlog​​
  • ​​log效果​​
  • ​​分数​​
  • ​​时间 C++11 std::chrono库详解​​
  • ​​【模板】输出函数:数量类型均可变​​
  • ​​可以使用constexpr写成一个函数​​
  • ​​使用getline实现split​​
  • ​​容器自增初始化​​

SFINAE

SFINAE 是 Substitution Failure Is Not An Error ( 替换失败不是一个错误 ) 的缩写。 在 C++11 之前,这并不是一个正式的 C++ 规范术语。而是由 David Vandevoorde 在其书籍 C++ Templates: The Complete Guide 首次命名的概念。

​https://modern-cpp.readthedocs.io/zh_CN/latest/sfinae.html​

c++ Log封装

__FILE__ __LINE__

__FILE__:插入正在编译文件的路径及文件名
__LINE__:插入正在编译文件的行号
__DATE__:插入编译时的日期 如“Jun 17 2017”
__TIME__:插入编译时的时间 如”10:00:00“
__STDC__:插入判断是不是标准C程序,当要求程序严格遵循ANSI C标准时该标识被赋值为1
__FUNCTION__:插入当前函数名
__cplusplus:当编写C++程序时该标识符被定义。
#include <iostream>
using namespace std;

#define debug(x) cout<<#x<<": "<<(x)<<endl;

int main()
{

debug(__FILE__)
debug(__LINE__)
debug(__DATE__)
debug(__TIME__)
//debug(__STDC__)
return 0;

}
__FILE__: E:\vsTest\src\vsTest.cpp
__LINE__: 10
__DATE__: Oct 11 2021
__TIME__: 19:55:59

可以输出变量名的函数

#define debug(x) cout<<#x<<": "<<(x)<<endl;

一个方便debug的多行宏定义函数

#define logDebug(x) {               \
cout << "[" << __FILE__ << "]" \
<< "[line:" << __LINE__ << "]" \
<< "[" << __DATE__ << "]" \
<< "[" << __TIME__ << "] " \
<< #x <<": "<< (x) << endl; \
}

使用

#include <iostream>
using namespace std;

#define debug(x) cout<<#x<<": "<<(x)<<endl;

#define logDebug(x) { \
cout << "[" << __FILE__ << "]" \
<< "[line:" << __LINE__ << "]" \
<< "[" << __DATE__ << "]" \
<< "[" << __TIME__ << "] " \
<< #x <<": "<< (x) << endl; \
}

int main()
{
int a = 10;
for (;;)logDebug(a)
return 0;

}

【C++】语法糖_#include

封装spdlog

log效果

【C++】语法糖_#define_02


分数


时间 C++11 std::chrono库详解

【模板】输出函数:数量类型均可变

void println() { cout << endl; }
template<typename T, typename ... Types>
void println(const T& firstArg, const Types&... args) {

//cout << "size of args: " << sizeof...(args) << endl;
cout << firstArg << " ";
println(args...);
}

可以使用constexpr写成一个函数

template<typename T, typename ...U>
void println(T v, U ...u) {
if constexpr (sizeof...(U) == 0) {
std::cout << v << std::endl;
}
else {
std::cout << v << ", ";
println(u...);
}
}

​​参见类模板部分​​

使用getline实现split

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

#define debug(x) do{cout<<#x<<": "<<(x)<<endl;}while(0);

int main() {

stringstream ss;
string s = "";
getline(cin,s);
ss << s;
std::string str;
while (std::getline(ss, str, ',')) {
cout << str << endl;
}
return 0;
}

【C++】语法糖_#define_03


容器自增初始化

#include <numeric>

vector<int> arr(10,0);
std::iota(arr.begin(),arr.end(),1);
从1开始
arr就会变成{1,2,3...,}