469,081 Members | 1,426 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,081 developers. It's quick & easy.

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 5894
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

24 posts views Thread by Duane Hebert | last post: by
44 posts views Thread by jmoy | last post: by
14 posts views Thread by Flzw | last post: by
reply views Thread by Erik Arner | last post: by
19 posts views Thread by Erik Wikström | last post: by
10 posts views Thread by Jim Langston | last post: by
1 post views Thread by simon.elbaz | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.