Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

c++ - Using pair<int, int> as key for map

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
}   

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...