473,796 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HashTable again

I've had a lot of help, but I need a little more.

Here's a snippett of code:
template <typename K,typename V>
class HashTable : private std::map<K, V>
{
private:

public:
bool Keys(K& key, bool firstp = false) const
{
static typename std::map<K,V>:: iterator map_iter;
if (firstp) map_iter = this->begin(); <<<<<<<<<<<<LIN E #81
if (map_iter == this->end()) return false;
key = map_iter->first;
++map_iter;
return true;
}
I need to call this method until it has hit all the keys. Thst's why I declare
map_iter as static.
HashTable<int,s td::string> h;
int i;
bool first=true;
while (h.Keys(i,first ))
{
first = false;
do_something_wi th_i();
}

Here's my actual test code:
void t_HashTable(voi d)
{
ENTER
HashTable<int,s td::string> h;
pass(0 EQ h.EntryCount() , "verify hash is empty");
pass(true EQ h.Put(1,"a") , "put a into 1");
pass(1 EQ h.EntryCount() , "verify hash hasn 1 entry");
pass(h.Value(1) EQ "a" , "Verify value of 1 is a");
int i;
pass(h.Keys(i,t rue) , "Get first key"); <<<<<<< LINE# 142
pass(i EQ 1 , "Verify only key is 1");

LEAVE
}
Here's my compiler message:
x.cxx: In member function `bool HashTable<K, V>::Keys(K&, bool) const [with K =
int, V = std::string]':
x.cxx:142: instantiated from here
x.cxx:81: error: no match for 'operator=' in 'map_iter = ((const std::map<int,
std::string, std::less<int>, std::allocator< std::pair<const int, std::string> >
*)((const HashTable<int, std::string>*)t his))->std::map<_Ke y, _Tp, _Compare, _Alloc>::begin [with _Key = int, _Tp = std::string, _Compare = std::less<int>,
_Alloc = std::allocator< std::pair<const int, std::string> >]()'
/apps/gcc/3.4.1/lib/gcc/i686-pc-linux-gnu/3.4.1/../../../../include/c++/3.4.1/bits/stl_tree.h:152:
note: candidates are: std::_Rb_tree_i terator<std::pa ir<const int, std::string>& std::_Rb_tree_i terator<std::pa ir<const int, std::string> >::operator=(co nst

std::_Rb_tree_i terator<std::pa ir<const int, std::string> >&)

___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodology Group
Dallas, Texas, 214-480-4455, b-******@ti.com
Jul 22 '05 #1
2 3622
Billy Patton wrote:
I've had a lot of help, but I need a little more.

Here's a snippett of code:
template <typename K,typename V>
class HashTable : private std::map<K, V>
{
private:

public:
bool Keys(K& key, bool firstp = false) const
{
static typename std::map<K,V>:: iterator map_iter;
if (firstp) map_iter = this->begin(); <<<<<<<<<<<<LIN E #81
Since 'Keys' is a const function, only a const 'begin' can be called.
The const 'begin' returns 'const_iterator ' value, not 'iterator'.
Declare your 'map_iter' like this:

static typename std::map<K,V>:: const_iterator map_iter;
if (map_iter == this->end()) return false;
key = map_iter->first;
++map_iter;
return true;
}
[...]

Jul 22 '05 #2
Billy Patton wrote:

I've had a lot of help, but I need a little more.

In addition to Victor's help:

What you are trying to do is an extremely bad idea.
It prevents the user of this class to have 2 HashTable
objects and iterate through them at the same time. Why?
Simple: Because there is only *1* iteration variable when
2 are needed.
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
13888
by: shine | last post by:
I am trying to add an integer array to a hashtable. While adding to hashtable I want to check the duplicates in array, the way I want to do is add the key(the integer stored in array) if a duplicate key is being added then the program should return the integer else it should return -1. Here is the piece of code I have written does the logic looks ok ? ------------------------------------------------------------------ class...
7
14748
by: davidw | last post by:
I always use NameValueCollection. But I read an article says the only differece between Hashtable and NameValueCollection is that NameValueCollection could accept more than one value with same key? I am not quite understand this. And I think NameValueCollection's Set is a nice method. If I use HashTable, how can I update a item in it? Do I have to check if it exists, and remove and add it again? Thanks.
5
2832
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would like it to return an XML serialized version of an object.
7
6889
by: Joseph Lee | last post by:
Hi All, I am having problem when i am using hashtable to keep an array of bytes value as keys. Take a look at the code snippet below --------------------------------------------------- ASCIIEncoding asciiEncoder = new ASCIIEncoding();
33
3324
by: Ken | last post by:
I have a C# Program where multiple threads will operate on a same Hashtable. This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable) method, so no further Lock statements are used before adding, removing or iterating the Hashtable. The program runs in a high workload environment. After running a few days, now it suddenly catchs this Exception when inserting a pair of key and object, stacktrace =...
8
1691
by: Robin Tucker | last post by:
When I create a hashtable hashing on Object-->Item, can I mix "string" and "integer" as the key types? I have a single thumbnail cache for a database with (hashed on key) and a file view (hashed on string). So, for example, when I want to know if the file "xyz.abc" is in the cache, I can write myTable.ContainsKey ( "xyz.abc" ) or if a given DB key is in the hash I can write myTable.ContainsKey ( 123 ). Or do the keys all have to be of...
7
1707
by: J L | last post by:
I have defined a structure private structure FieldInfo dim FieldName as string dim OrdinalPostioin as Integer dim DataType as Type dim Size as Integer end structure I read this information from a DataReader which retrieves schema info
9
4649
by: Nick Vaughan | last post by:
While running some long term tests on our Broadcast SDK one of our thread dropped out because an exception had been thrown: Exception: System.ArgumentOutOfRangeException Message: Load factor needs to be between 0.1 and 1. Parameter name: loadFactor Source: mscorlib at System.Collections.Hashtable..ctor(Int32 capacity, Single loadFactor, IHashCodeProvider hcp, IComparer comparer) at System.Collections.Hashtable..ctor()
3
5975
by: Manish | last post by:
Hi m not able to convert the node values of a XML file to Hashtable Actaully i need to traverse the XML Nodes again and again and want to pick values on filters what i want is to stroe the XML values in the Hashtable and return then from Hashtable (the hashtable will be filled only once) this will optimise the performance because the XML is not updated (or updated rarely)
2
2467
by: Bruce | last post by:
Hi I am having a problem understanding the exact advantages of using an ArrayList over a Hashtable in any situation. In most areas of an application I am working on, lookup needs to be fast. If I use a hashtable Addition - O(1) - same as ArrayList Removal - O(1) - same as ArrayList Lookup- O(1) - ArrayList is O(n) Memory - Maybe slightly more than ArrayList since we have a hash to store.
0
9685
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
9535
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
10465
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
10200
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
9061
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
7558
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
6800
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3744
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.