简体   繁体   English

以对为键访问地图

[英]Accessing map with pair as the key

As the title said. 正如标题所说。 I'm new to c++. 我是C ++的新手。 So i wanted to make a map with pair of int as the key and boolean as the value 所以我想用一对int作为键和布尔值作为映射

 map <pair<int,int>,bool>.

how do i assign the value and access it? 如何分配和访问值?

You'll need to pass one std::pair<int,int> object (your comment suggests passing two int objects, but that's no pair yet .) 您需要传递一个std::pair<int,int>对象(您的注释建议传递两个int对象,但这还不是一对。)

You can create a std::pair<int,int> like this: std::pair<int,int> {5,7} or figure out the types from the two arguments to std::make_pair(5, 7) . 您可以像这样创建一个std::pair<int,int>std::pair<int,int> {5,7}或从std::make_pair(5, 7) std::pair<int,int> {5,7}的两个参数中找出类型。

The first argument in 中的第一个参数

map <pair<int,int>, bool> mp;

is a key of pair. 是配对的钥匙。 Therefore you can assign a value and access it as - 因此,您可以分配一个值并将其访问为-

mp[{1,2}] = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM