#include<iostream>#include<iomanip>// without std::fixed, output will be scientific notation(科学计数法) format.std::cout<<std::fixed<<std::setprecision(5)<<number<<std::endl;
#include<cstdio>#include<cstdarg>voidfoo(constchar*format,...){// do this iterate to get lengthva_listarg_ptr;va_start(arg_ptr,format);intsize=vsnprintf(NULL,0,format,arg_ptr);va_end(arg_ptr);// 注意: 在用`vsnprintf` 获取长度后,先`va_end(arg_ptr)`, 然后需要再次使用一轮`va_...`函数来获取可变参数, 否则会产生乱码.// Restart va_list to reuse the argumentsva_start(arg_ptr,format);char*buf=newchar[size+1];vsnprintf(buf,size+1,format,arg_ptr);va_end(arg_ptr);// Don't forget to free the memory!delete[]buf;}
#include<iostream>structdebug{std::ostream&o;debug():o(std::cout){}template<classT>debug&operator,(Targ){std::cout<<arg;return*this;}~debug(){std::cout<<'\n';}};// usage:debug(),"hello"," world",Variable1,"spliter",variable2;// Notice: sepcial variable need to overload `operator <<` for `std::ostream`// for exampletemplate<classT>inlinestd::ostream&operator<<(std::ostream&out,std::optional<T>v){if(v.has_value()){out<<v.value();}else{out<<"[None]";}returnout;}