By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,725 Members | 1,040 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,725 IT Pros & Developers. It's quick & easy.

fast insertion to the std::map

P: n/a
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;
Feb 24 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 2006-02-24, Raider <sr*****@yandex.ru> wrote:
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[].

--
Neil Cerutti
It isn't pollution that is hurting the environment; it's the
impurities in our air and water that are doing it. --Dan Quayle
Feb 24 '06 #2

P: n/a
Raider wrote:
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.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 24 '06 #3

P: n/a
JE
Dietmar Kuehl wrote:
Raider wrote:
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.


<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

Feb 24 '06 #4

P: n/a
> 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
Feb 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.