473,396 Members | 2,010 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,396 software developers and data experts.

fast insertion to the std::map

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
4 4478
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
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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Woodster | last post by:
I have declared the following std::map<std::string, std::string> myMap; to pass myMap to functions should I be declaring functions as: void function(std::map<std::string, std::string>); ...
2
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string,...
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
2
by: digz | last post by:
Hi, I am trying to write a program which has two threads one of them write to a map , and the other one deletes entries based on a certain criterion.. first I cannot get the delete portion to...
8
by: mveygman | last post by:
Hi, I am writing code that is using std::map and having a bit of an issue with its performance. It appears that the std::map is significantly slower searching for an element then a sequential...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.