Printing lists with commas C++

后端 未结 28 2354
时光取名叫无心
时光取名叫无心 2020-11-22 03:33

I know how to do this in other languages, but not C++, which I am forced to use here.

I have a Set of Strings that I\'m printing to out in a list, and they need a co

28条回答
  •  情书的邮戳
    2020-11-22 03:58

    You can use a do loop, rewrite the loop condition for the first iteration, and use the short-circuit && operator and the fact that a valid stream is true.

    auto iter = keywords.begin();
    if ( ! keywords.empty() ) do {
        out << * iter;
    } while ( ++ iter != keywords.end() && out << ", " );
    out << endl;
    

提交回复
热议问题