STL< map>有两把钥匙? [英] STL <map> with two keys ?
问题描述
我在STL中看到地图正在使用一把钥匙。
每个人都知道是否有可能有两把钥匙。
你有吗?一个小例子。
谢谢Markus
I have seen in the STL that the map is working with one key.
Does everyboby know if there is a possibility to have two key.
Do you have a little example.
Thanks Markus
推荐答案
Markus H?mmerli写道:
Markus H?mmerli wrote:
我在STL中看到地图正在使用一个密钥。
每个人都知道是否有可能有两个密钥。
显然不是每个人都知道,因为你不... ... ^ _ ^
说真的,我不是真的通过两把钥匙明白你的意思?如果
你的意思是你需要两个键来唯一地识别一个值,你不能只用两个成员作为一个键来使用
a类吗?
你有一个小例子。
I have seen in the STL that the map is working with one key.
Does everyboby know if there is a possibility to have two key.
Obviously not everybody knows, since you don''t... ^_^
Seriously though, I don''t really understand what you mean by two keys? If
you mean you need two keys to uniquely identify a value, can''t you just use
a class with two members as a key?
Do you have a little example.
#include< map>
class Keys {
public:
Keys(int k1,int k2):key1(k1),key2(k2){}
bool operator<(const Keys&右)const {
return(key1< right.key1&& key2< right.key2);
}
int key1;
int key2;
};
int main(){
std: :map< Keys,int> mymap;
mymap.insert(std :: pair< Keys,int>(Keys(3,8),5));
返回0;
}
-
不可原谅
大多数人都会进行概括< br / >
Freek de Jonge
#include <map>
class Keys {
public:
Keys(int k1, int k2) : key1(k1), key2(k2) { }
bool operator<(const Keys &right) const {
return (key1 < right.key1 && key2 < right.key2);
}
int key1;
int key2;
};
int main() {
std::map<Keys, int> mymap;
mymap.insert(std::pair<Keys, int>(Keys(3, 8), 5));
return 0;
}
--
Unforgiven
"Most people make generalisations"
Freek de Jonge
Markus H?mmerli写道:
Markus H?mmerli wrote:
我在STL表示地图正在使用一个键。
每个人都知道是否有可能有两个键。
你有一个小例子。
I have seen in the STL that the map is working with one key.
Does everyboby know if there is a possibility to have two key.
Do you have a little example.
你的问题不是很清楚,但也许std :: multimap是你想要的b $ b。它类似于std :: map,但允许多个键
具有相同的值。 (由''具有相同的值''我的意思是按键
比较相等。)
-Kevin
-
我的电子邮件地址有效,但会定期更改。
要联系我,请使用最近发布的地址。
Your question is not very clear, but perhaps std::multimap is what you
are looking for. It is similar to std::map, but allows multiple keys
with the same value. (By ''with the same value'' I mean that the keys
compare equal to each other.)
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Unforgiven写道:
Unforgiven wrote:
Markus H?mmerli写道:
Markus H?mmerli wrote:
我在STL看到地图是使用一把钥匙。
每个人都知道是否有可能有两把钥匙。
I have seen in the STL that the map is working with one key.
Does everyboby know if there is a possibility to have two key.
显然不是每个人都知道,因为你不...... ^ _ ^
说真的,我真的不明白你的意思是两把钥匙?如果
你的意思是你需要两个键才能唯一地识别一个值,你不能只使用一个有两个成员的班级作为关键吗?
Obviously not everybody knows, since you don''t... ^_^
Seriously though, I don''t really understand what you mean by two keys? If
you mean you need two keys to uniquely identify a value, can''t you just use
a class with two members as a key?
你有一个小例子。
#include< map>
类密钥{
public:
Keys (int k1,int k2):key1(k1),key2(k2){}
bool运算符<(const Keys& right)const {
return(key1< right.key1&& ; key2< right.key2);
}
int key1;
int key2;
};
int main(){
std :: map< Keys,int> mymap;
mymap.insert(std :: pair< Keys,int>(Keys(3,8),5));
返回0;
}
#include <map>
class Keys {
public:
Keys(int k1, int k2) : key1(k1), key2(k2) { }
bool operator<(const Keys &right) const {
return (key1 < right.key1 && key2 < right.key2);
}
int key1;
int key2;
};
int main() {
std::map<Keys, int> mymap;
mymap.insert(std::pair<Keys, int>(Keys(3, 8), 5));
return 0;
}
难道你不能只用钥匙对吗?
std :: map< pair< int,int>,int> mymap;
NR
Couldn''t you just use pair for the key?
std::map<pair<int,int>, int> mymap;
NR
这篇关于STL< map>有两把钥匙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!