Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overload << operator to change " " to "\n"

I am trying to overload

<<

operator. For instance

cout << a << " " << b << " "; // I am not allowed to change this line

is given I have to print it in format

<literal_valueof_a><"\n>
<literal_valueof_b><"\n">
<"\n">

I tried to overload << operator giving string as argument but it is not working. So I guess literal

" "

is not a string. If it is not then what is it. And how to overload it? Kindly help;

Full code

//Begin Program
// Begin -> Non - Editable     
    #include <iostream>
    #include <string>
    using namespace std;

// End -> Non -Editable
//---------------------------------------------------------------------
// Begin -> Editable       (I have written )
    ostream& operator << (ostream& os, const string& str) {
        string s  = " ";
        if(str  ==  " ") {
            os << '\n';
        }
        else {
            for(int i = 0; i < str.length(); ++i)
                os << str[i];
        }
        return os;
    }

// End -> Editable
//--------------------------------------------------------------------------
// Begin -> No-Editable     
int main() {
        int a, b;
        double s, t;
        string mr, ms;
        cin >> a >> b >> s >> t ;
        cin >> mr >> ms ;
        cout << a << " " << b << " " ;
        cout << s << " " << t << " " ;
        cout << mr << " " << ms ;

        return 0;
    }
// End -> Non-Editable
//End Program

Inputs and outputs Input

 30 20 5.6 2.3 hello world 

Output

30
20
5.6
2.3
hello
world
like image 561
Brij Raj Kishore Avatar asked Jan 01 '23 18:01

Brij Raj Kishore


1 Answers

" " is a string-literal of length one, and thus has type const char[2]. std::string is not related.

Theoretically, you could thus overload it as:

auto& operator<<(std::ostream& os, const char (&s)[2]) {
    return os << (*s == ' ' && !s[1] ? +"\n" : +s);
}

While that trumps all the other overloads, now things get really hairy. The problem is that some_ostream << " " is likely not uncommon, even in templates, and now no longer resolves to calling the standard function. Those templates now have a different definition in the affected translation-units than in non-affected ones, thus violating the one-definition-rule.

What you should do, is not try to apply a global solution to a very local problem:

Preferably, modify your code currently streaming the space-character.
Alternatively, write your own stream-buffer which translates it as you wish, into newline.

like image 99
Deduplicator Avatar answered Jan 05 '23 16:01

Deduplicator