使用迭代器打印multimap

Using iterator to print a multimap

本文关键字:multimap 打印 迭代器      更新时间:2023-10-16

假设我有一些

    typedef std::pair<userObject*,char> Bar;
    std::multimap<int, Bar > Foo;
    std::multimap<int, Bar >::iterator it = foo.begin();

我已经在map中插入了一些成员,并且想使用迭代器遍历它的内容。但是,尝试第二行代码会产生no viable conversion from....

我知道错误是由Bar是一对引起的。我用ints写了一个简单的类来模拟行为,一切都如预期的那样工作。我是否需要提供一些模板类T?定义行为。

这里是源代码的链接,可以查看代码。

错误信息是

    no viable conversion from '__map_iterator<__tree_iterator<__value_type<int *, [...]>, std::__1::__tree_node<std::__1::__value_type<int *, std::__1::pair<Vertex *, char> >, void *> *, [...]>>' to '__map_iterator<__tree_iterator<__value_type<int, [...]>, std::__1::__tree_node<std::__1::__value_type<int, std::__1::pair<Vertex *, char> >, void *> *, [...]>>'

这并不是因为它们是一对。这是因为您没有调用函数foo。开始,而不是尝试直接访问它。

正确的代码是std::multimap<int, Bar >::iterator it = foo.begin();