代码改变世界

【c++】【常用函数】

2018-12-20 19:27  ZealouSnesS  阅读(223)  评论(0编辑  收藏  举报

分割字符串:https://www.cnblogs.com/zealousness/p/9971709.html

 

字符串比较:https://www.cnblogs.com/zealousness/p/10140189.html

 

求绝对值:

#include<cmath>
std::cout<<abs(-2)<<std::endl;

 

开平方:

#include<cmath>
std::cout<<sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))<<std::endl;

 

异常处理:

try{
    ...
}catch(std::exception e){
    ...
}

 

double 转字符串

// The C way:
char buffer[32];
snprintf(buffer, sizeof(buffer), "%g", myDoubleVar);

// The C++03 way:
std::ostringstream sstream;
sstream << myDoubleVar;
std::string varAsString = sstream.str();

// The C++11 way:
std::string varAsString = std::to_string(myDoubleVar);

// The boost way:
std::string varAsString = boost::lexical_cast<std::string>(myDoubleVar);