使用两个字符串值作为地图的键 [英] using two strings values as a key to the map

查看:53
本文介绍了使用两个字符串值作为地图的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够将两个字符串映射到地图中的某个值。我想b $ b考虑实现两种替代方法:使用pair类或使用包含两个成员字符串的单独的类/结构。我更喜欢

后者,因为它更具描述性。但是,在后面的实现(相当于下面显示)中,我在使用MyClass的gcc 3.3.1上得到了一些

编译器错误。

#include< map>

#include< string>


使用命名空间std;


struct MyClass

{

MyClass(const string& a,const string& b):a_(a),b_(b){}

string a_;

string b _;

};


内联bool运算符<(const MyClass& a, const MyClass& b)

{

返回a.a_< b.a_ ||(a.a _ == b.a_?a.b_< b.b_ :false);

}

int main()

{

//这导致错误

地图< MyClass的,MyClass的* GT;米;

MyClass的我的(QUOT;富","酒吧及QUOT);

m.find(我的);


//修好它

map< MyClass,MyClass *> m1;

m1.find( MyClass(" Foo"," Bar"));

}


很遗憾,我无法显示编译器错误,b / c我无法在家里重现它,只能在工作中重现它。也许,错误我会发布它

明天。


我想知道是否有人可以在这里看到问题或建议

替代设计。


谢谢

I need to be able to map two string to some value in the map. I
consider two alternatives to implement : using pair class or using a
separate class/struct containing two member strings. I prefer the
latter for it''s more descriptive. However, in the latter
implementation (equivalent of which is shown below), I got some
compiler errors on gcc 3.3.1 with MyClass.
#include<map>
#include <string>

using namespace std;

struct MyClass
{
MyClass ( const string &a, const string & b):a_(a),b_(b){}
string a_;
string b_;
};

inline bool operator <(const MyClass &a, const MyClass & b)
{
return a.a_<b.a_ ||(a.a_==b.a_?a.b_<b.b_:false);
}
int main()
{
//This caused the error
map<MyClass, MyClass *>m;
MyClass my("Foo", "Bar");
m.find(my);

//This fixed it
map<MyClass, MyClass *>m1;
m1.find(MyClass("Foo","Bar"));
}

Unfortunately, I can''t show the compiler error, b/c I couldn''t
reproduce it at home, only at work. Perhaps, error I''ll post it
tomorrow.

I want to see if anyone can see the issue here or suggest an
alternative design.

Thanks

推荐答案

9月22日,04: 01,puzzlecracker< ironsel2 ... @ gmail.comwrote:
On 22 Sep, 04:01, puzzlecracker <ironsel2...@gmail.comwrote:

我需要能够将两个字符串映射到地图中的某个值。我想b $ b考虑实现两种替代方法:使用pair类或使用包含两个成员字符串的单独的类/结构。我更喜欢

后者,因为它更具描述性。但是,在后一个

实现中(相当于下面显示的*),我在使用MyClass的gcc 3.3.1上得到了一些

编译器错误。


#include< map>

#include< string>


使用命名空间std;


struct MyClass

{

* * * MyClass(const string& a,const * string *& b):a_(a),b_( b){}

* * *字符串a_;

* * *字符串b_;


};


内联布尔运算符<(const MyClass& a,const MyClass& b)

{

* * *返回a.a_< ; b.a_ ||(a.a _ == b.a_?a.b_< b.b_:false);}


int main()

{

* * //导致错误

* * map< MyClass,MyClass *> m;

* * MyClass我的(Foo,Bar);

* * m.find(my);


//这修好了它

* * map< MyClass,MyClass *> m1;

* * m1.find(MyClass(" Foo"," Bar"));


}


不幸的是,我无法显示编译错误,b / c *我不能

在家里复制,只在工作中。也许,*错误我会发布它

明天。


我想看*如果有人能在这里看到问题或建议

替代*设计。


谢谢
I need to be able to map two string to some value in the map. I
consider two alternatives to implement : using pair class or using a
separate class/struct containing two member strings. I prefer the
latter for it''s more descriptive. However, in the latter
implementation (equivalent of which *is shown below), I got some
compiler errors on gcc 3.3.1 with MyClass.

#include<map>
#include <string>

using namespace std;

struct MyClass
{
* * *MyClass ( const string &a, const *string *& b):a_(a),b_(b){}
* * *string a_;
* * *string b_;

};

inline bool operator <(const MyClass &a, const MyClass & b)
{
* * *return a.a_<b.a_ ||(a.a_==b.a_?a.b_<b.b_:false);}

int main()
{
* *//This caused the error
* * map<MyClass, MyClass *>m;
* * MyClass my("Foo", "Bar");
* * m.find(my);

//This fixed it
* * map<MyClass, MyClass *>m1;
* * m1.find(MyClass("Foo","Bar"));

}

Unfortunately, I can''t show the compiler error, b/c *I couldn''t
reproduce it at home, only at work. Perhaps, *error I''ll post it
tomorrow.

I want to see *if anyone can see the issue here or suggest an
alternative *design.

Thanks



使用Microsoft Visual C ++ 2003(7.1)编译好了。我会

自己使用std ::对。写的少,我觉得它更好

描述性,人们确切地知道std :: pair是如何工作但需要

看看本土实体包括运算符<实施

之前他们对此感到满意。


问候,


Pete

That compiled ok here using Microsoft Visual C++ 2003 (7.1). I would
use a std::pair myself. Less to write and I think it''s MORE
descriptive and people know exactly how a std::pair works but need to
look at the home-grown entity including the operator< implementation
before they''re comfortable with it.

Regards,

Pete


On 9 ??22è?,é???11ê±01·?, puzzlecracker< ironsel2 .... @ gmail.comwrote:
On 9??22è?, é???11ê±01·?, puzzlecracker <ironsel2....@gmail.comwrote:

我需要能够将两个字符串映射到地图中的某个值。我想b $ b考虑实现两种替代方法:使用pair类或使用包含两个成员字符串的单独的类/结构。我更喜欢

后者,因为它更具描述性。但是,在后面的实现(相当于下面显示)中,我在使用MyClass的gcc 3.3.1上得到了一些

编译器错误。


#include< map>

#include< string>


使用命名空间std;


struct MyClass

{

MyClass(const string& a,const string& b):a_(a),b_(b){}

字符串a_;

字符串b_;


};


内联bool运算符< ;(const MyClass& a,const MyClass& b)

{

返回a.a_< b.a_ ||(a.a _ == b.a_ ?a.b_< b.b_:false);}


int main()

{

//这导致错误

map< MyClass,MyClass *> m;

MyClass my(" Foo"," Bar");

m.find(my);


//修好它

map< MyClass,MyClass *> m1;

m1.find(MyClass的(QUOT;富","酒吧及QUOT;));


}


不幸的是,我无法显示编译错误,b / c我不能

在家里复制,只在工作中。也许,错误我会发布它

明天。


我想知道是否有人可以在这里看到问题或建议

替代设计。
I need to be able to map two string to some value in the map. I
consider two alternatives to implement : using pair class or using a
separate class/struct containing two member strings. I prefer the
latter for it''s more descriptive. However, in the latter
implementation (equivalent of which is shown below), I got some
compiler errors on gcc 3.3.1 with MyClass.

#include<map>
#include <string>

using namespace std;

struct MyClass
{
MyClass ( const string &a, const string & b):a_(a),b_(b){}
string a_;
string b_;

};

inline bool operator <(const MyClass &a, const MyClass & b)
{
return a.a_<b.a_ ||(a.a_==b.a_?a.b_<b.b_:false);}

int main()
{
//This caused the error
map<MyClass, MyClass *>m;
MyClass my("Foo", "Bar");
m.find(my);

//This fixed it
map<MyClass, MyClass *>m1;
m1.find(MyClass("Foo","Bar"));

}

Unfortunately, I can''t show the compiler error, b/c I couldn''t
reproduce it at home, only at work. Perhaps, error I''ll post it
tomorrow.

I want to see if anyone can see the issue here or suggest an
alternative design.



标准容器要求类型可分配。

在您的代码中,MyClass不是,

如果编译器有概念检查,编译错误将是

产生的。


HTH。


-

最好的问候

Barry


The standard containers require the types to be Assignable.
In your code, MyClass is not,
if the compiler have concept checking, compile error would be
produced.

HTH.

--
Best Regards
Barry


Barry写道:
Barry wrote:

On 9 ??22è?,é???11ê±01·?, puzzlecracker< ironsel2 ... @ gmail.comwrote:
On 9??22è?, é???11ê±01·?, puzzlecracker <ironsel2...@gmail.comwrote:

>我需要能够将两个字符串映射到地图中的某个值。我考虑实现两种替代方法:使用pair类或使用包含两个成员字符串的单独的类/结构。我更喜欢
后者,因为它更具描述性。但是,在后面的实现(相当于下面显示)中,我在使用MyClass的gcc 3.3.1上得到了一些
编译器错误。
>I need to be able to map two string to some value in the map. I
consider two alternatives to implement : using pair class or using a
separate class/struct containing two member strings. I prefer the
latter for it''s more descriptive. However, in the latter
implementation (equivalent of which is shown below), I got some
compiler errors on gcc 3.3.1 with MyClass.



您的编译器太旧了。我使用g ++ 4.1.3没有问题

Your compiler is too old. I have no problems using g++ 4.1.3


>>
#include< map>
#include< string>

使用命名空间std;

struct MyClass
{MyClass(const string& a,const string& b) ):a_(a),b_(b){}
字符串a_;
字符串b_;

};

内联bool运算符< (const MyClass& a,const MyClass& b)
{
返回a.a_< b.a_ ||(a.a _ == b.a_?a.b_< b.b_: false);}

int main()
//这导致错误
map< MyClass,MyClass *> m;
MyClass我的(Foo,Bar);
m.find(my);

//修好了它
map< MyClass,MyClass *> m1 ;
m1.find(MyClass(" Foo"," Bar"));

}
很遗憾,我无法显示编译器错误,b / c我不能在家里复制它,只能在工作中复制它。也许,错误我会在明天发布它。

我想看看是否有人可以在这里看到问题或建议
替代设计。
>>
#include<map>
#include <string>

using namespace std;

struct MyClass
{
MyClass ( const string &a, const string & b):a_(a),b_(b){}
string a_;
string b_;

};

inline bool operator <(const MyClass &a, const MyClass & b)
{
return a.a_<b.a_ ||(a.a_==b.a_?a.b_<b.b_:false);}

int main()
{
//This caused the error
map<MyClass, MyClass *>m;
MyClass my("Foo", "Bar");
m.find(my);

//This fixed it
map<MyClass, MyClass *>m1;
m1.find(MyClass("Foo","Bar"));

}

Unfortunately, I can''t show the compiler error, b/c I couldn''t
reproduce it at home, only at work. Perhaps, error I''ll post it
tomorrow.

I want to see if anyone can see the issue here or suggest an
alternative design.




标准容器要求类型可分配。

在你的代码中,MyClass不是,

如果编译器有概念检查,编译错误会产生



The standard containers require the types to be Assignable.
In your code, MyClass is not,
if the compiler have concept checking, compile error would be
produced.



这个使用g ++编译4.1.3

int main()

{

MYCLASS一个(" Foo1"," BAR1");

MyClass的b(" foo2的"," BAR2");


b = a;


std :: cout<< b.a_<< std :: endl;

std :: cout< < b.b_<< std :: endl;

}


(当然,包括iostream)


我是以某种方式造成未定义的行为吗?

This compiles using g++ 4.1.3

int main()
{
MyClass a("Foo1", "Bar1");
MyClass b("Foo2", "Bar2");

b = a;

std::cout<<b.a_<<std::endl;
std::cout<<b.b_<<std::endl;
}

(off course, include iostream)

Am I causing undefined behavior somehow?


这篇关于使用两个字符串值作为地图的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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