Legasov
Legasov

Reputation: 64

overload the "<<" operator for map

I'm stuck at some code in C++. I have trouble to overload the << operator to print my map.

i tried to overload the operator but it didn't work. I can't use range-based for loops in C++98.

ostream & operator <<(std::ostream &os, 
                           const std::map<int, Person> &m)
{
    for (int i = 0; i < 3; i++)
    {
        os << i << ":";
        for (int x = 0; x < 2; i++) os << x << ' ';
        os << std::endl;
    }

    return os;
}

My code now, without my overloaded class:

class Person{
    public: 
    int kontostand; 
    string email;
    int alter;

    Person(int kontostand_, string email_, int alter_)
    {
        kontostand=kontostand_;
        email = email_;
        alter = alter_;
    }
};

int main()
{
    map<int, Person> testmap; 
    Person Kunde(100, "test", 21);
    Person Kunde2(200, "test", 22);
    Person Kunde3(300, "test", 23);

    testmap.insert(pair<int, Person>(1, Kunde));
    testmap.insert(pair<int, Person>(2, Kunde2));
    testmap.insert(pair<int, Person>(3, Kunde3));

    cout << testmap; 

    return 0;
}

Does anyone have an idea how can I print my map?

Upvotes: 0

Views: 179

Answers (2)

ritesh sangani
ritesh sangani

Reputation: 340

First of all there was a mistake in your code above.

for (int x = 0; x < 2; i++) os << x << ' ';

it should be x++ instead of i++

I believe you wanted to overload the operator in order to print the output of the map. In order for the operator << to accept a Complex type you have to overload the operator inside the Complex type also.

Please refer to the code below:

class Person {
public:
    int kontostand;
    string email;
    int alter;

    Person(int kontostand_, string email_, int alter_)
    {
        kontostand = kontostand_;
        email = email_;
        alter = alter_;
    }
    friend ostream& operator<<(std::ostream& os, const Person &p) {
        os << p.kontostand << "," << p.alter << "," << p.email;
        return os;
    }
};

ostream & operator <<(std::ostream &os,const std::map<int, Person> &m)
{
    for (map<int,Person>::const_iterator i = m.begin(); i != m.end(); ++i)
    {
        os << i->first << ":" << i->second << endl;
    }

    //for (int i = 0; i < 3; i++)
    //{
    //  os << i << ":";
    //  for (int x = 0; x < 2; x++) os << x << ' ';
    //  os << std::endl;
    //}

    return os;
}


int main()
{
    map<int, Person> testmap;
    Person Kunde(100, "test", 21);
    Person Kunde2(200, "test", 22);
    Person Kunde3(300, "test", 23);

    testmap.insert(pair<int, Person>(1, Kunde));
    testmap.insert(pair<int, Person>(2, Kunde2));
    testmap.insert(pair<int, Person>(3, Kunde3));

    cout << testmap;
    cin.get();
    return 0;
}

And the output is as under :

1:100,21,test
2:200,22,test
3:300,23,test

I hope this will help.

Upvotes: 2

Marek R
Marek R

Reputation: 37917

template<typename Key, typename Value>
std::ostream& operator<<(std::ostream& out, const std::pair<Key, Value>& pair)
{
    return out << pair.first << ':' << pair.second;
}

template<typename Key, typename Value>
std::ostream& operator<<(std::ostream& out, const std::map<Key, Value>& c)
{
    typedef typename std::map<Key, Value>::const_iterator Iter;
    if (c.size() == 0) {
        return out << "<empty>";
    }
    Iter it = c.begin();
    out << *it;
    for(++it; it != c.end(); ++it) {
        out << ',' << *it;
    }
    return out;
}

Live example

Upvotes: 2

Related Questions