Printing lists with commas C++

后端 未结 28 1755
时光取名叫无心
时光取名叫无心 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:52

    to avoid placing an if inside the loop, I use this:

    vector keywords = {1, 2, 3, 4, 5};
    
    if (!keywords.empty())
    {
        copy(keywords.begin(), std::prev(keywords.end()), 
             std::ostream_iterator (std::cout,", "));
        std::cout << keywords.back();
    }
    

    It depends on the vector type, int, but you can remove it with some helper.

提交回复
热议问题