C++ cout auto separator

C++

C++ Problem Overview


I am wondering if there is way that std::cout automatically will insert some predefined value between printed sequences.

For example:

std::cout << 2 << 3 << 33 << 45 << std::endl;

outputs

233345

and I would like it to output

2 3 33 45

and I know, that it's easy to:

std::cout << 2 << " " << 3 <<  " " << 33 <<  " " << 45 << std::endl;

But I'am wondering if there is a way to automate this, such as:

std::cout << set_some_separator(" ") << 2 << 3 << 33 << 45 << std::endl;

Anyone aware of something like this being possible?

C++ Solutions


Solution 1 - C++

Well, I got beaten to it. I'll post this anyway.

Edit : well, after reading Nim's answer, mine does achieve the exact syntax OP wished for.

#include <iostream>
#include <algorithm>

struct with_separator {
    with_separator(std::string sep)
    : sep(std::move(sep)) {}

    std::string sep;
};

struct separated_stream {
    separated_stream(std::ostream &stream, std::string sep)
    : _stream(stream), _sep(std::move(sep)), _first(true) {}

    template <class Rhs>
    separated_stream &operator << (Rhs &&rhs) {
        if(_first)
            _first = false;
        else
            _stream << _sep;

        _stream << std::forward<Rhs>(rhs);
        return *this;
    }
    
    separated_stream &operator << (std::ostream &(*manip)(std::ostream&)) {
        manip(_stream);
        return *this;
    }

    private:
    std::ostream &_stream;
    std::string _sep;
    bool _first;
};

separated_stream operator << (std::ostream &stream, with_separator wsep) {
    return separated_stream(stream, std::move(wsep.sep));
}

int main()
{
    std::cout << with_separator(", ") << 1 << 2 << 3 << std::endl;
}

Output :

1, 2, 3

Solution 2 - C++

Simple answer is No, however, you can roll your own...

#include <iostream>
#include <sstream>

using namespace std;

struct set_some_separator{
    set_some_separator(const char* sep) : _sep(sep)
    { };
    
    template <typename T>
    set_some_separator& operator<<(const T& v)
    {
        _str << v << _sep;
        return *this;
    }

    friend
    ostream& operator<<(ostream& os, const set_some_separator& s)
    { return os << s._str.str(); }
    
    const char* _sep;
    ostringstream _str;
};

int main()
{
    cout << (set_some_separator(" ") << 2 << 3 << 33 << 45) << endl;
}

Okay the format of the cout is slightly different, hey-ho...

Solution 3 - C++

Not quite the same thing, but:

#include <array>
#include <iostream>
#include <iterator>

int main() {
    std::array<int, 3> data = { 1, 2, 3 };
    std::ostream_iterator<int> out(std::cout, " ");
    std::copy(data.begin(), data.end(), out);
    std::cout << '\n';
    return 0;
}

Solution 4 - C++

How about using the ostream_iterator

int main()
{
     std::vector<int> data {2,3,33,45};
     std::copy(std::begin(data), std::end(data),
               std::ostream_iterator(std::cout, " "));
     std::cout << "\n";
}

Solution 5 - C++

A C++17 fold expression with the comma operator can create a nice one-liner:

[](auto &&...xs){ ((std::cout << xs << ',') , ...); }(2,3,33,45);

Solution 6 - C++

Simple solution, maybe you can tweak it to use <<

template<typename T> 
void myout(T value) 
{ 
   std::cout << value << std::endl; 
} 

template<typename First, typename ... Rest> 
void myout(First first, Rest ... rest) 
{ 
   std::cout << first << " ";
   myout(rest...); 
}


myout('a',"Hello",1,2,3,22/7.0);

Solution 7 - C++

If you're just looking for a way to print a vector with its elements properly delimited (and without an extra delimiter at the end), try this:

#include <iostream>
#include <vector>

int main()
{
  std::vector<int> v{1, 2, 3};

  auto it = v.begin();
  if (it != v.end())
  {
    std::cout << *it;
    ++it;
  }

  for (; it != v.end(); ++it)
  {
    std::cout << ", " << *it;
  }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFerenc DeakView Question on Stackoverflow
Solution 1 - C++QuentinView Answer on Stackoverflow
Solution 2 - C++NimView Answer on Stackoverflow
Solution 3 - C++Pete BeckerView Answer on Stackoverflow
Solution 4 - C++Martin YorkView Answer on Stackoverflow
Solution 5 - C++user2023370View Answer on Stackoverflow
Solution 6 - C++tejasView Answer on Stackoverflow
Solution 7 - C++Radon RosboroughView Answer on Stackoverflow