【问题标题】:overload the "<<" operator for map重载地图的“<<”运算符
【发布时间】:2019-06-11 15:30:04
【问题描述】:

我被 C++ 中的一些代码困住了。我无法重载&lt;&lt; 运算符来打印我的地图。

我试图重载运算符,但它不起作用。我不能在 C++98 中使用基于范围的 for 循环。

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;
}

我现在的代码,没有我的重载类:

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;
}

有人知道如何打印我的地图吗?

【问题讨论】:

  • 为什么主题与实际问题不同?
  • “但它没有工作”。 如何它不起作用?你得到编译器错误了吗?运行时错误?它没有输出您期望的结果?
  • 你能解释一下你对operato&lt;&lt;的实现吗?
  • I can't use range-based for loops in C++98。基于范围的循环仅隐藏了迭代器的使用。您可以创建一个迭代器 map&lt;int, Person&gt;::const_iterator it = testMap.begin() 并将其与 testMap.end() 进行比较?
  • 您的operator&lt;&lt;() 甚至无法访问传递给它的地图。这意味着地图中的任何内容都不会被打印。内部循环也是无限的,因为如果 x &lt; 2 会继续运行,但不会更改 x 的值。

标签: c++ c++98


【解决方案1】:

首先,您上面的代码有一个错误。

for (int x = 0; x

应该是 x++ 而不是 i++

我相信您想重载运算符以打印地图的输出。为了让运算符

请参考以下代码:

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;
}

输出如下:

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

我希望这会有所帮助。

【讨论】:

  • 请注意,OP 明确声明 “我不能在 C++98 中使用基于范围的 for 循环”。也许您可以将答案中的代码编辑为this
  • 感谢 Bob 的指出。我已经编辑了代码并替换为迭代器。
【解决方案2】:
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

【讨论】:

    猜你喜欢
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    • 2018-08-17
    相关资源
    最近更新 更多