473,772 Members | 3,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sequence algorithm usage help

I've not really used sequence algorithms before and am having some
trouble interpreting usage from The C++ Programming Language
[Stroustrup].

The effect I'd like to achieve is to copy all entries (key and value)
from map<string, stringsource to dest - without overwriting any
entries in dest unless the keys match, ie:

for (map<string, string>::const_ iterator p = source.begin(); p !=
source.end(); ++p)
dest[p->first] = dest[p->second];

AFAICT copy would overwrite the first N elements of dest with those of
source, where N = source.size(). Is this correct? Is there an
appropriate sequence algorithm that will achieve the code shown above?

--Rob

Dec 5 '06 #1
8 1606
On Dec 5, 12:23 pm, "[rob desbois]" <rob.desb...@gm ail.comwrote:
I've not really used sequence algorithms before and am having some
trouble interpreting usage from The C++ Programming Language
[Stroustrup].

The effect I'd like to achieve is to copy all entries (key and value)
from map<string, stringsource to dest - without overwriting any
entries in dest unless the keys match, ie:
So you want to insert all elements in source into dest and overwrite
any elements in dest having the same key as in source? Try the
following:

dest.insert(sou rce.begin(), source.end());

--
Erik Wikström

Dec 5 '06 #2
On Dec 5, 12:23 pm, "[rob desbois]" <rob.desb...@gm ail.comwrote:
I've not really used sequence algorithms before and am having some
trouble interpreting usage from The C++ Programming Language
[Stroustrup].

The effect I'd like to achieve is to copy all entries (key and value)
from map<string, stringsource to dest - without overwriting any
entries in dest unless the keys match, ie:

for (map<string, string>::const_ iterator p = source.begin(); p !=
source.end(); ++p)
dest[p->first] = dest[p->second];

AFAICT copy would overwrite the first N elements of dest with those of
source, where N = source.size(). Is this correct? Is there an
appropriate sequence algorithm that will achieve the code shown above?
The code above have a quite peculiar function, it finds the element in
dest that have the key p->second and assigns the value of this element
to the element in dest that has the key p->fist.

You might have meant to do dest[p->first] = p->second, which would
create (or ovrewrite) a new element in dest with key p->fist and value
p->second, which is the same as copying the element p points to into
dest. Another way would be to use dest.insert(*p) , if you just want to
insert a single element. To copy all elements see my other post.

--
Erik Wikström

Dec 5 '06 #3
map's insert(iter, iter) operation won't overwrite entries where the
key already exists, it will only add new entries where the key does not
exist.

--Rob

Dec 5 '06 #4
er****@student. chalmers.se wrote:
for (map<string, string>::const_ iterator p = source.begin(); p !=
source.end(); ++p)
dest[p->first] = dest[p->second];

The code above have a quite peculiar function, it finds the element in
dest that have the key p->second and assigns the value of this element
to the element in dest that has the key p->fist.

You might have meant to do dest[p->first] = p->second, which would
create (or ovrewrite) a new element in dest with key p->fist and value
p->second, which is the same as copying the element p points to into
dest. Another way would be to use dest.insert(*p) , if you just want to
insert a single element. To copy all elements see my other post.
Whoops - yes, dest[p->first] = p->second was what I meant to write.
Regarding copying all elements *with overwrite*, see my previous post.

Dec 5 '06 #5
On Dec 5, 1:02 pm, "[rob desbois]" <rob.desb...@gm ail.comwrote:
map's insert(iter, iter) operation won't overwrite entries where the
key already exists, it will only add new entries where the key does not
exist.
Seems like you are right, we learn new things every day. You can
however use something like this:

for (std::map<int, int>::iterator p = src.begin(); p != src.end(); ++p)
dst.insert(*p);

--
Erik Wikström

Dec 5 '06 #6
er****@student. chalmers.se wrote:
for (std::map<int, int>::iterator p = src.begin(); p != src.end(); ++p)
dst.insert(*p);
This is almost equivalent to my code (although better - hence I would
use it in preference).
The reason I have posted is to see if there is a STL algorithm for this
purpose so I can remove the need for an explicit loop.

--Rob

Dec 5 '06 #7
On Dec 5, 1:31 pm, "[rob desbois]" <rob.desb...@gm ail.comwrote:
eri...@student. chalmers.se wrote:
for (std::map<int, int>::iterator p = src.begin(); p != src.end(); ++p)
dst.insert(*p); This is almost equivalent to my code (although better - hence I would
use it in preference).
The reason I have posted is to see if there is a STL algorithm for this
purpose so I can remove the need for an explicit loop.
You can find what I believe to be a complete list of the STL algorithms
here: http://www.cppreference.com/cppalgorithm/index.html, the only one
I saw that might work is copy, but you've already knew of that one.

--
Erik Wikström

Dec 5 '06 #8
I have discovered someone else's solution to this:
source.insert(d est.begin(), dest.end());
dest.swap(sourc e);

It copies *without overwrite* to the intended source map, then simply
swaps the contents of both maps. Obviously if the source map was not
intended to be modified a copy will need to be made somewhere along the
line, but otherwise it's an effective mechanism, especially as it still
only involves a single implicit loop in the copy, and the simple fast
swap() operation.

Thanks for your input Erik

Dec 5 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1944
by: Joh | last post by:
hello, here is my trouble, i would to like to write a program which could help me to detect sequence of consecutive words in list in a very efficient way. (i need to do it upon large amount of text, and for now i'm looking for a good start point) i have many "sentences" of variables sizes, for example one could be : sentence1 = where wx can be seen as a word.
7
2807
by: Séb | last post by:
Hi everyone, I'm relatively new to python and I want to write a piece of code who do the following work for data mining purpose : 1) I have a list of connexion between some computers. This list has this format : Ip A Date Ip B .... ... ...
18
1956
by: Andy Green | last post by:
Emphasis is on efficiancy and speed this is the excercise: The program should monitor a possibly infinite stream of characters from the keyboard (standard input). If it detects the sequence "aaa" it outputs a "0". If it detects the sequence "aba" it outputs a "1". DO NOT detect sequences within sequences. The program should exit cleanly when it detects an End Of Input. For example: The following sequence aababaaabaaa<End Of Input>...
5
2234
by: sandravandale | last post by:
I want to be able to pass a sequence (tuple, or list) of objects to a function, or only one. It's easy enough to do: isinstance(var, (tuple, list)) But I would also like to accept generators. How can I do this? Anything else is assumed to be a single value (my fault if I pass a
6
2067
by: rhcarvalho | last post by:
Hello there! I'm trying to make a simple Contact Manager using python (console only), however i'm having trouble implementing a division by "Groups" or "Labels" just like in Gmail. I don't have any real code to post because all i got now is a raw TXT file holding the names and phones of my contacts. The only idea I could figure out until now seems too weak, so that's why i'm asking for help. I thought of making a separate list (in a...
8
5654
by: Daneel | last post by:
Hello! I'm looking for an algorithm which finds all occurences of a bit sequence (e.g., "0001") in a file. This sequence can start at any bit in the file (it is not byte aligned). I have some ideas of how to approach the problem (1) reading file into unsigned char buffer, 2) defining bit structure, 3) comparing the first 4 bits of the buffer with the bit structure, 4) shifting the char buffer one left, 5) repeat at step 3)) but I'm...
5
2163
by: majestik666 | last post by:
Am not sure if it's been asked before (did a search but didn't come up with anything related...). I'm trying to write some code to "collapse" image sequences into a single item , i.e.: image.1.exr image.2.exr .... image.100.exr
8
2350
by: Hahnemann | last post by:
Is there a sample in C out there to represent a sequence of numbers (shorts) using bits? With the intention of saving space, instead of using the full 16 bits per short. Thanks. - Hahnemann
0
9620
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10104
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...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9912
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...
1
7460
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
6715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.