温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-08-02 07:31:33 来源:网络 阅读:848 作者:fengyuzaitu 栏目:编程语言

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;

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 <sstream>
#include <string>

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;
}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI