#include <algorithm>
#include "afxtempl.h"//type class must have <
Template<class TYPE, Class arg_type>
void Sort (carray<type,arg_type>& dest)
{
Std::sort (dest. GetData (), Dest. GetData () +dest. GetSize ());
}//constructs an ordered sequence that retains only elements that exist in the first sequence and that do not exist in the second.
Src1,src2 must already be sorted
Template<class TYPE, Class arg_type>
void Setdifference (carray<type,arg_type>& dest,const carray<type,arg_type>& src1,const CArray <TYPE,ARG_TYPE>& src2)
{
Dest. SetSize (SRC1. GetSize ());
Const type* p = std::set_difference (Src1. GetData (), Src1. GetData () +src1. GetSize (),
Src2. GetData (), Src2. GetData () +src2. GetSize (),
Dest. GetData ());
Dest. SetSize (p-dest. GetData ());
};//constructs an ordered sequence that contains all the non-repeating elements in two sequences.
Src1,src2 must already be sorted
Template<class TYPE, Class arg_type>
void Setunion (carray<type,arg_type>& dest,const carray<type,arg_type>& src1,const CArray< type,arg_type>& src2)
{
Dest. SetSize (SRC1. GetSize () +src2. GetSize ());
Const type* p = std::set_union (Src1. GetData (), Src1. GetData () +src1. GetSize (),
Src2. GetData (), Src2. GetData () +src2. GetSize (),
Dest. GetData ());
Dest. SetSize (p-dest. GetData ());
};//constructs an ordered sequence in which elements exist in two sequences.
Src1,src2 must already be sorted
Template<class TYPE, Class arg_type>
void Setintersection (carray<type,arg_type>& dest,const carray<type,arg_type>& src1,const CArray <TYPE,ARG_TYPE>& src2)
{
Dest. SetSize (min (src1). GetSize (), Src2. GetSize ()));
Const type* p = std::set_intersection (Src1. GetData (), Src1. GetData () +src1. GetSize (),
Src2. GetData (), Src2. GetData () +src2. GetSize (),
Dest. GetData ());
Dest. SetSize (p-dest. GetData ());
};//constructs an ordered sequence that takes a symmetric difference set of two sequences (a set-intersection)
Src1,src2 must already be sorted
Template<class TYPE, Class arg_type>
void Setsymmetricdifference (carray<type,arg_type>& dest,const carray<type,arg_type>& Src1, Const carray<type,arg_type>& SRC2)
{
Dest. SetSize (SRC1. GetSize () +src2. GetSize ());
Const type* p = std::set_symmetric_difference (Src1. GetData (), Src1. GetData () +src1. GetSize (),
Src2. GetData (), Src2. GetData () +src2. GetSize (),
Dest. GetData ());
Dest. SetSize (p-dest. GetData ());
}; CString lngarrtostr (const carray<long,const long&>& src)//convert long array to str for ease of troubleshooting
{
CString str;
Str. Format ("%d elements", SRC. GetSize ());
for (int i = 0; i < src. GetSize (); i++)
{
CString s;
S.format ("%d", src[i]);
str + = s;
}
return str;
}void Gongnengceshi ()//Functional test
{
Srand (GetTickCount ());
const int iNum = 11;
Carray<long,const long&> Src1,src2;
Src1. SetSize (INum);
Src2. SetSize (INum);
for (int i = 0; i < iNum; i++)
{
Src1[i] = rand ()%10;
Src2[i] = rand ()%10;
} const CString STR1 = LNGARRTOSTR (SRC1);
Const CString STR2 = LNGARRTOSTR (SRC2); Sort (SRC1);
Sort (SRC2); Const CString STR3 = LNGARRTOSTR (SRC1);
Const CString STR4 = LNGARRTOSTR (SRC2); Carray<long,const long&> DST;
Setdifference (DST,SRC1,SRC2);
Const CString STR5 = LNGARRTOSTR (DST); Setunion (DST,SRC1,SRC2);
Const CString STR6 = LNGARRTOSTR (DST);
Setintersection (DST,SRC1,SRC2);
Const CString STR7 = LNGARRTOSTR (DST); Setsymmetricdifference (DST,SRC1,SRC2);
Const CString STR8 = LNGARRTOSTR (DST);
}//adds elements to the array, which can be called consecutively.
Template<class TYPE, Class arg_type>
Class Ctestarray:public Carray<type,arg_type>
{
Public
ctestarray<type,arg_type>& Addlianxu (Arg_type newelement)
{
ADD (newelement);
return *this;
};
};void Bianjiecheshi ()//Boundary test
{
Ctestarray<long,const long&> Src1,src2;
Src1. Addlianxu (1). Addlianxu (2). Addlianxu (3);
Sort (SRC1);
Sort (SRC2); Carray<long,const long&> DST;
The result is src1.
Setdifference (DST,SRC1,SRC2);
Const CString STR1 = LNGARRTOSTR (DST);
Result is empty
Setdifference (DST,SRC1,SRC1);
Const CString STR2 = LNGARRTOSTR (DST); Ctestarray<long,const long&> SRC3;
Src3. Addlianxu (4). Addlianxu (5);
No weight no element
Setunion (DST,SRC1,SRC3);
Const CString STR3 = LNGARRTOSTR (DST);
All are repeating elements
Setunion (DST,SRC1,SRC1);
Const CString STR4 = LNGARRTOSTR (DST); No weight no element
Setintersection (DST,SRC1,SRC3);
Const CString STR5 = LNGARRTOSTR (DST);
All are repeating elements
Setintersection (DST,SRC1,SRC1);
Const CString STR6 = LNGARRTOSTR (DST); No weight no element
Setsymmetricdifference (DST,SRC1,SRC3);
Const CString STR7 = LNGARRTOSTR (DST);
All are repeating elements
Setsymmetricdifference (DST,SRC1,SRC1);
Const CString STR8 = LNGARRTOSTR (DST);}
Using STL's aggregate class functions to manipulate MFC arrays