#include <iostream>
#include <algorithm>
#include <iterator>
//Sorted Ranges上的set操作
int main()
{
/************************************************************************/
//includes
/************************************************************************/
/*includes:测试某个集合是否为另一个集合的子集合,此处两个集合都以sorted range来表示
[first1,last1)是大集合,而[first2,last2)是小集合,
即当且仅当[first2,last2)中的每个元素在[first1,last1)中存在等价元素,函数返回true
并且当某个元素在[first2,last2)中出现了m次,在[first1,last1)中出现了n次,若m<n,函数返回false
*/
/*
template<class InputIterator1, class InputIterator2>
bool includes(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last1
);
template<class InputIterator1, class InputIterator2, class BinaryPredicate>
bool includes(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last1,
BinaryPredicate _Comp
);
*/
int a1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int a2[] = { 1, 4, 7, 9 };
int a3[] = { 1, 2, 8, 12 };
int b1[] = { 1, 1, 4, 5, 6, 8, 8, 9 };
int b2[] = { 1, 1, 5, 8 };
int b3[] = { 1, 1, 1, 8, 9 };
//a2 contains in a1: true
std::cout << "a2 contains in a1: "
<< (std::includes(std::begin(a1), std::end(a1), std::begin(a2), std::end(a2)) ? "true" : "false")
<< std::endl;
//a3 contains in a1: false
std::cout << "a3 contains in a1: "
<< (std::includes(std::begin(a1), std::end(a1), std::begin(a3), std::end(a3)) ? "true" : "false")
<< std::endl;
//b2 contains in b1: true
std::cout << "b2 contains in b1: "
<< (std::includes(std::begin(b1), std::end(b1), std::begin(b2), std::end(b2)) ? "true" : "false")
<< std::endl;
//b3 contains in b1: false
std::cout << "b3 contains in b1: "
<< (std::includes(std::begin(b1), std::end(b1), std::begin(b3), std::end(b3)) ? "true" : "false")
<< std::endl;
std::cout << "============================" << std::endl;
/************************************************************************/
//set_union
/************************************************************************/
/*set_union:两个集合的联集(并集),两个集合及联集都是以sorted ranges表示
如果某个值在[first1,last1)中出现了m次,在[first2,last2)中出现了n次,则在联集中会出现max(m,n)次,
即:在等价的元素中有m个是从[first1,last1)复制过来的,而其他max(n-m,0)个是从[first2,last2)中复制过来的
set_union属于稳定操作,在两个ranges中都存在的元素,一定是从第一个range中复制到结果中去的
*/
/*
template<class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_union(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2,
OutputIterator _Result
);
template<class InputIterator1, class InputIterator2, class OutputIterator, class BinaryPredicate>
OutputIterator set_union(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2,
OutputIterator _Result,
BinaryPredicate _Comp
);
*/
int A1[] = { 1, 3, 5, 7, 9, 11 };
int A2[] = { 1, 1, 2, 3, 5, 7, 8, 9, 13 };
char C1[] = { 'a', 'b', 'B', 'B', 'f', 'H' };
char C2[] = { 'A', 'B', 'b', 'C', 'D', 'F', 'F', 'h', 'h' };
//Union of A1 and A2: 1 1 2 3 5 7 8 9 11 13
std::cout << "Union of A1 and A2: ";
std::set_union(std::begin(A1), std::end(A1), std::begin(A2), std::end(A2),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
//Union of C1 and C2: a b B B C D f F H h
std::cout << "Union of C1 and C2: ";
std::set_union(std::begin(C1), std::end(C1), std::begin(C2), std::end(C2),
std::ostream_iterator<char>(std::cout, " "), [](const char c1, const char c2)
{
return tolower(c1) < tolower(c2);
});
std::cout << std::endl;
std::cout << "========================" << std::endl;
/************************************************************************/
//set_intersection
/************************************************************************/
/*set_intersection:两集合的交集,其中两集合及交集都是以sorted range表示
如果某个值在[first1,last1)中出现m次,在[first2,last2)中出现n次,则该值在结果中出现min(m,n)次
set_intersection属于稳定操作,在两个ranges中都存在的元素,一定是从第一个range中复制到结果中去的
*/
/*
template<class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_intersection(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2,
OutputIterator _Result
);
template<class InputIterator1, class InputIterator2, class OutputIterator, class BinaryPredicate>
OutputIterator set_intersection(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2,
OutputIterator _Result,
BinaryPredicate _Comp
);
*/
//Intersection of A1 and A2: 1 3 5 7 9
std::cout << "Intersection of A1 and A2: ";
std::set_intersection(std::begin(A1), std::end(A1), std::begin(A2), std::end(A2),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
//Intersection of C1 and C2: a b B f H
std::cout << "Intersection of C1 and C2: ";
std::set_intersection(std::begin(C1), std::end(C1), std::begin(C2), std::end(C2),
std::ostream_iterator<char>(std::cout, " "),[](const char c1, const char c2)
{
return tolower(c1) < tolower(c2);
});
std::cout << std::endl;
std::cout << "================================" << std::endl;
/************************************************************************/
//set_difference
/************************************************************************/
/*set_difference:两集合之差,其中两集合及差集都是以sorted ranges表示
如果某个值在[first1,last1)中出现了m次,在[first2,last2)中出现了n次,那么该值在差集中出现max(m-n,0)次
set_difference属于稳定操作,即差集中的元素均来自第一个range,而非第二个range中
*/
/*
template<class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_difference(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2,
OutputIterator _Result
);
template<class InputIterator1, class InputIterator2, class OutputIterator, class BinaryPredicate>
OutputIterator set_difference(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2,
OutputIterator _Result,
BinaryPredicate _Comp
);
*/
//Difference of A1 and A2: 11
std::cout << "Difference of A1 and A2: ";
std::set_difference(std::begin(A1), std::end(A1), std::begin(A2), std::end(A2),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
//Difference of C1 and C2: B
std::cout << "Difference of C1 and C2: ";
std::set_difference(std::begin(C1), std::end(C1), std::begin(C2), std::end(C2),
std::ostream_iterator<char>(std::cout, " "),[](const char c1, const char c2)
{
return tolower(c1) < tolower(c2);
});
std::cout << std::endl;
std::cout << "==============================" << std::endl;
/************************************************************************/
//set_symmetric_difference
/************************************************************************/
/*set_symmetric_difference:两集合的对称差,即出现于第一个range而不出现于第二个range的所有元素及出现于第二个range而不出现于第一个range的所有元素
如果某个值在[first1,last1)中出现m次,在[first2,last2)中出现n次,则该值在结果集中出现|m-n|次
*/
/*
template<class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_symmetric_difference(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2,
OutputIterator _Result
);
template<class InputIterator1, class InputIterator2, class OutputIterator, class BinaryPredicate>
OutputIterator set_symmetric_difference(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2,
OutputIterator _Result,
BinaryPredicate _Comp
);
*/
//Symmetric_difference of A1 and A2: 1 2 8 11 13
std::cout << "Symmetric_difference of A1 and A2: ";
std::set_symmetric_difference(std::begin(A1), std::end(A1), std::begin(A2), std::end(A2),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
//Symmetric_difference of C1 and C2: B C D F h
std::cout << "Symmetric_difference of C1 and C2: ";
std::set_symmetric_difference(std::begin(C1), std::end(C1), std::begin(C2), std::end(C2),
std::ostream_iterator<char>(std::cout, " "),[](const char c1, const char c2)
{
return tolower(c1) < tolower(c2);
});
std::cout << std::endl;
return 0;
}
====================打个广告,欢迎关注====================
QQ: | 412425870 |
csdn博客: | http://blog.youkuaiyun.com/caychen |
码云: | https://gitee.com/caychen/ |
github: | https://github.com/caychen |
点击群号或者扫描二维码即可加入QQ群: | ![]() |
![]() |