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;
}
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;
}