重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

stringstream字符串的格式化数据提取

1 格式化字符串,提取其中的各种数据类型
void test1()
{
std::string strCompleteMsg = "0R1,Dn=236D,Dm=283D,Dx=031D,Sn=0.0M,Sm=1.0M,Sx=2.2M/r/n";
for (int i = 0; i < strCompleteMsg.length(); i++)
{
if ((',' == strCompleteMsg[i]) || ('D' == strCompleteMsg[i]) || ('=' == strCompleteMsg[i]) || ('M' == strCompleteMsg[i]) || ('S' == strCompleteMsg[i]))
{
strCompleteMsg[i] = ' ';
}
}
int nDn = 0, nDm = 0, nDx = 0;
float fSn = 0, fSm = 0, fSx = 0;
std::string str1;
char str2, str3, str4, str5, str6;

创新互联建站成立与2013年,先为右江等服务建站,右江等地企业,进行企业商务咨询服务。为右江企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

std::stringstream ss;
ss << strCompleteMsg;
ss >> str1;
ss >> str1;
ss >> nDn;
ss >> str1;
ss >> nDm;
ss >> str1;
ss >> nDx;
ss >> str1;
ss >> fSn;
ss >> str1;
ss >> fSm;
ss >> str1;
ss >> fSx;

}

void test2()
{
std::string strCompleteMsg = "DSC,6,2,0.33,0.01,0.08,0.28,0.2887,0.234";
for (int i = 0; i < strCompleteMsg.length(); i++)
{
if (',' == strCompleteMsg[i])
{
strCompleteMsg[i] = ' ';
}
}
std::stringstream ss;
ss << strCompleteMsg;

std::string strDsc;

int nSurfaceStatus = 0;
int nWarning = 0;
float fLevelOfGrip = 0, fAmountOfWater = 0, fAmountOfIce = 0, fAmountOfSnow = 0;

ss >> strDsc;
ss >> nSurfaceStatus;
ss >> nWarning;
ss >> fLevelOfGrip;
ss >> fAmountOfWater;
ss >> fAmountOfIce;
ss >> fAmountOfSnow;

}

2 整型与字符串之间的转换
#include
#include

void Int2Str(int nNumber, std::string &strNumber)
{
stringstream stream;
stream << nNumber;
strNumber = stream.str();
}

void Str2Int(const std::string &strNumber, int &nNumber)
{
stringstream stream(strNumber);
stream >> nNumber;
}


文章名称:stringstream字符串的格式化数据提取
文章链接:http://cqcxhl.com/article/jechip.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP