Results 1 to 6 of 6

Thread: How to use the set_difference() in C++

  1. #1
    Join Date
    Dec 2009
    Posts
    27

    How to use the set_difference() in C++

    I am the studying in the MSC-IT. I can know basic the concept of functions. I can have learn the C++ language in BSC. So, I want to know about the set_difference() function of the C++ language that can be in-built into the library of the C++ language. I also would like to know about how to use the set_difference() function of the C++ language. Anyone can help me to know about the set_difference() function of the C++ language, Reply Me!!!

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    The set_difference() in C++

    The set_difference() function algorithm can be used to compute the difference between two given sets that can be defined by the range [strt1,nd1) and the range [strt2,nd2) and the set_difference() function can stores the difference that can be beginning at result. Both of the sets that can be given as ranges, can must be sorted in an ascending order. The set_difference() function can returns a value that can be an iterator to the end of the resultant range.

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    How to use the set_difference() in C++

    The set_difference() function of the C++ language can be used to construct a range that can be sorted starting at the location that can be pointed by the result with the given set difference of range [fst1,lst1) with respect to the range [fst2,lst2) as content. The following can be the syntax of the set_difference() function in the C++ language :
    Code:
    template <class InptItrtr1, class InptItrtr2, class OtptItrtr>
      OtptItrtr set_difference ( InptItrtr1 first1, InptItrtr1 last1,
                                      InptItrtr2 first2, InptItrtr2 last2,
                                      OtptItrtr result );

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Use the set_difference() in C++

    The following program can demonstrates you how to implement the set_difference() function of the C++ language in the C++ programming as follows :
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    #include <algorithm>
    #include <iterator>
    int main()
    {
       const int SZ1 = 10, SZ2 = 5, SZ3 = 20;
       int a1[ SZ1 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
       int a2[ SZ2 ] = { 4, 5, 6, 7, 8 };
       int a3[ SZ2 ] = { 4, 7, 6, 11, 15 };
       std::ostream_iterator< int > output( cout, " " );
       cout << "a1 contains: ";
       std::copy( a1, a1 + SZ1, output );
       cout << "\na2 contains: ";
       std::copy( a2, a2 + SZ2, output );
       cout << "\na3 contains: ";
       std::copy( a3, a3 + SZ2, output );
       int diff[ SZ1 ];
       int *p = std::set_diff( a1, a1 + SZ1, a2, a2 + SZ2, diff);
       cout << "\n\nset_diff of a1 and a2 is: ";
       std::copy( diff, p, output );
       return 0;
    }
    The output of above code :
    Code:
    a1 contains: 1 2 3 4 5 6 7 8 9 10
    a2 contains: 4 5 6 7 8
    a3 contains: 4 7 6 11 15
    
    set_diff of a1 and a2 is: 1 2 3 9 10

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    set_difference() in C++

    The following can be the parameters of the set_difference() function of the C++ language :
    1. fst2, lst2 : These parameters of the set_difference() function can contains an input iterators to the final and the starting positions of the given second sequence. The range used can be as [fst2,lst2).
    2. cmp : This parameters of the set_difference() function can contains an object of the comparison function that can taking two values of the same type than those that can contained in the given specified range.
    3. first1, last1 : These parameters of the set_difference() function can contains an input iterators to the final and starting positions of the given first sequence. The range used can be as [fst1,lst1).

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to use the set_difference() in C++

    This can be one more example for you that can states you what is the use and how to use the set_difference() function in the C++ programming as follows :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main () 
    {
      int ft[] = {5,10,15,20,25};
      int sc[] = {50,40,30,20,10};
      vector<int> d(10);                 
      vector<int>::iterator t;
      sort (ft,ft+5);   
      sort (sc,sc+5);  
      t=set_difference (ft, ft+5, sc, sc+5, d.begin());
      cout << "difference has " << int(t - d.begin()) << " elements.\n";
      return 0;
    }
    Output:
    Code:
    difference has 3 elements

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,299,334.64471 seconds with 16 queries