c++ - 将浮点值映射到一对键

标签 c++

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

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

}

最佳答案

您正在尝试输出 std::pair,这是您的键(即映射的第一个模板参数),但尚未为其定义流运算符。使用这个:

std::cout << setw(10) << it->first.first
          << setw(5) << it->first.second
          << setw(5) << it->second
          << std::endl;

关于c++ - 将浮点值映射到一对键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13499844/

相关文章:

c++ - NUMA 获取当前节点/核心

c++ - 在 C++ 中使用 map<pair<int,int>,string>

c++ - 标准头文件中的错误

c++如何将迭代器指针传递给需要引用对象的函数

c++ - 在 C++ 中,严格的自底向上分析如何暗示返回类型不用于重载决策?

c++ - 为什么用户定义的转换没有在调用对象上隐式发生

java - 在 Text-to-Speech 中自定义语音

c++ - __thiscall 与 __stdcall 行为

c++ - 在 QQuickItem 引用上发出信号

c++ - 应用在 RPC 上挂起