print deque包含multimap

时间:2015-03-02 14:19:09

标签: c++ multimap deque

我需要打印一个包含多图的dequeu,但它不起作用,我做错了什么?

int main()
{
    deque<multimap<string,int> > q_map;
    multimap<string,int> m;

    m.insert(pair<string, int>("a", 1));
    m.insert(pair<string, int>("c", 2));
    m.insert(pair<string, int>("b", 3));
    m.insert(pair<string, int>("b", 4));
    m.insert(pair<string, int>("a", 5));
    m.insert(pair<string, int>("b", 6));
    cout << "Map size: " << m.size() << endl;

    q_map.push_back(m);
    for (std::deque<int>::iterator it = q_map.begin(); it!=q_map.end(); ++it)
        std::cout << ' ' << *it;
}

我总是遇到编译错误:

error: conversion from ‘std::_Deque_iterator<std::multimap<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >, std::multimap<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >&, std::multimap<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >*>’ to non-scalar type ‘std::_Deque_iterator<int, int&, int*>’ requested
q_map.cpp:23: error: no match for ‘operator!=’ in ‘it != std::deque<_Tp, _Alloc>::end() [with _Tp = std::multimap<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >, _Alloc = std::allocator<std::multimap<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > > >]()’

2 个答案:

答案 0 :(得分:1)

好吧,两个简单的for循环,一个迭代deque,另一个迭代multimap

for ( auto it = q_map.begin(); it != q_map.end(); ++it )
    for ( auto it2 = (*it).begin(); it2 != (*it).end(); ++it2 )
        cout << ' ' << it2->first << ' ' << it2->second << endl;

或使用范围循环

for ( auto& it : q_map )
    for ( auto& it2 : it )
        cout << ' ' << it2.first << ' ' << it2.second << endl;

答案 1 :(得分:0)

你有这种类型:

deque<multimap<string,int> > q_map;

但是你正试图用这种类型迭代它:

for (std::deque<int>::iterator it = q_map.begin(); ...

那是你做错了什么。

如果您不能使用基于C ++ 11范围的for循环或C ++ 11 auto功能,那么请不要重复multimap<string,int>类型无处不在,你可以使用typedef:

typedef multimap<string,int> mmap;

现在,您可以在使用它的任何地方引用std::deque<mmap>,因此for循环将使用std::deque<mmap>::iterator