【发布时间】:2015-01-28 05:29:01
【问题描述】:
我正在从 MySQL 接收数据并尝试使用它。收到的数据在m_caracs 中,然后我尝试在其他float 中剪切此流的每个子部分。
让我们看看代码:
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <vector>
#include <string>
std::string m_sten;
std::string m_feal;
std::string m_felt;
std::string m_inte;
std::string m_sag;
std::string m_ende;
std::string m_asko;
std::string m_vit;
void test(bool mon)
{
std::string m_caracs = "f21.0i51.24v58.65c47.3s5.54d57.68e54.23h24.42";
if (mon == 0)
{
std::vector<std::string> charmps;
boost::split(charmps, m_caracs, boost::is_any_of("fivcsdeh"));
m_sten = boost::lexical_cast<float>(charmps[1]);
m_feal = boost::lexical_cast<float>(charmps[2]);
m_felt = boost::lexical_cast<float>(charmps[3]);
m_inte = boost::lexical_cast<float>(charmps[4]);
m_sag = boost::lexical_cast<float>(charmps[5]);
m_ende = boost::lexical_cast<float>(charmps[6]);
m_asko = boost::lexical_cast<float>(charmps[7]);
m_vit = boost::lexical_cast<float>(charmps[8]);
std::cout << m_caracs << std::endl;
}
else
{
std::cout << m_caracs << std::endl;
m_caracs = "f" + boost::lexical_cast<std::string>(m_sten) +
"i" + boost::lexical_cast<std::string>(m_feal) +
"v" + boost::lexical_cast<std::string>(m_felt) +
"c" + boost::lexical_cast<std::string>(m_inte) +
"s" + boost::lexical_cast<std::string>(m_sag) +
"d" + boost::lexical_cast<std::string>(m_ende) +
"e" + boost::lexical_cast<std::string>(m_asko) +
"h" + boost::lexical_cast<std::string>(m_vit);
std::cout << m_caracs << std::endl;
}
}
int main()
{
test(1);
test(0);
}
您可以看到f21.0i51.24v58.65c47.3s5.54d57.68e54.23h24.42 变成了f21.0i51.24v58.65c47.3s5.54d57.68e54.23h24.42。这正是我想要的。问题是,我有这个:
我不知道它来自哪里。唯一的变化是m_caracs 是从数据库接收的流。这是转换问题吗?
【问题讨论】:
-
我认为你的全局变量
m_*应该是floats(并且你想在test(1)之前调用test(0)) -
哎呀,忘记编辑了,对不起。我正在阅读你的代码。谢谢你,嘿嘿。
标签: c++ boost floating-point lexical-cast