472,371 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 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 3082
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.