1

以下のコードには、最後にたくさんの出力文字列があります。ベクトルに押し戻して文字列に追加しようとすると、文字列を返すことができますが、出力された最後の文字列のみを取得します。すべてを取得する必要があります。 。

すべての文字列を押し戻すことができるように、私は何を間違っているのですか?

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

に複数回割り当てると、連結ではなく、置き換えaられます。あなたが探しているのは、おそらく出力ストリーミング(または出力イテレータ)です。

単純化することを提案する:

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 に答える