By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,657 Members | 1,029 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,657 IT Pros & Developers. It's quick & easy.

Wildcard handling with STL

P: n/a
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;
}

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Generic Usenet Account wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.