473,394 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

STL map with key as a structure compare function issue

hi am facin a small problm,tried solving it, but my soln doesn't work.
cud u help me out.?

I need to maintain a map of <unsigned int[2], vector<int>>

here's wht i did - defined a class for storing the unsigned int [2] &
defined the corrspndng
compare function, the problem is that inspite of filling in the map
correctly, the find() returns an end - seems like the comparision
function's incorrect - but I can't seem to use a
workarnd.

If nothng works then I'd have to use a double as a key - but is there a
way out? I searched the net - but cudn't find an approp answer.

thanks a bunch!
here's the code

#include <map>
#include<string>
#include<vector>
# include<iostream.h>
using namespace std;

class Keysq
{
public :
bool operator() (const Keysq &a, const Keysq &b) const
{
return a.key[0] < b.key[0];
}
unsigned int key[2];
/*ostream& operator<< (ostream& os, const Keysq& s)
{
return os<<s.key[1];
}*/

};

typedef vector <int> df;

typedef map<Keysq,df,Keysq >assocMap;
assocMap::iterator itr;
main()
{
Keysq k,a;
k.key[0] = 10; k.key[1] = 20;
a.key[0] = 123; a.key[1] = 32;

df d;
d.push_back(1);
d.push_back(2);
d.push_back(3);

assocMap s ;
s[k] = d;
d.clear();
d.push_back(4);
d.push_back(5);
d.push_back(6);

s[a] = d;
itr = s.find(k);
cout << (itr == s.end())<<endl; // return a true!! l
}

May 4 '06 #1
2 7783
<ad********@gmail.com> schrieb im Newsbeitrag news:11**********************@v46g2000cwv.googlegr oups.com...
hi am facin a small problm,tried solving it, but my soln doesn't work.
cud u help me out.?

I need to maintain a map of <unsigned int[2], vector<int>>

here's wht i did - defined a class for storing the unsigned int [2] &
defined the corrspndng
compare function, the problem is that inspite of filling in the map
correctly, the find() returns an end - seems like the comparision
function's incorrect - but I can't seem to use a
workarnd.

If nothng works then I'd have to use a double as a key - but is there a
way out? I searched the net - but cudn't find an approp answer.

thanks a bunch!
here's the code

#include <map>
#include<string>
#include<vector>
# include<iostream.h>
There is no iostream.h. Use #include <iostream> instead.
using namespace std;

class Keysq
{
public :
bool operator() (const Keysq &a, const Keysq &b) const
{
return a.key[0] < b.key[0];
}
unsigned int key[2];
/*ostream& operator<< (ostream& os, const Keysq& s)
{
return os<<s.key[1];
}*/

};

typedef vector <int> df;

typedef map<Keysq,df,Keysq >assocMap;
assocMap::iterator itr;
main()
Should be

int main()
{
Keysq k,a;
k.key[0] = 10; k.key[1] = 20;
a.key[0] = 123; a.key[1] = 32;

df d;
d.push_back(1);
d.push_back(2);
d.push_back(3);

assocMap s ;
s[k] = d;
d.clear();
d.push_back(4);
d.push_back(5);
d.push_back(6);

s[a] = d;
itr = s.find(k);
cout << (itr == s.end())<<endl; // return a true!! l
}


Except for the two little bugs mentioned above, the program does as expected. It prints 0 as it should.

Heinz
May 4 '06 #2

<ad********@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
: hi am facin a small problm,tried solving it, but my soln doesn't work.
: cud u help me out.?
:
: I need to maintain a map of <unsigned int[2], vector<int>>
:
: here's wht i did - defined a class for storing the unsigned int [2] &
: defined the corrspndng
: compare function, the problem is that inspite of filling in the map
: correctly, the find() returns an end - seems like the comparision
: function's incorrect - but I can't seem to use a
: workarnd.
:
: If nothng works then I'd have to use a double as a key - but is there a
: way out? I searched the net - but cudn't find an approp answer.
:
: thanks a bunch!
: here's the code
:
: #include <map>
: #include<string>
: #include<vector>
: # include<iostream.h>
NB: the standard header is called <iostream>

: using namespace std;
:
: class Keysq
: {
: public :
: bool operator() (const Keysq &a, const Keysq &b) const
This should not be a member function:
friend bool operator( const Keysq &a, const Keysq &b)
: {
: return a.key[0] < b.key[0];
: }
But such a comparison function does not belong within the value
class itself. It should be a distinct 'predicate' class.
If you want to implement comparision within the class itself,
you should implement operator <(a,b) instead of operator(a,b) --
then you do not need to specify a comparision object for the map.

: unsigned int key[2];
: /*ostream& operator<< (ostream& os, const Keysq& s)
: {
: return os<<s.key[1];
: }*/
:
: };
:
: typedef vector <int> df;
:
: typedef map<Keysq,df,Keysq >assocMap;
: assocMap::iterator itr;
: main()
NB: return type needs to be specified: int main()
: {
: Keysq k,a;
: k.key[0] = 10; k.key[1] = 20;
: a.key[0] = 123; a.key[1] = 32;
:
: df d;
: d.push_back(1);
: d.push_back(2);
: d.push_back(3);
:
: assocMap s ;
: s[k] = d;
: d.clear();
: d.push_back(4);
: d.push_back(5);
: d.push_back(6);
:
: s[a] = d;
: itr = s.find(k);
: cout << (itr == s.end())<<endl; // return a true!! l

Well, in spite of your errors, this should actually work
(and return 0).

: }

Also, have you considered replacing Keysq with std::pair<int,int> ?
hth - Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
May 4 '06 #3

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

Similar topics

18
by: Chris R. | last post by:
Hi everyone. I am trying to finish my homework, but I seem not to figure out how to fix this structure that seems to make wrong output. What this program should do is use structure to save the...
9
by: B Vidyadhar Joshi | last post by:
I have converted a few C++ structures to C# structures. However, when I use them in the code, I get errors in "internal static BluetoothDeviceInfo Create()". I feel I'm doing something wrong while...
3
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String...
18
by: jparus | last post by:
Hello, can anybody explain me how to port FILE structure into other languages. I have an DLL library and functions in this library take FILE* as a parameter. I want to make an interface into...
0
by: ccshine | last post by:
I'm working on an app that implements a Structure to store a recordset in an ArrayList. I used this setup to bind to a DataGrid and it worked out so well, I thought it might be a better solution...
0
by: MikeCS | last post by:
Hi all I would like some help with this issue. I am new to VB 2005 (OK with VB6) My problem is that I cannot seem to return a structure from a function. Example: I defined a structure in a...
5
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have a Container that is an an Array List of Class Each ArrayList element can be the class or a another ArrayList of Class So there the ArrayList could look like Element 1 - Class...
4
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.