首页 > 代码库 > 容器set相关算法

容器set相关算法


  1. set_union

    算法set_union可构造S1S2的并集。此集合内含S1S2内的每一个元素。S1S2及其并集都是以排序区间表示。返回值是一个迭代器,指向输出区间的尾端。

    由于S1S2内的每个元素都不需唯一,因此,如果某个值在S1出现n次,在S2出现m次,那么该值再输出区间中会出现max(m,n)次,其中n个来自S1,其余来自S2。在STL set容器内,m小于等于1n小于等于1

    template <class InputIterator1,classInputIterator2,class OutputIterator>

    OutputIterator set_union(InputIterator1first1,InputIterator1 last1,

                                              InputIterator2 first2,InputIterator2 last2,

                                              OutputIterator result)

    {

           while(first1 != last1 && first2 != last2)

           {

                  if(*first1 < *first2)

                  {

                        *result= *first1;

                        ++first1;

                  }

                  elseif (*first2 < *first1)

                  {

                        *result= *first2;

                        ++first2;

                  }

                  else

                  {

                        *result= *first1;

                        ++first1;

                        ++first2;

                  }

                  ++result;

           }

    returncopy(first2,last2,copy(first1,last1,result));

    }

  2. set_intersection

    算法set_intersection可构造S1S2的交集。此集合内含同时出现于S1S2内的每一个元素。S1S2及其交集都是以排序区间表示。返回值为一个迭代器,指向输出区间的尾端。

    由于S1S2内的每个元素都不需唯一,因此,如果某个值在S1出现n次,在S2出现m次,那么该值再输出区间中出现min(m,n)次,并且全部来自S1。在STL set容器内,m小于等于1n小于等于1

    template <class InputIterator1,classInputIterator2,class OutputIterator>

    OutputIteratorset_intersection(InputIterator1 first1,InputIterator1 last1,

                                              InputIterator2 first2,InputIterator2 last2,

                                              OutputIterator result)

    {

           while(first1 != last1 && first2 != last2)

           {

                  if(*first1 < *first2)

                  {

                        //*result= *first1;

                        ++first1;

                  }

                  elseif (*first2 < *first1)

                  {

                        //*result= *first2;

                        ++first2;

                  }

                  else

                  {

                        *result= *first1;

                        ++first1;

                        ++first2;

                        ++result;

                  }

                  //++result;

           }

           //returncopy(first2,last2,copy(first1,last1,result));

    }

  3. set_difference

    算法set_difference可构造S1S2的差集。此集合内含“出现于S1但不出现于S2”的每一个元素。S1S2及其差集都是以排序区间表示。返回值为一个迭代器,指向输出区间的尾端。

    由于S1S2内的每个元素都不需唯一,因此如果某个值再S1出现n次,在S2出现m次,那么该值再输出区间中出现max(n-m,0)次,并且全部来自S1。在STL set容器内,m小于等于1n小于等于1

    template <class InputIterator1,classInputIterator2,class OutputIterator>

    OutputIteratorset_difference(InputIterator1 first1,InputIterator1 last1,

                                              InputIterator2 first2,InputIterator2 last2,

                                              OutputIterator result)

    {

           while(first1 != last1 && first2 != last2)

           {

                  if(*first1 < *first2)

                  {

                        *result= *first1;

                        ++first1;

                        ++result;

                  }

                  elseif (*first2 < *first1)

                  {

                        //*result= *first2;

                        ++first2;

                  }

                  else

                  {

                        //*result= *first1;

                        ++first1;

                        ++first2;

                        //++result;

                  }

                  //++result;

           }

           //returncopy(first2,last2,copy(first1,last1,result));

           returncopy(first1,last2,result);

    }

  4. set_symmetric_difference

    算法set_symmetric_difference可构造S1S2的对称差集。此集合内含“出现于S1但不出现于S2”以及“出现于S2但不出现于S1”的每一个元素。S1S2及其差集都是以排序区间表示。返回值为一个迭代器,指向输出区间的尾端。

    由于S1S2内的每个元素都不需唯一,因此如果某个值在S1出现n次,在S2出现m次,那么该值再输出区间中出现|m-n|次。如果n>m,输出区间内的最后n-m个元素将由S1复制而来,如果n<m则输出区间内的最后m-n个元素将由S2复制而来。在STL set容器内,m小于等于1n小于等于1

    template <class InputIterator1,classInputIterator2,class OutputIterator>

    OutputIteratorset_symmetric_difference(InputIterator1 first1,InputIterator1 last1,

                                              InputIterator2 first2,InputIterator2 last2,

                                              OutputIterator result)

    {

           while(first1 != last1 && first2 != last2)

           {

                  if(*first1 < *first2)

                  {

                        *result= *first1;

                        ++first1;

                        ++result;

                  }

                  elseif (*first2 < *first1)

                  {

                        *result= *first2;

                        ++first2;

                        ++result;

                  }

                  else

                  {

                        //*result= *first1;

                        ++first1;

                        ++first2;

                        //++result;

                  }

                  //++result;

           }

           returncopy(first2,last2,copy(first1,last1,result));

           //returncopy(first1,last2,result);

    }