Hi all,
Suppose I want to implement a dictionary data structure of some kind,
say using a simple BST. I'm wondering how to express the basic
operations in the nicest manner. Especially the operations FIND, INSERT
and DELETE. I'm aware of the way STL does it, but I don't want to use
iterators.
The dictionary has a key/value structure:
template <class KEY_T, class DATA_T>
class bstree
....
....
My operations are declared as follows:
// Try to find the key in the tree. If found, a pointer
// to the data is returned, else 0 is returned
//
const DATA_T* bstree::find(co nst KEY_T& key)
// Insert a new key/data pair into the tree.
// True is returned if such a key already existed (and
// was replaced), false otherwise
//
bool bstree::insert( const KEY_T& key, const DATA_T& data)
// Remove a key from the tree. True is returned if such
// a key was found (and removed), false otherwise
//
bool bstree::remove( const KEY_T& key)
My biggest concern is with "find", naturally, because it
returns a pointer to the data. I couldn't use a reference
because I want to be able to test via the return value also
whether the element was found at all (now just testing the
pointer with 0).
I know there are several ways to implement it, but I wonder
at what's the best. I could, for instance, make it:
bool bstree::find(co nst KEY_T& key, DATA_T& data)
And return true (and fill the data) if found, false otherwise.
But this seems more cluttered to me - first because the caller
has to pass another argument, and second because it's unclear
what to place in "data" if nothing is found.
As I mentioned, STL solves these problems by using iterators.
But I want an implementation without iterators, so a constant
pointer seems like the safest way (my dictionary "owns" the
memory for the key/data, just like STL's containers do).
Any ideas ?
Thanks in advance
Eli 0 1356 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Paul Moore |
last post by:
I can't find anything which spells this out in the manuals. I guess
that, at some level, the answer is "a single bytecode operation", but
I'm not sure that explains it for me.
This thought was triggered by a comment on the Python Cookbook site,
which basically said that it was OK to do
tss = {}
...
id = thread.get_ident()
tss = {}
|
by: I am dog |
last post by:
Hi ,
I use VS 2005 beta1 to do some thing as follows.
declare a Dictionary<ulong,DelegateTyep> dictionary = new ...
and use dictionary object such as
dictionary += aDelegateTyepObject;
|
by: ESPNSTI |
last post by:
Hi,
I'm trying to use a generics dictionary with a key class that implements and
needs IComparable<>.
However when I attempt to use the dictionary, it doesn't appear to use the
IComparable<> to find the key.
In the example below, accessing the dictionary by using the exact key object
that was used to add to the dictionary works.
(see code...
|
by: Robert McCall |
last post by:
Greetings All,
Does anyone have any idea why a method signature does not
include the return type?
Example:
int someFct( int a, int b );
is the same signature as
|
by: Martin Widmer |
last post by:
Hi Folks.
When I iterate through my custom designed collection, I always get the
error:
"Unable to cast object of type 'System.Collections.DictionaryEntry' to type
'ContentObjects.ContentBlock'."
The error occurs at the "For...Each" line if this method:
| |
by: Bill Woodruff |
last post by:
<
note : this message was sparked in part by comments by David Browne on a
previous thread : "inserting an anonymous method as a value in a generic
dictionary ?" : David had shown the use of 'Delegate as a valid Type
declaration for the Value of a Generic Dictionary.
I am curious as to why I can compile and use this syntax : it seems to...
|
by: wildThought |
last post by:
If I have an object that contains a generic dictionary inside of it,
how do I get access to its properties such as count?
|
by: digicrowd |
last post by:
http://bytes.com/images/howtos/applemail_sig_icon.jpg
At first glance, it may not appear that Apple Mail (otherwise known as Mail.app) supports the use of HTML signature emails. However, with a little terminal and Safari magic, you will be sending HTML signatures.
1. Prepare your HTML signature.
Open TextEdit or your favorite HTML editor and...
|
by: sachin2 |
last post by:
I am using 3 types of dictionaries.
1) Dictionary<string, string > d = new Dictionary<string, string>();
2) Dictionary<string, List<string>> d = new Dictionary<string, List<string>>();
3) Dictionary<string, Dictionary<string, string>> d = new Dictionary<string, Dictionary<string, string>>();
Now I am using GetDictionaryType Function...
|
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...
|
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...
| |
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. ...
|
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...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |