Create a prefix sequence on one line

For initialized variables unsigned a, unsigned bs b > aand std::vector<std::string> stringssize b-a. How can I fill with stringselements, for example. "x3" "x4" "x5" "x6"(in case a=3and b=7) for arbitrary aand bwith one C ++ command (which means one semicolon :))?

+3
source share
6 answers

Not too complicated ...

std::transform(
    boost::make_counting_iterator(a), boost::make_counting_iterator(b), 
    strings.begin(), 
    "x" + boost::lambda::bind(boost::lexical_cast<std::string, unsigned int>, 
                              boost::lambda::_1));
+3
source

What a problem!

while (a < b) strings.push_back('x' + boost::lexical_cast<std::string>(a++));

Also, compare verbosity with Manuel's answer :)

+8
source

#define IM_NOT_A_SEMICOLON_REALLY ;, .

+6

UncleBen, STL

while( a < b ) vStrings.push_back( 'x' + ( (std::stringstream&)( std::stringstream() << a++ ) ).str() );
+3
BOOST_FOREACH(std::string & str, strings) str = "x" + boost::lexical_cast<std::string>(a++);
+1

Violation of semicolon operators that are obviously not a semicolon:

while (a<b) {
   char s[12],
        t = (snprintf(s, 11, "x%d", a++), strings.push_back(s), 0);
}
+1
source

Source: https://habr.com/ru/post/1733055/


All Articles