STL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roland groenvynck

    #1

    STL

    I try to use STL to contruct two sets and make the unions of it , but
    my program keeps running in a loop when the union is tried :

    typedef set<int> TVSet;
    void CPSetDlg::OnBut ton1()
    {
    TVSet V1, V2, V3;
    V1.insert (1);
    V1.insert (2);

    V2.insert (1);
    V2.insert (3);



    set_union(V1.be gin(),
    V1.end() ,
    V2.begin(),
    V2.end(),
    V3.begin()
    );
    }

    Could someone help me?? thank you
  • Rob Williscroft

    #2
    Re: STL

    Roland groenvynck wrote in
    news:a952b245.0 404130232.678b2 f7f@posting.goo gle.com in comp.lang.c++:
    [color=blue]
    > I try to use STL to contruct two sets and make the unions of it , but
    > my program keeps running in a loop when the union is tried :
    >
    > typedef set<int> TVSet;
    > void CPSetDlg::OnBut ton1()
    > {
    > TVSet V1, V2, V3;
    > V1.insert (1);
    > V1.insert (2);
    >
    > V2.insert (1);
    > V2.insert (3);
    >
    >
    >
    > set_union(V1.be gin(),
    > V1.end() ,
    > V2.begin(),
    > V2.end(),
    > V3.begin()
    > );
    > }
    >
    > Could someone help me?? thank you
    >[/color]

    #include <iostream>
    #include <ostream>
    #include <set>
    #include <algorithm>
    #include <iterator>

    int main()
    {
    using namespace std;

    set< int > V1, V2, V3;
    V1.insert (1);
    V1.insert (2);

    copy( V1.begin(), V1.end(), ostream_iterato r< int >( cout, ", " ) );
    cout << '\n';

    V2.insert (1);
    V2.insert (3);

    copy( V2.begin(), V2.end(), ostream_iterato r< int >( cout, ", " ) );
    cout << '\n';

    set_union(
    V1.begin(), V1.end(),
    V2.begin(), V2.end(),
    ostream_iterato r< int >( cout, ", " )
    );
    cout << '\n';

    set_union(
    V1.begin(), V1.end(),
    V2.begin(), V2.end(),
    inserter( V3, V3.end() )
    );
    cout << '\n';

    copy( V3.begin(), V3.end(), ostream_iterato r< int >( cout, ", " ) );
    cout << '\n';
    }

    std::set_union( )'s 5th paramiter needs to be an output iterator,
    two examples above.

    Rob.
    --

    Comment

    • red floyd

      #3
      Re: STL

      SNT_Roland@hotm ail.com (Roland groenvynck) wrote in message news:<a952b245. 0404130232.678b 2f7f@posting.go ogle.com>...[color=blue]
      > I try to use STL to contruct two sets and make the unions of it , but
      > my program keeps running in a loop when the union is tried :
      >
      > typedef set<int> TVSet;
      > void CPSetDlg::OnBut ton1()
      > {
      > TVSet V1, V2, V3;
      > V1.insert (1);
      > V1.insert (2);
      >
      > V2.insert (1);
      > V2.insert (3);
      >
      >
      >
      > set_union(V1.be gin(),
      > V1.end() ,
      > V2.begin(),
      > V2.end(),
      > V3.begin()[/color]
      ^^^ -- wrong. Try this instead
      std::back_inser ter(V3.begin())[color=blue]
      > );
      > }
      >
      > Could someone help me?? thank you[/color]

      Comment

      • red floyd

        #4
        Re: STL

        redfloyd11@yaho o.com (red floyd) wrote in message news:<968b76.04 04130942.24e6db c4@posting.goog le.com>...[color=blue]
        > [proposed set_union problem redacted][/color]

        DISCLAIMER: I don't have my copy of the library reference here. I
        don't remember if std::set has a back_inserter.

        Comment

        • Kevin Goodsell

          #5
          Re: STL

          red floyd wrote:
          [color=blue]
          > redfloyd11@yaho o.com (red floyd) wrote in message news:<968b76.04 04130942.24e6db c4@posting.goog le.com>...
          >[color=green]
          >>[proposed set_union problem redacted][/color]
          >
          >
          > DISCLAIMER: I don't have my copy of the library reference here. I
          > don't remember if std::set has a back_inserter.[/color]

          No time to check right now, but it probably doesn't have a push_back()
          (why would it?). You probably want std::inserter for std::set.

          -Kevin
          --
          My email address is valid, but changes periodically.
          To contact me please use the address from a recent posting.

          Comment

          Working...