Click here to Skip to main content
15,867,756 members
Articles / Programming Languages / C++

cpplinq: Set Operators

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
23 Nov 2012CPOL 6.7K   2  
In this post, I will discuss the set operators the library provides

In the previous posts, I introduced cpplinq, a C++ template library that provides .NET-like query operators for sequences of objects in C++11. In this third installment, I will discuss the set operators the library provides.

There are four set operators: distinct, union_with (called so because union is a keyword in C++), intersect_with (suffix _with is for consistency with the union operator) and except. These operators should be pretty straight forward: distinct eliminates the duplicate elements from a sequence, yielding a new sequence with only the distinct elements (in the order their original order), union_with produces the set union of two sequences, intersect_with produces the set intersection of two sequences and except produces the set difference of two sequences.

Before seeing some examples, it worth nothing that all these operators delay the traversal of the sequences until the resulting object is enumerated.

Let’s see some examples:

C++
int numbers[] = {5,4,3,2,1,2,3,4,5};
auto result = from_array(numbers) >> distinct() >> to_vector(); // yields {5,4,3,2,1}
C++
auto result = empty<int>() >> union_with(range(1,5)) >> to_vector(); // yields {1,2,3,4,5}

int set1[] = {5,4,3,2,1,2,3,4,5};
int set2[] = {4,5,6,7};
auto set3 = from_array(set1) >> union_with(from_array(set2)) >> to_vector(); // yields {5,4,3,2,1,6,7}
auto set4 = from_array(set2) >> union_with(from_array(set1)) >> to_vector(); // yields {4,5,6,7,3,2,1}
C++
auto result1 = empty<int>() >> intersect_with( range(0, 10) ) >> to_vector(); // yields {}
auto result2 = range(0, 10) >> intersect_with( empty<int>() ) >> to_vector(); // yields {}

int set1[] = {5,4,3,2,1,2,3,4,5};
int set2[] = {4,5,6};
auto set3 = from_array(set1) >> intersect_with(from_array(set2)) >> to_vector(); // yields {5,4}
auto set3 = from_array(set2) >> intersect_with(from_array(set1)) >> to_vector(); // yields {4,5}
C++
auto result1 = empty<int>() >> except( range(0, 10) ) >> to_vector(); // yields {}
auto result2 = range(0, 10) >> except( empty<int>() ) >> to_vector(); // yields {0,1,2,3,4,5,6,7,8,9}

int set1[] = {5,4,3,2,1,2,3,4,5};
int set2[] = {4,5,6,7};
auto set3 = from_array(set1) >> except(from_array(set2)) >> to_vector(); // yields {3,2,1}
auto set4 = from_array(set2) >> except(from_array(set1)) >> to_vector(); // yields {6,7}

You can learn more about these operators (and the others that are implemented) by reading the cpplinq query operators documentation.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Visma Software
Romania Romania
Marius Bancila is the author of Modern C++ Programming Cookbook and The Modern C++ Challenge. He has been a Microsoft MVP since 2006, initially for VC++ and nowadays for Development technologies. He works as a system architect for Visma, a Norwegian-based company. He works with various technologies, both managed and unmanaged, for desktop, cloud, and mobile, mainly developing with VC++ and VC#. He keeps a blog at http://www.mariusbancila.ro/blog, focused on Windows programming. You can follow Marius on Twitter at @mariusbancila.

Comments and Discussions

 
-- There are no messages in this forum --