将浮点值映射到键是一对

Vit*_*one 3 c++

我正在尝试创建一个映射并将浮点值映射到类型为pair的键.我无法使用显示功能显示地图.

#include <iostream>
#include <utility>
#include <iomanip>
#include <map>

using namespace std;
typedef pair<int, int> Key; //pair

void display (map <Key,float> &m) // to print maps
{
    cout << "\tTotal size: " << m.size() << endl; 
    map <Key,float>::iterator it;
    for (it = m.begin(); it != m.end(); ++it)
       cout << setw(10) << it->first << setw(5) << it->second << endl;

    cout << endl; 
}

int main() {

map< Key , float> mapa; //create map

Key p1 (1, 45); //key values
Key p2 (2, 20);

mapa[p1]= 25.11; //map float to keys
mapa[p2]= 11.23;

display(mapa); //display map

return 0;

}
Run Code Online (Sandbox Code Playgroud)

Den*_*lin 6

您正在尝试输出a std::pair,这是您的关键(即地图的第一个模板参数),但没有为其定义流操作符.用这个:

std::cout << setw(10) << it->first.first
          << setw(5) << it->first.second
          << setw(5) << it->second
          << std::endl;
Run Code Online (Sandbox Code Playgroud)