STL set Problem

M

Mike Copeland

I have the following application stub that compiles and works. Now I
wish to add "find" functionality (based on matching the "baseBib"
element), and I can't see from the documentation or examples I've found
hoe to do so. The STL set "find" seems meant only for scalar objects
(but the optional insert parameter supports field-based ordering...).
What am I missing in this? TIA

#pragma warning (disable:4786)
#include <set>
#include <iostream>
#include <string>
using namespace std;

class Entrant
{
public:
int baseBib;
char baseEvent;
};
class LoToHigh
{
public:
bool operator()(Entrant s1, Entrant s2)
{
if(s1.baseBib < s2.baseBib) return true;
else return false;
}
};

int main(int argc, char* argv[])
{
set <Entrant, LoToHigh> myEntrant;
set <Entrant, LoToHigh>::iterator it;
Entrant a1, a2, a3;

a1.baseBib = 2077, a1.baseEvent = 'a';
a2.baseBib = 2066, a2.baseEvent = 'b';
a3.baseBib = 2055, a3.baseEvent = 'a';
myEntrant.insert(a1), myEntrant.insert(a2);
myEntrant.insert(a1), myEntrant.insert(a3);
cout << "The # of Entrants is " << myEntrant.size() << endl;
for(it = myEntrant.begin(); it != myEntrant.end(); it++)
{
cout << it->baseBib << "\t" << it->baseEvent << endl;
}
return 0;
}
 
I

Ian Collins

I have the following application stub that compiles and works. Now I
wish to add "find" functionality (based on matching the "baseBib"
element), and I can't see from the documentation or examples I've found
hoe to do so. The STL set "find" seems meant only for scalar objects
(but the optional insert parameter supports field-based ordering...).
What am I missing in this? TIA

Have you considered using a map rather than a set?
 
K

Kai-Uwe Bux

Mike said:
I have the following application stub that compiles and works. Now I
wish to add "find" functionality (based on matching the "baseBib"
element), and I can't see from the documentation or examples I've found
hoe to do so. The STL set "find" seems meant only for scalar objects
(but the optional insert parameter supports field-based ordering...).
What am I missing in this?

I don't understand the bit about scalar objects.

#pragma warning (disable:4786)
#include <set>
#include <iostream>
#include <string>
using namespace std;

class Entrant
{
public:
int baseBib;
char baseEvent;
};
class LoToHigh
{
public:
bool operator()(Entrant s1, Entrant s2)
{
if(s1.baseBib < s2.baseBib) return true;
else return false;
}
};

The function std::set<>::find() uses the notion of equivalence. Two entries
a and b are equivalent when neither a<b nor b<a. In your case, that should
amount to the baseBib fields being equal. Thus, if you are given an int
value bib, you can search for that, by doing:

find( Entrant( bib, 0 ) )

I.e., you turn this into a meaningless Entrant object with correct baseBib
value and search for that. The invented value for baseEvent will be ignored
within the comparison predicate.

int main(int argc, char* argv[])
{
set <Entrant, LoToHigh> myEntrant;
set <Entrant, LoToHigh>::iterator it;
Entrant a1, a2, a3;

a1.baseBib = 2077, a1.baseEvent = 'a';
a2.baseBib = 2066, a2.baseEvent = 'b';
a3.baseBib = 2055, a3.baseEvent = 'a';
myEntrant.insert(a1), myEntrant.insert(a2);
myEntrant.insert(a1), myEntrant.insert(a3);
cout << "The # of Entrants is " << myEntrant.size() << endl;
for(it = myEntrant.begin(); it != myEntrant.end(); it++)
{
cout << it->baseBib << "\t" << it->baseEvent << endl;
}
return 0;
}

Things to ponder:

a) Would std::map<> be better for this.
b) Are baseBib values guaranteed to be unique? std::set<> will purge
previous entries when equivalent entries arive.


Best,

Kai-Uwe Bux
 
M

Mike Copeland

I have the following application stub that compiles and works. Now I
Have you considered using a map rather than a set?

Briefly. Using a map seems a bit more complex in coding and
processing (I can't verify the latter), so I though I'd try a set for
this simple application need. (I don't know if it's faster or less
resource-intensive, and this is an investigational experiment.)
 
M

Mike Copeland

I have the following application stub that compiles and works. Now I
I don't understand the bit about scalar objects.

By that I mean, "a set of int" or "a set of char". My application
needs a structure. (Perhaps my usage of "scalar" isn't appropriate...)
8<{{
 
M

Mike Copeland

<sigh> I've decided that doing this as a set is to difficult and
convoluted. I changed it to a map, and it's working. Perhaps a lesson
learned... 8<{{
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top