映射与对作为关键 [英] map with pair as key

查看:85
本文介绍了映射与对作为关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello world,

我想知道是否有可能创建一个使用

一对整数作为键和一个浮点作为值的映射。我使用带有const

char * []的地图作为关键,但到目前为止还没有成功使用版本

使用对作为键。


例如,我想按如下方式设置地图:


struct eqstr {

bool operator()( pair< int,int> s1,pair< int,int> s2)const {

return((s1.first == s2.first)&&(s1.second == s2。第二));

}

};


std :: map< std :: pair< int,int> * ,float,hash< const char *>,eqstr>

连接;


不幸的是,这不起作用。我如何将元素放入地图

以及如何访问它们?哪里可能是eqstr

结构和地图初始化的问题?


非常感谢你们......


Tim

解决方案

ki * ***@web.de 写道:

Hello world,
我想知道是否可以创建一个使用
一对int的地图作为键,浮动作为值。我使用带有const
char * []的地图作为键,但到目前为止还没有成功使用使用对作为键的版本

例如,我我想按如下方式设置地图:

struct eqstr {
bool operator()(pair< int,int> s1,pair< int,int> s2)const {
return((s1.first == s2.first)&&(s1.second == s2.second));
}
};

标准:: map< std :: pair< int,int> *,float,hash< const char *>,eqstr>
connections;




First好吧,哈希不是标准的C ++。


更重要的是,你的地图模板参数看起来很糟糕。

模板参数应该是映射<关键,类型,比较,分配取代。我是

不确定哈希应该如何适应这个方案但你需要什么

是一个小于Key类型的函数。显然eqstr不是一个

分配器 - 很可能你根本不需要指定第四个参数




例如...


struct PairCompare //或,ComPair :)

{

bool operator() (pair< int,int> * pp1,pair< int,int> * pp2)const

{

return pp1-> first< pp2-> first ||

pp1-> first == pp2-> first&& pp1->第二< pp2-> second;

}

};


-Mark

ki****@web.de schrieb:

Hello world,一对int作为键和一个float作为值的映射。我使用带有const
char * []的映射作为键,但到目前为止还没有成功使用使用对作为键的版本



有可能,我使用类似这样的东西:

std :: map< std :: pair< std :: pair< int,int>, int>,some_value_type>

例如,我想按如下方式设置地图:

struct eqstr {
bool operator()(pair< int ,int> s1,pair< int,int> s2)const {
return((s1.first == s2.first)&&(s1.second == s2.second));
}
};


当s1小于s2时,你的比较函数应该是真的,而不是当

相等时。的std ::对<>重载运算符<,所以不需要自定义

比较仿函数。

std :: map< std :: pair< int,int> *,float,hash< ; const char *>,eqstr>
连接;




1.为什么你有一个指针配对作为键?一个值就可以了。

否则你的仿函数会被指针调用。

2.比较仿函数应该是第三个模板参数。

3.什么是哈希<>为什么在这里使用它?

4.不幸的是,这不起作用。对我们没有帮助。复制并粘贴

错误消息。说,*出了什么问题,而不是出现问题。


Thomas


Mark P schrieb:

例如...

struct PairCompare //或,ComPair :)
{
bool operator()(pair< int,int> * pp1 ,pair< int,int> * pp2)const
{
返回pp1->第一个< pp2-> first ||
pp1-> first == pp2-> first&& pp1->第二< pp2->秒;
}
};




或者像这样:


struct PairLess

{

bool operator()(std :: pair< int,int> * pp1,std :: pair< int,int> * pp2)const

{

return * pp1< * PP2; // return std :: less(* pp1,* pp2);

}

};


Thomas


Hello world,
I was wondering whether it would be possible to create a map which uses
a pair of ints as key and a float as value. I have used maps with const
char*[] as key, but have so far not been successful to use a version
which uses pairs as keys.

For instance, I would like to set up the map as follows:

struct eqstr {
bool operator()(pair<int,int> s1, pair<int,int> s2) const{
return ((s1.first==s2.first) && (s1.second==s2.second));
}
};

std::map<std::pair<int,int>*, float, hash<const char*>, eqstr>
connections;

Unforunately, this does not work. How would I put elements into the map
and how could I access them? Where could be the problem with the eqstr
struct and map-initialization?

Thanks a lot you guys...

Tim

解决方案

ki****@web.de wrote:

Hello world,
I was wondering whether it would be possible to create a map which uses
a pair of ints as key and a float as value. I have used maps with const
char*[] as key, but have so far not been successful to use a version
which uses pairs as keys.

For instance, I would like to set up the map as follows:

struct eqstr {
bool operator()(pair<int,int> s1, pair<int,int> s2) const{
return ((s1.first==s2.first) && (s1.second==s2.second));
}
};

std::map<std::pair<int,int>*, float, hash<const char*>, eqstr>
connections;



First of all, hash is not standard C++.

More to the point, your map template parameters look to be out of whack.
The template parameters should be map<Key,Type,Compare,Alloc>. I''m
not sure how hash is supposed to fit into this scheme but what you need
is a less than function for the Key type. And clearly eqstr is not an
allocator-- most likely you don''t need to specify this fourth parameter
at all.

For example...

struct PairCompare // or, ComPair :)
{
bool operator () (pair<int,int>* pp1, pair<int,int>* pp2) const
{
return pp1->first < pp2->first ||
pp1->first == pp2->first && pp1->second < pp2->second;
}
};

-Mark


ki****@web.de schrieb:

Hello world,
I was wondering whether it would be possible to create a map which uses
a pair of ints as key and a float as value. I have used maps with const
char*[] as key, but have so far not been successful to use a version
which uses pairs as keys.
It is possible, I use something like this:
std::map<std::pair<std::pair<int,int>,int>, some_value_type>
For instance, I would like to set up the map as follows:

struct eqstr {
bool operator()(pair<int,int> s1, pair<int,int> s2) const{
return ((s1.first==s2.first) && (s1.second==s2.second));
}
};
Your compare function should be true, when s1 is less than s2, not when
equal. std::pair<> overloads operator<, so there is no need for a custom
compare functor.
std::map<std::pair<int,int>*, float, hash<const char*>, eqstr>
connections;



1. Why do you have a pointer to pair as key? A value would do it.
Otherwise your functor would be called with pointers.
2. The compare functor should be the 3rd template parameter.
3. What is hash<> and why do you use it here?
4. "Unforunately, this does not work." does not help us. Copy&Paste the
error messages. Say, *what* went wrong, not *that* something went wrong.

Thomas


Mark P schrieb:

For example...

struct PairCompare // or, ComPair :)
{
bool operator () (pair<int,int>* pp1, pair<int,int>* pp2) const
{
return pp1->first < pp2->first ||
pp1->first == pp2->first && pp1->second < pp2->second;
}
};



Or like this:

struct PairLess
{
bool operator() (std::pair<int,int>* pp1, std::pair<int,int>* pp2) const
{
return *pp1 < *pp2; // return std::less(*pp1, *pp2);
}
};

Thomas


这篇关于映射与对作为关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆