2012-02-03 13 views
1

以下のコードは、最後に文字列を出力しています。私は、ベクトルを返して文字列に追加しようとしますが、返すことはできますが、出力私はそれらのすべてを取得する必要があります。ベクトルに挿入するC++

私は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]); 
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

あなたのコードには1つの 'push_back'しかありません。なぜあなたのベクターに複数の文字列が含まれていると思われますか? – Mat

+0

多分、文字列の後ろに@Matを付け加えた場合 – CodersSC

+0

あなたは文字列を追加していません。 – Mat

答えて

1

連結、ではなく置き換えられます押し返すことができるように、私は間違って何をdoignています。あなたが探しているのは、出力のストリーム(または出力イテレータ)です。

は簡素化することを提案する:

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(); 

std::ostringstream

+0

私の簡潔さと明快さのための投票:) – qdot

0

ため<sstream>を含め、あなたが得ている結果は、単に最初の文字列ではないことを確認していますか?

あなたが何をしようとしているのかは完全にはっきりしていませんが、投稿したコードの上にループするコードがあると仮定すると、問題は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; 
     } 
関連する問題