std:map insert question

E

Evyn

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, double> fset1;
map<double, double> fset3;

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
 
R

Rolf Magnus

Evyn said:
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, double> fset1;
map<double, double> fset3;

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;
 
K

Kai-Uwe Bux

Evyn said:
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, double> fset1;
map<double, double> fset3;

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
 
E

Evyn

Thanks for the help!

Evyn said:
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, double> fset1;
map<double, double> fset3;
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 -
 
D

dasjotre

Evyn said:
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, double> fset1;
map<double, double> fset3;

fset1.insert(fset3.begin(), fset3.end());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top