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

setting values for all elements in hash to a value

i have a hash that's populated with some keys/values
i did some calculations (logic simulations) and and set the values.

now i want to set all the values to "-1" (the keys remain the same)
so that i can simulate my circuit with a totally new input vector and
recalculate the values of different nets and fill up the hash again.

right now, i am doing this by going thru all the keys in the hash and
setting each value to -1 (i.e. right now i have a way to work it out)

just curious.. is there a simpler/better way to do this ? (like in just
1 line or something)

Sep 15 '05 #1
4 1253
ma******@gmail.com wrote:
i have a hash that's populated with some keys/values
i did some calculations (logic simulations) and and set the values.

now i want to set all the values to "-1" (the keys remain the same)
so that i can simulate my circuit with a totally new input vector and
recalculate the values of different nets and fill up the hash again.

right now, i am doing this by going thru all the keys in the hash and
setting each value to -1 (i.e. right now i have a way to work it out)

just curious.. is there a simpler/better way to do this ? (like in just
1 line or something)


You might be able to use a standard library function, but you didn't
tell us if you are using a standard container for your hash or if you
have created your own. If the latter, does it support standard
iterators?

Cheers! --M

Sep 15 '05 #2
i am using

map <string, int>

and i think it has itererators. thats what i am currently usng to fill
it up with -1

but i was wondering if there is a way to do it without going thru the
loop

Sep 15 '05 #3
ma******@gmail.com wrote:
i am using

map <string, int>

and i think it has itererators. thats what i am currently usng to fill
it up with -1

but i was wondering if there is a way to do it without going thru the
loop


do {
map <string, int> mymap;
... use the map ...
} while (...I want to do another run ... );

--
Ron House ho***@usq.edu.au
http://www.sci.usq.edu.au/staff/house
Sep 16 '05 #4
ma******@gmail.com wrote:
i am using

map <string, int>

and i think it has itererators. thats what i am currently usng to fill
it up with -1

but i was wondering if there is a way to do it without going thru the
loop


Well, you could simply use a function to hide the loop:

template<class Key, class Value>
inline void ZapValues( map<Key,Value>& m, const Value& val )
{
for( map<Key,Value>::iterator i=m.begin(); i != m.end(); ++i )
i->second = val;
}

Or you could use a standard algorithm like std::for_each in conjunction
with a functor:

#include <algorithm>
#include <map>

template<class Key, class Value>
struct Zapper
{
const Value val_;

Zapper( const Value& val ) : val_(val) {}

void operator()( pair<Key,Value>& it )
{
it.second = val_;
}
};

template<class Key, class Value>
void Zap( map<Key, Value>& m, const Value& val )
{
for_each( m.begin(), m.end(), Zapper<const Key,Value>( val ) );
}

// Invoke it like this:
Zap( m, -1 );

The first generally seems like the preferred option since it involves
less code and its meaning is more obvious to those who might look at
your code later (including you).

Cheers! --M

Sep 16 '05 #5

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

Similar topics

4
by: P1ayboy | last post by:
Does anyone know if it is possible to collect all the values of a form and place them into a Hash table. Would save a lot of time referencing the form elements for their value!
12
by: daniel kaplan | last post by:
Hi All, Been learning Javascript (via google) to create forms with pre-set values. As seen below. My problem I have found is this: I can't seem to figure out (or find so far via google) how to...
1
by: Maziar Aflatoun | last post by:
Hi, I have the lKey object that can be used to enable different operation systems such Windows 2000 Linux FreeBSD etc. Now I want to read a database table that contains key/values and if...
22
by: Ben Finney | last post by:
Howdy all, I've recently packaged 'enum' in PyPI. In its description, I make the claim that it creates "immutable" enumeration objects, and that the enumeration values are "constant" values. ...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
13
by: Iris83 | last post by:
Hi, I have a question about converting some of the data in my dataset but leave some data the way it is. I have a hash and if the key is present and the Value of the Hash equals B it should...
1
by: Wayne Deleersnyder | last post by:
Hi All, I was going to write and ask if someone could help me fix the formatting of my output for hash values, but I believe I got it right now. But, because I couldn't find any website or...
1
by: | last post by:
Hello all, I have a question which might be simple or need some work around. I want to do something like this. My class/instance has a dict as a property. I want the instance to catch the...
9
Thekid
by: Thekid | last post by:
Hi, I can't seem to figure this out. Here's my objective: I have a value that is an md5 hash and I have a wordlist. I need to md5 the words in the list, then compare them to the given hash, then have...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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,...
0
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...
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
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.