Thread: Mutiset STL

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    Mutiset STL

    hi..
    i am facing a prob as shown below



    it=find(value) and erase(it) erases both the values(in case of repeating values)... but how do i erase only one of the two values

    thanks

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Does it?

    Code:
    #include <set>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
        multiset<int> s;
        s.insert(2);
        s.insert(3);
        s.insert(1);
        s.insert(2);
        copy(s.begin(), s.end(), ostream_iterator<int>(cout, " ")); cout << '\n';
        s.erase(s.find(2));
        copy(s.begin(), s.end(), ostream_iterator<int>(cout, " ")); cout << '\n';
    }
    1 2 2 3
    1 2 3
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    Smile

    no insertion is done in right way..(tat s the neccesity for multiset)
    eg:
    if i insert
    1 2 2 3 3 4 5 6 7 8 8 9
    its done
    then my requirement is to delte the first occurence of 2

    so i need to : find(2)

    erase(it)

    but when i erase
    it is resulting in 1 3 3 4 5 6 7 8 8 9
    but the expected outcome is
    1 2 3 3 4 5 6 7 8 8 9

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You may need to post your code because my code doesn't produce unexpected output.

    Code:
    #include <set>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
        int arr[] = {1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 8, 9};
        multiset<int> s(arr, arr + sizeof(arr) / sizeof(arr[0]));
    
        copy(s.begin(), s.end(), ostream_iterator<int>(cout, " ")); cout << '\n';
        s.erase(s.find(2));
        copy(s.begin(), s.end(), ostream_iterator<int>(cout, " ")); cout << '\n';
    }
    1 2 2 3 3 4 5 6 7 8 8 9
    1 2 3 3 4 5 6 7 8 8 9
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Formatting Using STL
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 05:52 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM