C++ 11搜索一个std ::复合键
问题描述:
地图我想要实现的标准::地图上该搜索下面的函数(findn)找到的元素。但在我的情况下,关键是复合值,它是一个<int,int>
C++ 11搜索一个std ::复合键
如何在这里使用std::map.find
?
#include <iostream>
#include <map>
#include <utility>
#include <string>
using namespace std;
std::map<std::pair<int, int>, std::string> studentMap;
int insert(int i, int j, std::string name) {
if(!studentMap.insert(std::make_pair (std::make_pair(i,j), name)).second) {
std::cout << "game not added" << std::endl;
} else {
std::cout << "game added" << std::endl;
}
return 0;
}
void findn(int i, int j) {
// how to find when we have composite key?
}
int main() {
insert(1,1,"test");
insert(1,1,"tes");
insert(1,2,"test 2");
std::cout << studentMap.size() << std::endl;
findn(1,1);
}
答
这将做的工作:
auto it = mymap.find(std::make_pair(i,j));
+0
如何检查“it”是否存在? – cateof
+2
'布尔存在=吧= mymap.end'!; [查找](http://en.cppreference.com/w/cpp/container/map/find)如果没有元素被发现返回结束迭代 –
如果钥匙*不*一个'的std :: pair',你会怎么做呢?是什么让你觉得在这种情况下会有所不同? –
我只是问正常的查找方法是否适用于组合键。 – cateof
只要先尝试一下就简单了。 :) –