473,692 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(cons t char *str):string(st r){}

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(co nst char *str):_str(stri ng(str))
{
}

CompositeStr(co nst CompositeStr& cs):_str(cs._st r){}

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<CompositeSt r> 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<CompositeSt r>::iterator itor2, endOfSet2 = cstrSet.end();

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

Jul 23 '05 #1
1 3222
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
7269
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 database field containing "somestring". Is there a way to avoid the need for the asterisks? This would make it easier for the users and also I wouldn't have to explain that asterisks are required when performing a wildcard search.
3
2879
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 past I believe if it wasn't an aspx file, things didnt work right. Can anyone provide input if this really is the case now?
2
2137
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 friendly names to various dynamic web pages). To do this, I've written an httphandler and put it in the web.config for my webapp (stored in the wwwroot\webapp directory). It works great for the most part; I get to handle any URLs below my webapp...
7
2539
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 mapping in IIS6 using the ASP.NET 2.0 ISAPI DLL. Here is what I am attempting and I cannot for the life of me figure this out. I have several situations:
2
2884
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 getting 404 error, not even the asp.net one, so I guess the IIS is blocking the request. I would uncheck to verify that file exists if it would be for special extension, but this is wildcarded so I don't know what to do... any solution?
6
3090
by: Jan Kucera | last post by:
Hi, does anybody know about wildcard mapping ASP.NET 2 in IIS6? Any tutorial? Thanks, Jan
1
3724
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 were originally created under VS.Net 2003. The proper version number is generated as expected, assembly version and file version of the compiled executable are both 3.0.xxxx.yyyyy.
11
22331
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 of code ... Any help much appreciated! Sample string : "the cat sat on the mat" matches : "the cat*" , "*cat*" , "*the mat"
2
3186
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 character is a percent sign. This works great if I place the % at the end of a search string, but I'm getting peculiar errors if I start the search string with the wildcard character. My search string seems to be altered at the front, and I can't...
0
8611
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8547
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9090
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8812
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8810
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7639
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6462
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2242
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.