1

下面的代码在最后有很多输出字符串我尝试将其推回 avector 并将其附加到一个字符串,以便我可以返回它,但它只获取输出的最后一个字符串,我需要获取所有这些字符串.

我做错了什么,所以我可以推回所有的字符串

DCS_LOG_DEBUG("--------------- Validating .X/ ---------------")
std::string str = el[i].substr(3);
std::vector<std::string>st;
split(st,str,boost::is_any_of("/"));
boost::regex const string_matcher(splitMask[0]);
if(boost::regex_match(st[0],string_matcher))
{
    a = "Correct Security Instruction\n";
}
else
{
    a = "Incorrect Security Instruction\n"
}


boost::regex const string_matcher4(splitMask[4]);
if(boost::regex_match(st[4],string_matcher4))
{
    a = "Correct Autograpgh\n"
}
else
{
    a = "Incorrect Autograpgh\n"
}

boost::regex const string_matcher5(splitMask[5]);
if(boost::regex_match(st[5],string_matcher5))
{
    a = "Correct Free text\n";

}
else
{
    a = "Incorrect Free text\n"
}

std::vector<std::string>::iterator it;
std::string s = ("");
output.push_back(a);
i++;

for(it = output.begin(); it < output.end(); it++)
{
    s+= *it;
}

return s;
4

2 回答 2

1

多次分配 toa替换,而不是连接。您正在寻找的更可能是输出流(或输出迭代器)。

建议简化:

DCS_LOG_DEBUG("--------------- Validating .X/ ---------------")
std::string str = el[i].substr(3);
std::vector<std::string> st;
split(st,str,boost::is_any_of("/"));
boost::regex const string_matcher(splitMask[0]);
boost::regex const string_matcher4(splitMask[4]);
boost::regex const string_matcher5(splitMask[5]);

std::ostringstream oss;

oss << (boost::regex_match(st[0],string_matcher )? "correct":"incorrect") << " Security Instruction\n";
oss << (boost::regex_match(st[4],string_matcher4)? "correct":"incorrect") << " Autograpgh\n";
oss << (boost::regex_match(st[5],string_matcher5)? "correct":"incorrect") << " Free text\n";

return oss.str();

包括<sstream>_std::ostringstream

于 2012-02-03T16:07:30.750 回答
0

你确定你得到的结果不仅仅是第一个字符串吗?

也就是说,尚不完全清楚您要做什么,但假设在您发布的内容之上有一些循环代码,您的问题似乎在于 for 循环的定位。

         while( i < el.size() )  //Assuming something like this
         {                  
              ...

              else
              {
                  a = "Incorrect Free text\n"
              }               
              output.push_back(a);
              i++;
          }
          //move this stuff out of the loop so that it only runs after you have 
          // processed all the strings in el
          std::vector<std::string>::iterator it;
          std::string s = ("");
          for(it = output.begin(); it < output.end(); it++)
          {
             s+= *it;
          }
          return s;
       }
于 2012-02-03T16:15:51.267 回答