如何在 C++ 中在 int 和 pair 之间创建 unordered_multimap?
How can I make an unordered_multimap in C++ between an int and a pair?
尝试运行以下代码时出现此编译错误
"error: type/value mismatch at argument 2 in template parameter list for ‘template class std::unordered_multimap’ unordered_multimap m;"
有什么方法可以设置多图吗?如果不是我怎么能做我想做的事?谢谢!
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <map>
#include <unordered_map>
#include <utility>
using namespace std;
int main(){
int n = 100;
unordered_multimap<int, pair> m; //Error is in this line
for (int a = 0; a <= n; ++a)
for (int b = 0; b <= n; ++b)
{
int result = (a*a*a) + (b*b*b);
pair<int,int> p = {a,b};
pair<int,pair> p2 = {result,p};
m.insert(p2);
}
return 0;
}
A std::pair
本身不是类型,它是 "generates" 类型的模板。您需要通过指定它要求的 2 种模板参数类型来指定您想要 "make" 的类型。
您的用例表明您希望将两个整数作为一对,因此您应该在任何地方指定:
unordered_multimap<int, pair<int, int>> m;
和
pair<int,pair<int, int>> p2 = {result,p};
尝试运行以下代码时出现此编译错误 "error: type/value mismatch at argument 2 in template parameter list for ‘template class std::unordered_multimap’ unordered_multimap m;"
有什么方法可以设置多图吗?如果不是我怎么能做我想做的事?谢谢!
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <map>
#include <unordered_map>
#include <utility>
using namespace std;
int main(){
int n = 100;
unordered_multimap<int, pair> m; //Error is in this line
for (int a = 0; a <= n; ++a)
for (int b = 0; b <= n; ++b)
{
int result = (a*a*a) + (b*b*b);
pair<int,int> p = {a,b};
pair<int,pair> p2 = {result,p};
m.insert(p2);
}
return 0;
}
A std::pair
本身不是类型,它是 "generates" 类型的模板。您需要通过指定它要求的 2 种模板参数类型来指定您想要 "make" 的类型。
您的用例表明您希望将两个整数作为一对,因此您应该在任何地方指定:
unordered_multimap<int, pair<int, int>> m;
和
pair<int,pair<int, int>> p2 = {result,p};