Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   std:map insert question (http://www.velocityreviews.com/forums/t459923-std-map-insert-question.html)

Evyn 01-19-2007 11:46 AM

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, 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


Rolf Magnus 01-19-2007 12:34 PM

Re: std:map insert question
 
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, 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;

> }



Kai-Uwe Bux 01-19-2007 12:37 PM

Re: std:map insert question
 
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, 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

Evyn 01-19-2007 01:08 PM

Re: std:map insert question
 
Thanks for the help!

On Jan 19, 2:37 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> 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, 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 -



dasjotre 01-19-2007 01:29 PM

Re: std:map insert question
 

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


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



All times are GMT. The time now is 05:52 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.