473,231 Members | 1,654 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,231 software developers and data experts.

std:map insert question

Hi all,

I'm starting to fool around with STL and in particular std::map.

How do I iterate through one map and insert every pair in another map?

I have the following so far:

map<double, doublefset1;
map<double, doublefset3;

fset1.insert(pair<double, double>(1.0,0.4));
// etc etc more values etc etc

ifs1 = fset1.begin();
while(ifs1 != fset1.end())
{
// how to insert is the q!
}
Thanks for your time!
Evyn

Jan 19 '07 #1
4 6215
Evyn wrote:
Hi all,

I'm starting to fool around with STL and in particular std::map.

How do I iterate through one map and insert every pair in another map?
By using std::copy.
I have the following so far:

map<double, doublefset1;
map<double, doublefset3;

fset1.insert(pair<double, double>(1.0,0.4));
fset1.insert(make_pair(1.0,0.4));
// etc etc more values etc etc

ifs1 = fset1.begin();
while(ifs1 != fset1.end())
{
// how to insert is the q!
fset3.insert(*ifs1);
++ifs1;
}
Jan 19 '07 #2
Evyn wrote:
Hi all,

I'm starting to fool around with STL and in particular std::map.

How do I iterate through one map and insert every pair in another map?

I have the following so far:

map<double, doublefset1;
map<double, doublefset3;

fset1.insert(pair<double, double>(1.0,0.4));
// etc etc more values etc etc

ifs1 = fset1.begin();
while(ifs1 != fset1.end())
{
// how to insert is the q!
}
std::copy( fset1.begin(), fset1.end(),
std::inserter( fset2, fset2.begin() ) );
E.g.:

#include <map>
#include <algorithm>
#include <iterator>
#include <iostream>

int main ( void ) {
std::map< int, int a;
std::map< int, int b;
b[2] = 1;
b[4] = 2;
a[3] = 0;
a[5] = 2;

std::copy( a.begin(), a.end(),
std::inserter( b, b.begin() ) );

for ( std::map< int, int >::const_iterator iter = b.begin();
iter != b.end(); ++iter ) {
std::cout << iter->first
<< " --"
<< iter->second
<< "\n";
}
}
Best

Kai-Uwe Bux
Jan 19 '07 #3
Thanks for the help!

On Jan 19, 2:37 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Evyn wrote:
Hi all,
I'm starting to fool around with STL and in particular std::map.
How do I iterate through one map and insert every pair in another map?
I have the following so far:
map<double, doublefset1;
map<double, doublefset3;
fset1.insert(pair<double, double>(1.0,0.4));
// etc etc more values etc etc
ifs1 = fset1.begin();
while(ifs1 != fset1.end())
{
// how to insert is the q!
}std::copy( fset1.begin(), fset1.end(),
std::inserter( fset2, fset2.begin() ) );

E.g.:

#include <map>
#include <algorithm>
#include <iterator>
#include <iostream>

int main ( void ) {
std::map< int, int a;
std::map< int, int b;
b[2] = 1;
b[4] = 2;
a[3] = 0;
a[5] = 2;

std::copy( a.begin(), a.end(),
std::inserter( b, b.begin() ) );

for ( std::map< int, int >::const_iterator iter = b.begin();
iter != b.end(); ++iter ) {
std::cout << iter->first
<< " --"
<< iter->second
<< "\n";
}

}Best

Kai-Uwe Bux- Hide quoted text -- Show quoted text -
Jan 19 '07 #4

Evyn wrote:
Hi all,

I'm starting to fool around with STL and in particular std::map.

How do I iterate through one map and insert every pair in another map?

I have the following so far:

map<double, doublefset1;
map<double, doublefset3;
fset1.insert(fset3.begin(), fset3.end());

Jan 19 '07 #5

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

Similar topics

24
by: Duane Hebert | last post by:
2 questions: Given a map defined as std::map<int,string> stringmap; //How do you access the data_type (string) via an iterator? std::string spoo("doh");
44
by: jmoy | last post by:
I am a C programmer graduating to C++. As an exercise I wrote a program to count the number of times that different words occur in a text file. Though a hash table might have been a better choice,...
14
by: Flzw | last post by:
Well I have a map like this : std::map <string, CObject> ObjectList; I have a function like this : CObject* NewObject( char* Name, CArg* Arg) { std::string key = Name; ObjectList =...
0
by: Erik Arner | last post by:
Hi, let's say I have a std::map<std::string,int> and I want to search the map for all keys that start with "foo". The regexp equivalent is to search for "foo*", or perhaps "^foo*". At present...
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...
10
by: Jim Langston | last post by:
Expected output of program: Key is: 0 String is: Hello Key is: 1 String is: Goodbye Key is: 2 String is: The end Actual output: Key is: 0 String is: The End Key is: 1 String is: Key is: 2...
1
by: simon.elbaz | last post by:
Hi, i use the following map: std::map<Key, std::vector< std::string >, KeyCmp mSQLElem; The following code does not insert the newly created pair: std::vector<std::stringvcTemp;...
7
by: guido | last post by:
Hi, I'm looking for a container class that can map whole ranges of keys to objects - something like std::map, but not only for individual values for the key, but for whole ranges. Example: I...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.