473,811 Members | 3,220 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using two strings values as a key to the map

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

Sep 22 '08 #1
10 1588
On 22 Sep, 04:01, puzzlecracker <ironsel2...@gm ail.comwrote:
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
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
Sep 22 '08 #2
On 9ÔÂ22ÈÕ, ÉÏÎç11ʱ01·Ö, puzzlecracker <ironsel2....@g mail.comwrote:
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.

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
Sep 22 '08 #3
Barry wrote:
On 9ÔÂ22ÈÕ, ÉÏÎç11ʱ01·Ö, puzzlecracker <ironsel2...@gm ail.comwrote:
>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
implementati on (equivalent of which is shown below), I got some
compiler errors on gcc 3.3.1 with MyClass.
Your compiler is too old. I have no problems using g++ 4.1.3
>>
#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" ));

}

Unfortunatel y, 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.


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.
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?
Sep 22 '08 #4
On Sep 22, 5:37 am, Barry <dhb2...@gmail. comwrote:
On 9ÔÂ22ÈÕ, ÉÏÎç11ʱ01·Ö, puzzlecracker <ironsel2....@g mail.comwrote:
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.

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
Assignable?

Do I need a copy constructor and operator =?
How would it be implemented with pair?

Thanks
Sep 22 '08 #5
Barry wrote:
>struct MyClass
{
MyClass ( const string &a, const string & b):a_(a),b_(b){ }
string a_;
string b_;

};

The standard containers require the types to be Assignable.
In your code, MyClass is not,
Exactly how is it not assignable?
Sep 22 '08 #6
puzzlecracker wrote:
>>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) ;}
Assignable?

Do I need a copy constructor and operator =?
No, Barry is wrong. Your struct is copy constructable and assignable.

The compiler automatically defines a copy constructor and a copy
assignment operator for you. These work element-wise, that is, if all
elements of the struct are copy constructable and assignable, the struct
is, too.
How would it be implemented with pair?
Just replace MyClass with std::pair<std:: string,std::str ing>.
std::pair already has a correct operator< implemented.

You can then initialize a std::pair with the make_pair helper function:

m1.find( std::make_pair( "Foo","Bar" ) );

--
Thomas
Sep 22 '08 #7
On Sep 22, 9:13*am, "Thomas J. Gritzan" <phygon_antis.. .@gmx.de>
wrote:
puzzlecracker wrote:
>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) ;}
Assignable?
Do I need a copy constructor and operator =?

No, Barry is wrong. Your struct is copy constructable and assignable.

The compiler automatically defines a copy constructor and a copy
assignment operator for you. These work element-wise, that is, if all
elements of the struct are copy constructable and assignable, the struct
is, too.
How would it be implemented with pair?

Just replace MyClass with std::pair<std:: string,std::str ing>.
std::pair already has a correct operator< implemented.

You can then initialize a std::pair with the make_pair helper function:

m1.find( std::make_pair( "Foo","Bar" ) );

--
Thomas
how is operator< implemented if I have two strings not just one. I
think, i still need to supply the functor or operator <.

Sep 22 '08 #8
On Sep 22, 11:37 am, Barry <dhb2...@gmail. comwrote:
On 9?22?, ??11?01?, puzzlecracker <ironsel2...@gm ail.comwrote:
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.
The standard containers require the types to be Assignable.
In your code, MyClass is not,
It looks Assignable to me. What makes you say that it isn't
Assignable?
if the compiler have concept checking, compile error would be
produced.
I have no problem compiling it with g++ (4.1.0), with all
checking turned on.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 22 '08 #9
On Sep 22, 3:13 pm, "Thomas J. Gritzan" <phygon_antis.. .@gmx.de>
wrote:
puzzlecracker wrote:
>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) ;}
Assignable?
Do I need a copy constructor and operator =?
No, Barry is wrong. Your struct is copy constructable and
assignable.
The compiler automatically defines a copy constructor and a
copy assignment operator for you. These work element-wise,
that is, if all elements of the struct are copy constructable
and assignable, the struct is, too.
How would it be implemented with pair?
Just replace MyClass with std::pair<std:: string,std::str ing>.
std::pair already has a correct operator< implemented.
You can then initialize a std::pair with the make_pair helper function:
m1.find( std::make_pair( "Foo","Bar" ) );
But why doesn't his operator< work? It looks OK to me (although
I've have written the expression:

return a.a_ < b.a_ || (a.a_ == b.a_ && a.b_ < b.b_) ;

..)

For once someone is doing the right thing, and trying to write
readable code (rather than using std::pair). Let's not
discourage him.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 22 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2579
by: Jean-Fran?ois Lacrampe | last post by:
Hello, I've got two random number/statistics questions I'd like you to review. My first question is not directly related to PHP, but will be implemented in PHP, as explained in my second question, so let's go: I want to generate 10000 strings of x characters, with one chance (or less) on a million that you can guess them by just randomly typing them. So I need to know what is the value of x.
13
12215
by: Eric | last post by:
I need an effective way (time is my main concern here) to generate 10 000 000 unique alphanumeric strings of 16 characters each. I used STL set and map but after about 5 000 000 entries, it becomes very slow, even if I still have enough RAM available on my computer Do you have any advice? Here a code sample that I used to ensure uniqueness.
3
24039
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked tables in the database where the code resides. If we move the database with the data tables to a new directory, the links are no longer valid. I tried to update the links by changing the Connect property and refreshing: Set td = db.TableDefs(0)...
11
6609
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
37
2468
by: MLH | last post by:
For example: Nz(,0) returns "300" if the value in field is 300 (currency data type) and "0" if the value is zero or null. I get strings in the query output - they are all left aligned and I cannot add them without first converting them to values. What might be causing this?
19
78850
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not find one.
2
11152
by: JH | last post by:
I have checked my return strings and all are correct. But when I want to insert them into an Access database table it fails. However, if I change the insert values just to simple strings it works. Yet I have all the aryItemFields values as strings correctly. Any HINT>>>>PLEASE. ...(THIS PIECE OF CODE DOES NOT WORK.....). ... aryItemFields = fielInfo.fileName; aryItemFields = fielInfo.size.ToString();
1
2887
by: Nuno Morgadinho | last post by:
Hello all, I'm messing around with the Server Programming Interface and the particular example presented at: http://www.postgresql.org/docs/current/interactive/spi-examples.html Ideally, I would want to make the example function return the information as a "set" and not through elog() so I can later access it and print it using PHP.
7
2960
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that manipulate (and report on) members of the class. Interface consists of: - 5 private variables char author; char title; char code;
14
3139
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are very unhappy with the idea and the compiler refuses to play ball with any of the attempts I have made to build such a structure. There is no problem when using a single string but a two dimensional array of strings appears to be a very different...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10403
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5555
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5693
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4341
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3868
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.