使用pair< int,int>作为地图的关键 [英] Using pair<int, int> as key for map

查看:216
本文介绍了使用pair< int,int>作为地图的关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据上一个问题,我正在尝试创建地图使用一对整数作为关键字 map< pair< int,int> int> ,我找到了如何插入信息:

Based on a previous question, I am trying to create a map using a pair of integers as a key i.e. map<pair<int, int>, int> and I've found information on how to insert:

#include <iostream>
#include <map>

using namespace std;

int main ()
{
map<pair<int, int>, int> mymap;

mymap.insert(make_pair(make_pair(1,2), 3)); //edited
}   

但我似乎无法访问元素!我试过 cout<< mymap [(1,2)]< endl; 但它显示一个错误,我找不到如何使用键访问元素的信息。

but I can't seem to access the element! I've tried cout << mymap[(1,2)] << endl; but it shows an error, and I can't find information on how to access the element using the key. Am I doing something wrong?

推荐答案

您需要一个键作为键 cout< mymap [make_pair(1,2)]< endl; 您目前拥有的 cout<< mymap [(1,2)]< endl; 不是正确的语法。

you need a pair as a key cout << mymap[make_pair(1,2)] << endl; What you currently have cout << mymap[(1,2)] << endl; is not the correct syntax.

这篇关于使用pair&lt; int,int&gt;作为地图的关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆