不要打印尾随分隔符stream_iterator C ++

时间:2012-06-17 18:50:43

标签: c++ iterator c++11 ostream

在您看来,在C ++ 11中使用std::cout打印到std::ostream_iterator并避免打印尾随分隔符时,最优雅的方法是什么?

我正在打印的对象具有双向迭代器,但不是随机访问迭代器。

std::list<double> x{1,2,3,4,5,6};
std::copy(x.begin(), std::prev(x.end()),
                std::ostream_iterator<int>(std::cout, ",") );
if ( x.size() != 0 )
  std::cout << *(--x.end()) << std::endl;

3 个答案:

答案 0 :(得分:10)

这是我的最爱之一,但它不使用std::ostream_iterator

#include <iterator>
#include <string>
#include <iosfwd>

template <class C>
auto
print(std::ostream& os, const C& c,
      const std::string& delim = std::string(", "),
      const std::string& open_brace = std::string("{"),
      const std::string& close_brace = std::string("}")
     ) -> decltype(std::begin(c), std::end(c), os)
{
    os << open_brace;
    auto i = std::begin(c);
    auto e = std::end(c);
    if (i != e)
    {
        os << *i;
        for (++i; i != e; ++i)
            os << delim << *i;
    }
    os << close_brace;
    return os;
}

#include <list>
#include <iostream>

int main()
{
    std::list<double> x{1,2,3,4,5,6};
    print(std::cout, x) << '\n';
}

{1, 2, 3, 4, 5, 6}

<强>更新

奥利弗怂恿我无法抗拒的挑战。 : - )

#include <iterator>
#include <string>
#include <iosfwd>

namespace my {

template <class C>
auto
print(std::ostream& os, const C& c,
      const std::string& delim = std::string(", "),
      const std::string& open_brace = std::string("{"),
      const std::string& close_brace = std::string("}")
     ) -> decltype(std::begin(c), std::end(c), os);

template <class C,
           typename std::enable_if
                    <
                       !std::is_same<C, std::string>::value,
                    bool>::type = false
         >
inline
auto
operator<< (std::ostream& os, const C& c) -> decltype(print(os, c))
{
    return print(os, c);
}

template <class C>
auto
print(std::ostream& os, const C& c,
      const std::string& delim,
      const std::string& open_brace,
      const std::string& close_brace
     ) -> decltype(std::begin(c), std::end(c), os)
{
    os << open_brace;
    auto i = std::begin(c);
    auto e = std::end(c);
    if (i != e)
    {
        os << *i;
        for (++i; i != e; ++i)
            os << delim << *i;
    }
    os << close_brace;
    return os;
}

}

#include <list>
#include <forward_list>
#include <iostream>

int main()
{
    std::forward_list<std::list<double>> x{{}, {3, 2, 1}, {1,2,3,4,5,6}};
    my::print(std::cout, x) << '\n';
}

{{}, {3, 2, 1}, {1, 2, 3, 4, 5, 6}}

这不完美,但很有趣。 :-)可能有更好的方法可以更忠实地传播自定义分隔符和括号。

答案 1 :(得分:4)

只需将光标向后移动:

std::cout << "\b";

然后覆盖分隔符。

答案 2 :(得分:1)

C ++ 14 - 遗憾的是需要输入/输出流:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <list>

int main()
{
    std::list<double> x{1,2,3,4,5,6};
    std::stringstream sstr;
    std::transform(x.begin(), x.end(), std::ostream_iterator<std::string>(sstr), [&](auto c){
        return (sstr.rdbuf()->in_avail() ? "," : "") + std::to_string(c);
    });
    std::cout << sstr.str() << std::endl;
}

live

从C ++ 17开始,ostream_joiner会解决这个问题:

#include <algorithm>
#include <experimental/iterator>
#include <iostream>
#include <iterator>

int main()
{
    int i[] = {1, 2, 3, 4, 5};
    std::copy(std::begin(i),
              std::end(i),
              std::experimental::make_ostream_joiner(std::cout, ", "));
}

live