std:map insert question

Discussion in 'C++' started by Evyn, Jan 19, 2007.

  1. Evyn

    Evyn Guest

    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
    Evyn, Jan 19, 2007
    #1
    1. Advertising

  2. Evyn

    Rolf Magnus Guest

    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;

    > }
    Rolf Magnus, Jan 19, 2007
    #2
    1. Advertising

  3. Evyn

    Kai-Uwe Bux Guest

    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
    Kai-Uwe Bux, Jan 19, 2007
    #3
  4. Evyn

    Evyn Guest

    Thanks for the help!

    On Jan 19, 2:37 pm, Kai-Uwe Bux <> 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 -
    Evyn, Jan 19, 2007
    #4
  5. Evyn

    dasjotre Guest

    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());
    dasjotre, Jan 19, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,255
    Ivan Vecerina
    Mar 17, 2005
  2. Replies:
    1
    Views:
    431
  3. Replies:
    1
    Views:
    402
    red floyd
    Dec 21, 2008
  4. Thomas J. Gritzan
    Replies:
    6
    Views:
    993
    James Kanze
    Dec 22, 2008
  5. James Kanze
    Replies:
    0
    Views:
    1,975
    James Kanze
    Dec 21, 2008
Loading...

Share This Page