473,416 Members | 1,584 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,416 software developers and data experts.

Wildcard handling with STL

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
1 3199
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: deko | last post by:
I have a form where users can enter a string with asterisks to perform a wildcard search. Currently, the string entered by the user looks like this: *somestring* The purpose is to match any...
3
by: Adam | last post by:
Its my understanding that in asp.net 2.0 you can write an httpmodule that will acts as a wildcard handler and pass requests on to the proper engine, for instance, asp or perl for example, In the...
2
by: Ken Yee | last post by:
First a little background: I've written an httphandler to handle wildcard extensions (i.e., I want to handle all URLs that come in rather than just URLs w/ a specific file extension so I can give...
7
by: SlimFlem | last post by:
I have searched hard for 2 days on this and keep hitting a wall. I have a custom IHttpHandler setup to do Url mappings to prop up some old Urls for our site. I have also created a wildcard...
2
by: Jan Kucera | last post by:
Hi, I have virtual MyFolder/* and MyFolder/MySubFolder/* mapped to an httphandler in the web.config file and all works perfectly on the asp.net development server. However on the IIS6/Win2003 I'm...
6
by: Jan Kucera | last post by:
Hi, does anybody know about wildcard mapping ASP.NET 2 in IIS6? Any tutorial? Thanks, Jan
1
by: Lucvdv | last post by:
In my assembly.vb files, I'm using the revision/build wildcard style: <Assembly: AssemblyVersion("3.0.*")> <Assembly: AssemblyFileVersion("3.0.*")> This onkly seems to work in projects that...
11
by: google | last post by:
I need a simple wildcard pattern matching function written in JS. I have wrestled with regular expresions but frankly am struggling to come up with anything less than an epic function of many lines...
2
by: Arch Stanton | last post by:
I have an aspx page with a text box. My user enters text to search for and presses a button; the text is passed via a QueryString to another aspx page and used in a SQL search. The wildcard...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.