c++ 常用函数 集锦 整理中
c++ 常用函数集锦
目录
c++ 常用函数集锦
1、string和wstring之间转换
2、经纬度转 xyz 值 互转
3 、获取 根目录下的文件地址
1、string和wstring之间转换
std::string convertWStringToString(std::wstring wstr)
{
std::string str;
if (!wstr.empty())
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
str = converter.to_bytes(wstr);
}
return str;
}
std::wstring convertStringToWString(std::string wstr)
{
std::wstring str;
if (!wstr.empty())
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
str = converter.from_bytes(wstr);
}
return str;
}
2、经纬度转 xyz 值 互转
//经纬度 转 xyz
XYZ LBtoXYZ(double lontitude, double latitude, double height)
{
double f = 1 / 298.25722293287; //地球扁率
double Re = 6378137.0; //长半轴,单位:m
double Rp = 6356752.3142; //短半轴,单位:m
double PI = 3.14159265;
double R = 0; //当量半径
double Rdenominator = 0;
Rdenominator = (1 - f)*(1 - f)*sin(latitude)*sin(latitude) + cos(latitude)*cos(latitude);
//if(Rdenominator<=0)
// return ;
R = Re / (sqrt(Rdenominator)); //
XYZ res = { 0 };
res.X = (R + height)*cos(latitude)*cos(lontitude);
res.Y = (R + height)*cos(latitude)*sin(lontitude);
res.Z = (R*(1 - f)*(1 - f) + height)*sin(latitude);
return res;
}
// xyz 转 经纬度
BLH XYZtoLB(double X, double Y, double Z)
{
double a, f, e2, B = 0.0, N = 0.0, H = 0.0, R0, R1, deltaH, deltaB;
a = 6378137.0, f = (1.0 / 298.257223563), e2 = f*(2 - f);
BLH res = { 0 };
R0 = sqrt(pow(X, 2) + pow(Y, 2));
R1 = sqrt(pow(X, 2) + pow(Y, 2) + pow(Z, 2));
//经度直接求解
res.L = atan2(Y, X);
//迭代求大地维度和大地高
N = a;
H = R1 - a;
B = atan2(Z * (N + H), R0 * (N * (1 - e2) + H));
do
{
deltaH = N;//判断收敛所用
deltaB = B;
N = a / sqrt(1 - e2 * pow(sin(B), 2));
H = R0 / cos(B) - N;
B = atan2(Z * (N + H), R0 * (N * (1 - e2) + H));
} while (fabs(deltaH - H) > 1.0e-3 && fabs(deltaB - B) > 1.0e-9);
res.B = B;
res.H = H;
return res;
}
3 、获取 根目录下的文件地址
QString xxxxx::getConfigPath(QString fileName)
{
QString appDirPath = QCoreApplication::applicationDirPath();
return appDirPath + "/config/" + fileName;
}