Search code examples
c++vectorpush-back

inserting on the vector c++


The code below has alot of outputting strings at the end i try to push the back onto avector and the append it to a string so then i can return it, but it only gets the last string which is outputted i need to get all of them.

What am i doign wrong so that i can push back all the strings

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;

Solution

  • Assigning more than once to a will replace, not concatenate. What you are looking for, is more likely output streaming (or output iterators).

    Propose to simplify:

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

    Include <sstream> for std::ostringstream