#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

int main() {

    map<pair<int,int>,pair<int,int>> items;

    items.insert(make_pair(1,20),make_pair(0,0));
    items.insert(make_pair(2,10),make_pair(0,0));
    items.insert(make_pair(3,30),make_pair(0,0));
    items.insert(make_pair(4,5),make_pair(0,0));
    items.insert(make_pair(5,35),make_pair(0,0));

    map<pair<int,int>,pair<int,int>>::iterator it;
    cout<<"Class ID:\t\t\tSamples:\t\t\tTP:\t\t\tPrecision:"<<endl;
    for(it = items.begin();it!=items.end();++it)
    {
        cout<<(it->first).first<<"\t\t\t"<<(it->first).second<<"\t\t\t"<<(it->second).first<<"\t\t\t"<<(it->second).second<<endl;
    }
}

我有这样的代码,但我无法打印我的值。有人能帮助我吗?

我得到了有趣的编译器错误:

c&#43;&#43; - 如何在cout中打印我的 map &lt;pair &lt;int,int&gt;,pair &lt;int,int &gt;&gt;-LMLPHP

这是错误的文本:
In file included from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/map:60,
from C:\Users\Fatih\Desktop\clion\quiz3\main.cpp:2:
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_tree.h: In instantiation of 'void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_II, _II) [with _InputIterator = std::pair<int, int>; _Key = std::pair<int, int>; _Val = std::pair<const std::pair<int, int>, std::pair<int, int> >; _KeyOfValue = std::_Select1st<std::pair<const std::pair<int, int>, std::pair<int, int> > >; _Compare = std::less<std::pair<int, int> >; _Alloc = std::allocator<std::pair<const std::pair<int, int>, std::pair<int, int> > >]':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_map.h:893:4:   required from 'void std::map<_Key, _Tp, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = std::pair<int, int>; _Key = std::pair<int, int>; _Tp = std::pair<int, int>; _Compare = std::less<std::pair<int, int> >; _Alloc = std::allocator<std::pair<const std::pair<int, int>, std::pair<int, int> > >]'
C:\Users\Fatih\Desktop\clion\quiz3\main.cpp:10:48:   required from here
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_tree.h:2467:28: error: no match for 'operator++' (operand type is 'std::pair<int, int>')
  for (; __first != __last; ++__first)
                            ^~~~~~~~~
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_tree.h:2468:29: error: no match for 'operator*' (operand type is 'std::pair<int, int>')
    _M_insert_unique_(end(), *__first, __an);
                             ^~~~~~~~

最佳答案

std::map::insert采用value_type的参数std::pair<const Key, Value>。这意味着您需要将make_pair(1,20),make_pair(0,0)组合为单个参数,以便可以调用该函数。看起来像

items.insert({make_pair(1,20),make_pair(0,0)});
//           ^curly braces create value_type^
items.insert({make_pair(2,10),make_pair(0,0)});
items.insert({make_pair(3,30),make_pair(0,0)});
items.insert({make_pair(4,5),make_pair(0,0)});
items.insert({make_pair(5,35),make_pair(0,0)});

或者,您可以使用 emplace ,它将使用参数来构造value_type。这样一来,您无需使用花括号就可以脱身,看起来像
items.emplace(make_pair(1,20),make_pair(0,0));
items.emplace(make_pair(2,10),make_pair(0,0));
items.emplace(make_pair(3,30),make_pair(0,0));
items.emplace(make_pair(4,5),make_pair(0,0));
items.emplace(make_pair(5,35),make_pair(0,0));

10-04 10:36