使用pair作为映射中的键(C++/STL)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3277172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using pair as key in a map (C++ / STL)
提问by ccarpenterg
I want to use a pair from STL as a key of a map.
我想使用 STL 中的一对作为映射的键。
#include <iostream>
#include <map>
using namespace std;
int main() {
typedef pair<char*, int> Key;
typedef map< Key , char*> Mapa;
Key p1 ("Apple", 45);
Key p2 ("Berry", 20);
Mapa mapa;
mapa.insert(p1, "Manzana");
mapa.insert(p2, "Arandano");
return 0;
}
But the compiler throw a bunch of unreadable information and I'm very new to C and C++.
但是编译器会抛出一堆不可读的信息,我对 C 和 C++ 很陌生。
How can I use a pair as a key in a map? And in general How can I use any kind of structure (objects, structs, etc) as a key in a map?
如何在地图中使用一对作为键?总的来说,我如何使用任何类型的结构(对象、结构等)作为映射中的键?
Thanks!
谢谢!
采纳答案by James McNellis
std::map::insert
takes a single argument: the key-value pair, so you would need to use:
std::map::insert
采用单个参数:键值对,因此您需要使用:
mapa.insert(std::make_pair(p1, "Manzana"));
You should use std::string
instead of C strings in your types. As it is now, you will likely not get the results you expect because looking up values in the map will be done by comparing pointers, not by comparing strings.
您应该std::string
在类型中使用而不是 C 字符串。就像现在一样,您可能不会得到预期的结果,因为在映射中查找值将通过比较指针来完成,而不是通过比较字符串来完成。
If you really want to use C strings (which, again, you shouldn't), then you need to use const char*
instead of char*
in your types.
如果您真的想使用 C 字符串(同样,您不应该这样做),那么您需要在您的类型中使用const char*
而不是char*
。
And in general How can I use any kind of structure (objects, structs, etc) as a key in a map?
总的来说,我如何使用任何类型的结构(对象、结构等)作为映射中的键?
You need to overload operator<
for the key type or use a custom comparator.
您需要重载operator<
密钥类型或使用自定义比较器。
回答by Alec Jacobson
Here's a working rewrite of the code in question:
这是对相关代码的有效重写:
#include <map>
#include <string>
class Key
{
public:
Key(std::string s, int i)
{
this->s = s;
this->i = i;
}
std::string s;
int i;
bool operator<(const Key& k) const
{
int s_cmp = this->s.compare(k.s);
if(s_cmp == 0)
{
return this->i < k.i;
}
return s_cmp < 0;
}
};
int main()
{
Key p1 ("Apple", 45);
Key p2 ("Berry", 20);
std::map<Key,std::string> mapa;
mapa[p1] = "Manzana";
mapa[p2] = "Arandano";
printf("mapa[%s,%d] --> %s\n",
p1.s.c_str(),p1.i,mapa.begin()->second.c_str());
printf("mapa[%s,%d] --> %s\n",
p2.s.c_str(),p2.i,(++mapa.begin())->second.c_str());
return 0;
}
回答by Julian Declercq
Alternatively to what James McNellis stated:
替代詹姆斯麦克内利斯所说的:
mapa.insert(std::make_pair(p1, "Manzana"));
you could use mapa.insert({p1, "Manzana"});
你可以用 mapa.insert({p1, "Manzana"});
回答by Kumar Utkarsh
This is a similar version of what you want to do, just change the data types, that's all. Also, use a c++ string, not the one we use in c.
这是您想要做的类似版本,只需更改数据类型,仅此而已。另外,使用 c++ 字符串,而不是我们在 c 中使用的字符串。
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
typedef pair<ll,ll> my_key_type;
typedef map<my_key_type,ll> my_map_type;
int main()
{
my_map_type m;
m.insert(make_pair(my_key_type(30,40),6));
}
回答by trahul
This is will do exactly what you want
这将完全符合您的要求
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<pair<string, long long int>, string> MAP;
pair<string, long long int> P;
MAP.insert(pair<pair<string, long long int>, string>(pair<string, long long int>("Apple", 45), "Manzana"));
MAP.insert(pair<pair<string, long long int>, string>(pair<string, long long int>("Berry", 20), "Arandano"));
P = make_pair("Berry", 20);
//to find berry, 20
cout<<MAP[P]<<"\n";
return 0;
}