setting values for all elements in hash to a value

M

mahurshi

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)
 
M

mlimber

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
 
M

mahurshi

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
 
R

Ron House

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 ... );
 
M

mlimber

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top