fast insertion to the std::map

R

Raider

Is there std::map member-function that do as code below?

typedef std::map<NameClass, ValueClass> ParameterContainer;
....

// this code is equivalent to "_Parameters[Name] = Value",
// but a bit faster for insertion

<code>

void SetParameter(const NameClass& Name, const ValueClass& Value)
{
ParameterContainer::iterator Existent = _Parameters.find(Name);
if (Existent != _Parameters.end())
Existent->second = Value;
else
_Parameters.insert(ParameterContainer::value_type(Name, Value));
}

</code>

PS. for insertion, "_Parameters[Name] = Value" calls default constructor
and than operator=, because it consists of two operations: operator[]
(which calls default constructor for ValueClass), than operator=. It's a
little bit slow for complicated ValueClass;
 
D

Dietmar Kuehl

Raider said:
Is there std::map member-function that do as code below?

No, there is a faster approach which locates the correct position
for insertion only once: associative containers have a method
'insert()' which takes the element to be inserted (in the case of
a 'std::map' this is actually a pair consisting of the key and the
value). The function returns a pair consisting of the element's
position and a bool which specifies whether the insertion took
place. To modify the value, you might need to catch the returned
pair, check the Boolean value, and update the value part of the
element specified by the iterator.
 
N

Neil Cerutti

Is there std::map member-function that do as code below?

typedef std::map<NameClass, ValueClass> ParameterContainer;
...

// this code is equivalent to "_Parameters[Name] = Value",
// but a bit faster for insertion

<code>

void SetParameter(const NameClass& Name, const ValueClass& Value)
{
ParameterContainer::iterator Existent = _Parameters.find(Name);
if (Existent != _Parameters.end())
Existent->second = Value;
else
_Parameters.insert(ParameterContainer::value_type(Name, Value));
}

</code>

PS. for insertion, "_Parameters[Name] = Value" calls default
constructor and than operator=, because it consists of two
operations: operator[] (which calls default constructor for
ValueClass), than operator=. It's a little bit slow for complicated
ValueClass;

How did you measure it? It seems unlikely that the above would be any
faster than operator[].
 
J

JE

Dietmar said:
No, there is a faster approach which locates the correct position
for insertion only once: associative containers have a method
'insert()' which takes the element to be inserted (in the case of
a 'std::map' this is actually a pair consisting of the key and the
value). The function returns a pair consisting of the element's
position and a bool which specifies whether the insertion took
place. To modify the value, you might need to catch the returned
pair, check the Boolean value, and update the value part of the
element specified by the iterator.

<snip>

(to OP)...and use map::lower_bound() so you have the right iterator to
pass to insert() as a hint, if you need to insert (i.e. return is end,
or if the key doesn't match your search key).

JE
 
R

Raider

How did you measure it? It seems unlikely that the above would be any
faster than operator[].

Just imagine that default constructor takes a lot of time to construct
object with some stable state, than operator= takes another time period
to erase all this data and fit the object by another data. Total - 2
periods.

My way is to use insert() which calls only copy constructor for a period
of time. Total - 1 period.

Raider
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top