473,756 Members | 3,211 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fast insertion to the std::map

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

typedef std::map<NameCl ass, ValueClass> ParameterContai ner;
....

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

<code>

void SetParameter(co nst NameClass& Name, const ValueClass& Value)
{
ParameterContai ner::iterator Existent = _Parameters.fin d(Name);
if (Existent != _Parameters.end ())
Existent->second = Value;
else
_Parameters.ins ert(ParameterCo ntainer::value_ type(Name, Value));
}

</code>

PS. for insertion, "_Parameter s[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 4500
On 2006-02-24, Raider <sr*****@yandex .ru> wrote:
Is there std::map member-function that do as code below?

typedef std::map<NameCl ass, ValueClass> ParameterContai ner;
...

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

<code>

void SetParameter(co nst NameClass& Name, const ValueClass& Value)
{
ParameterContai ner::iterator Existent = _Parameters.fin d(Name);
if (Existent != _Parameters.end ())
Existent->second = Value;
else
_Parameters.ins ert(ParameterCo ntainer::value_ type(Name, Value));
}

</code>

PS. for insertion, "_Parameter s[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.co m> <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_boun d() 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
30083
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>); or is there a preferred/better method of doing this?
2
3405
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, MathFuncPtr> predefinedFunctions; // lots of other stuff void makeDictionary(){ predefinedFunctions=&F::f_sin(); } };
1
3553
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 program uses two MFC classes: CRect and CPoint which represents Rectangle and Point concepts (as usual) and a user
19
6163
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 found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
3
3714
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 can't think of a good fix, or even why it broke. The problem In one of my CFormView derived classes I have a member variable of the type...
1
6479
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 under Visual Studio .NET 2003 in a Win32 Console Project // VectorInsert.cpp : Defines the entry point for the console application / #include "stdafx.h #include "VectorInsert.h #ifdef _DEBU
13
9674
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
5378
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 work , what am i missing here. also is it possible/correct that the removeKeyValue function acquire the mutex lock only during the call to map.erase(), and not the during the whole iteration process as i have done here( i logically felt it was...
8
4074
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 search in a vector. Has anyone run into this before?
0
9872
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9713
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.