以我自己的类型为关键的地图 [英] maps with my own type as key

查看:53
本文介绍了以我自己的类型为关键的地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好:)


i有一个简单的(?)问题,使用我自己的密钥类型的地图。


i使用过struct重载<运营商,但是如果我用地图中不存在的密钥搜索(使用

find),我会从地图中获得现有的

键/值对。 br />

i试过这样的事情:


struct MyKey {

public:

MyKey(

unsigned short key1,

unsigned long key2)

:key1(key1),

key2( key2){}

bool运算符<(MyKey const& scnd)const {

return(

key1< scnd.key1 &&

key2< scnd.key2);

};


私人:

unsigned short key1;

unsigned long key2;

};


class MyValue {

....

}


std :: map< MyKey,MyValue * MyValueList;


any想法?教程?代码示例?


谢谢&关于


asterix

解决方案

asterixgallier写道:


i使用我自己的键类型的地图有一个简单的(?)问题。


i使用了一个结构重载<运营商,但是如果我用地图中不存在的密钥搜索(使用

find),我会从地图中获得现有的

键/值对。 br />

i试过这样的事情:


struct MyKey {

public:

MyKey(

unsigned short key1,

unsigned long key2)

:key1(key1),

key2( key2){}

bool运算符<(MyKey const& scnd)const {

return(

key1< scnd.key1 &&

key2< scnd.key2);

};


私人:

unsigned short key1;

unsigned long key2;

};


class MyValue {

...

}


std :: map< MyKey,MyValue * MyValueList;



首先,考虑(a)MyKey(1,10)和(b)MyKey(9,2)。哪一个比另一个少?
?然后google进行严格弱排序。提示 -

您的运营商<需要保证,但没有这样做。


祝你好运,


Tom


asterixgallier写道:


hello :)


i有一个简单的(?)问题用我自己的键类型映射。


i使用了一个结构重载<运营商,但是如果我用地图中不存在的密钥搜索(使用

find),我会从地图中获得现有的

键/值对。 br />

i试过这样的事情:


struct MyKey {

public:

MyKey(

unsigned short key1,

unsigned long key2)

:key1(key1),

key2( key2){}

bool运算符<(MyKey const& scnd)const {

return(

key1< scnd.key1 &&

key2< scnd.key2);

};


私人:

unsigned short key1;

unsigned long key2;

};


class MyValue {

...

}


std :: map< MyKey,MyValue * MyValueList;


任何想法?教程?代码示例?


谢谢&关于


asterix



你能发一个问题的例子吗?以下代码似乎对我来说是好的工作:


#include< map>

using namespace std;


struct MyKey {

public:

MyKey(unsigned short key1,unsigned long key2)

: key1(key1),key2(key2){}

bool运算符<(MyKey const& scnd)const {

return(key1< scnd.key1& ;&

key2< scnd.key2);

};


私人:

unsigned short key1;

unsigned long key2;


};


class MyValue

{

};

int main(int argc,char * argv [])

{

map< MyKey,MyValue * MyValueList;

MyKey TestKey(0,0); //这个键在地图中不存在

map< MyKey,MyValue *> :: iterator It = MyValueList.find(TestKey);


if(It == MyValueList.end())

cout<< 密钥不存在 << endl;


返回EXIT_SUCCESS;

}


jo *********** @ gmail.com 写道:


asterixgallier写道:


hello :)


i有一个简单的(?)问题用我自己的键类型映射。


i使用了一个结构重载<运营商,但是如果我用地图中不存在的密钥搜索(使用

find),我会从地图中获得现有的

键/值对。 br />

i试过这样的事情:


struct MyKey {

public:

MyKey(

unsigned short key1,

unsigned long key2)

:key1(key1),

key2( key2){}

bool运算符<(MyKey const& scnd)const {

return(

key1< scnd.key1 &&

key2< scnd.key2);

};


私人:

unsigned short key1;

unsigned long key2;

};


class MyValue {

...

}


std :: map< MyKey,MyValue * MyValueList;


任何想法?教程?代码示例?


谢谢&关于


asterix



你能发一个问题的例子吗?以下代码似乎对我来说是好的工作:


#include< map>

using namespace std;


struct MyKey {

public:

MyKey(unsigned short key1,unsigned long key2)

: key1(key1),key2(key2){}

bool运算符<(MyKey const& scnd)const {

return(key1< scnd.key1& ;&

key2< scnd.key2);

};


私人:

unsigned short key1;

unsigned long key2;


};


class MyValue

{

};


int main(int argc,char * argv [])

{

map< MyKey,MyValue * MyValueList;

MyKey TestKey(0,0); //这个键在地图中不存在

map< MyKey,MyValue *> :: iterator It = MyValueList.find(TestKey);


if(It == MyValueList.end())

cout<< 密钥不存在 <<结束;


返回EXIT_SUCCESS;

}



感谢您的所有努力。


我认为问题在于,地图只能通过两个键之一来查找和排序

。例如,如果我想向地图添加第二个值

,使用相同的''key1''但不同的''key2'',则不添加的元素

到列表。


map< MyKey,MyValue * MyValueList;

MyValueList [MyKey(1,1)] = new MyValue();

MyValueList [MyKey(2,1)] = new MyValue();

MyValueList [MyKey(1,2)] = new MyValue();

//期望三个元素的大小但只能获得一个大小

cout<< 列表长度: << MyValueList.size();


hello :)

i have a simple (?) problem using maps with my own type for the key.

i have used a struct overloading the < operator, but if i search (using
find) with a key, that does not exists in the map, i get anexisting
key/value pair from the map.

i have tried something like this:

struct MyKey {
public:
MyKey(
unsigned short key1,
unsigned long key2)
:key1(key1),
key2(key2) {}

bool operator<(MyKey const& scnd) const {
return (
key1<scnd.key1 &&
key2<scnd.key2);
};

private:
unsigned short key1;
unsigned long key2;
};

class MyValue {
....
}

std::map <MyKey, MyValue*MyValueList;

any ideas? tutorials? code examples?

thanks & with regards

asterix

解决方案

asterixgallier wrote:

i have a simple (?) problem using maps with my own type for the key.

i have used a struct overloading the < operator, but if i search (using
find) with a key, that does not exists in the map, i get anexisting
key/value pair from the map.

i have tried something like this:

struct MyKey {
public:
MyKey(
unsigned short key1,
unsigned long key2)
:key1(key1),
key2(key2) {}

bool operator<(MyKey const& scnd) const {
return (
key1<scnd.key1 &&
key2<scnd.key2);
};

private:
unsigned short key1;
unsigned long key2;
};

class MyValue {
...
}

std::map <MyKey, MyValue*MyValueList;

First, consider (a) MyKey(1, 10) and (b) MyKey(9, 2). Which one is
less than the other? Then google for "strict weak ordering." Hint -
your operator< needs to guarantee it, but fails to do so.

Best regards,

Tom


asterixgallier wrote:

hello :)

i have a simple (?) problem using maps with my own type for the key.

i have used a struct overloading the < operator, but if i search (using
find) with a key, that does not exists in the map, i get anexisting
key/value pair from the map.

i have tried something like this:

struct MyKey {
public:
MyKey(
unsigned short key1,
unsigned long key2)
:key1(key1),
key2(key2) {}

bool operator<(MyKey const& scnd) const {
return (
key1<scnd.key1 &&
key2<scnd.key2);
};

private:
unsigned short key1;
unsigned long key2;
};

class MyValue {
...
}

std::map <MyKey, MyValue*MyValueList;

any ideas? tutorials? code examples?

thanks & with regards

asterix

Can you post an example of your problem? The following code appears to
work fine for me:

#include <map>
using namespace std;

struct MyKey {
public:
MyKey(unsigned short key1, unsigned long key2)
:key1(key1), key2(key2) {}

bool operator<(MyKey const& scnd) const {
return (key1<scnd.key1 &&
key2<scnd.key2);
};

private:
unsigned short key1;
unsigned long key2;

};

class MyValue
{
};
int main(int argc, char *argv[])
{
map<MyKey, MyValue*MyValueList;
MyKey TestKey(0, 0); //this key does not exist in the map
map<MyKey, MyValue*>::iterator It = MyValueList.find(TestKey);

if (It == MyValueList.end())
cout << "Key does not exist" << endl;

return EXIT_SUCCESS;
}


jo***********@gmail.com wrote:

asterixgallier wrote:

hello :)

i have a simple (?) problem using maps with my own type for the key.

i have used a struct overloading the < operator, but if i search (using
find) with a key, that does not exists in the map, i get anexisting
key/value pair from the map.

i have tried something like this:

struct MyKey {
public:
MyKey(
unsigned short key1,
unsigned long key2)
:key1(key1),
key2(key2) {}

bool operator<(MyKey const& scnd) const {
return (
key1<scnd.key1 &&
key2<scnd.key2);
};

private:
unsigned short key1;
unsigned long key2;
};

class MyValue {
...
}

std::map <MyKey, MyValue*MyValueList;

any ideas? tutorials? code examples?

thanks & with regards

asterix


Can you post an example of your problem? The following code appears to
work fine for me:

#include <map>
using namespace std;

struct MyKey {
public:
MyKey(unsigned short key1, unsigned long key2)
:key1(key1), key2(key2) {}

bool operator<(MyKey const& scnd) const {
return (key1<scnd.key1 &&
key2<scnd.key2);
};

private:
unsigned short key1;
unsigned long key2;

};

class MyValue
{
};
int main(int argc, char *argv[])
{
map<MyKey, MyValue*MyValueList;
MyKey TestKey(0, 0); //this key does not exist in the map
map<MyKey, MyValue*>::iterator It = MyValueList.find(TestKey);

if (It == MyValueList.end())
cout << "Key does not exist" << endl;

return EXIT_SUCCESS;
}

thanks for all your effort.

I think the problem is, that the map only looks and sorts ony by one of
the two keys. For example, if i want to add a second value to the map
with the same ''key1'' but different ''key2'', the element where not added
to the list.

map<MyKey, MyValue*MyValueList;
MyValueList[MyKey(1,1)] = new MyValue();
MyValueList[MyKey(2,1)] = new MyValue();
MyValueList[MyKey(1,2)] = new MyValue();
// expect a size of three elements but only get size of one
cout << "Length of List: " << MyValueList.size();


这篇关于以我自己的类型为关键的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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