Wildcard handling with STL

  • Thread starter Generic Usenet Account
  • Start date
G

Generic Usenet Account

Here's the requirement that I am trying to satisfy:

Handle wildcards in STL such that the wildcard entry is encountered
before non-wildcard entries while traversing containers like sets and
maps.

I have come up with two approaches, one using inheritance and the other
using composition. I am unable to decide which one is better.

The sample source code follows.

Thanks,
Gus

///////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <set>
using namespace std;

#define wildcard "Honduras"


/////////////////////////////////////////////////////////
// Class 1 (Using inheritance)
////////////////////////////////////////////////////////
class DerivedStr : public string
{
public:
DerivedStr(const char *str):string(str){}

bool operator<(const DerivedStr& otherStr) const
{
#ifdef DEBUG
cout << "DerivedStr::" <<__func__ << endl;
#endif

bool status = true;
if(*this != wildcard)
status = (strcmp(data(), otherStr.data()) < 0);

return status;
}
};


/////////////////////////////////////////////////////////
// Class 2 (Using composition)
////////////////////////////////////////////////////////
class CompositeStr
{
string _str;
public:
CompositeStr(const char *str):_str(string(str))
{
}

CompositeStr(const CompositeStr& cs):_str(cs._str){}

bool operator<(const CompositeStr& otherStr) const
{
#ifdef DEBUG
cout << "CompositeStr::" << __func__ << endl;
#endif

bool status = true;

if(_str != wildcard)
status = (_str < otherStr._str);

return status;
}

string data() const
{
return _str;
}
};


main()
{
//
// Derivation case
//
set<DerivedStr> dstrSet;

dstrSet.insert("Hello_DS");
dstrSet.insert("Apple_DS");
dstrSet.insert("Zombie_DS");
dstrSet.insert(wildcard);
cout << "dstrSet has " << dstrSet.size() << " entries\n\n";

set<DerivedStr>::iterator itor, endOfSet = dstrSet.end();

for(itor = dstrSet.begin(); itor != endOfSet; itor++)
cout << "It is " << itor->data() << endl;


//
// Composition case
//
set<CompositeStr> cstrSet;

cstrSet.insert("Hello_CS");
cstrSet.insert("Apple_CS");
cstrSet.insert("Zombie_CS");
cstrSet.insert(wildcard);
cout << "cstrSet has " << cstrSet.size() << " entries\n\n";

set<CompositeStr>::iterator itor2, endOfSet2 = cstrSet.end();

for(itor2 = cstrSet.begin(); itor2 != endOfSet2; itor2++)
cout << "It is " << itor2->data() << endl;
}
 
K

Karl Heinz Buchegger

Generic said:
Here's the requirement that I am trying to satisfy:

Handle wildcards in STL such that the wildcard entry is encountered
before non-wildcard entries while traversing containers like sets and
maps.

I have come up with two approaches, one using inheritance and the other
using composition. I am unable to decide which one is better.

Neither is better.
The way to go is to tell a std::set or a std::map that you want a different
sorting strategy. Both classes have an additional template parameter
to do just that.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top